LVM+Mysql for hot backup

System environment:

rooky8.0.6 192.168.111.111

1. Install mysql and download these apps.

[root@localhost mysql]# yum list installed | grep mysql
mysql.x86_64                         8.0.26-1.module+el8.4.0+652+6de068a7   @appstream
mysql-common.x86_64                  8.0.26-1.module+el8.4.0+652+6de068a7   @appstream
mysql-errmsg.x86_64                  8.0.26-1.module+el8.4.0+652+6de068a7   @appstream
mysql-server.x86_64                  8.0.26-1.module+el8.4.0+652+6de068a7   @appstream
[root@localhost mysql]# yum list installed | grep mariadb
mariadb-connector-c-config.noarch    3.1.11-2.el8_3                         @appstream

 No password is required when entering mysql for the first time. Therefore, you need to set your own password to enter mysql. code show as below:

mysql> use mysql
mysql> alter user 'root'@'localhost' identified by '123456';

 2. Create LVM

 Image source: Detailed explanation of LVM for disk management under Linux and common disk operation commands of lvm_yg@hunter's blog-CSDN blog_lvm command

 1. Add a disk to the virtual machine and partition it.

 As shown in the figure below, an sdb disk is added and the sdb disk is divided into an sdb1 partition.

[root@localhost mysql]# lsblk 
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0   60G  0 disk 
├─sda1        8:1    0    2G  0 part /boot
└─sda2        8:2    0   58G  0 part 
  ├─rl-root 253:0    0   56G  0 lvm  /
  └─rl-swap 253:1    0    2G  0 lvm  [SWAP]
sdb           8:16   0    8G  0 disk 
└─sdb1        8:17   0    8G  0 part 
sr0          11:0    1 1024M  0 ro

 After creation, you need to restart or execute "partprobe" to use it.

##重启磁盘
[root@localhost mysql]# partprobe

2. Change the partition to lvm type.

[root@localhost mysql]# fdisk /dev/sdb

 Change /dev/sdb to linux LVM mode.

 

 3. Create a physical volume (pv).

[root@localhost mysql]# pvcreate /dev/sdb1
WARNING: dos signature detected on /dev/sdb1 at offset 510. Wipe it? [y/n]: y
  Wiping dos signature on /dev/sdb1.
  Physical volume "/dev/sdb1" successfully created.

[root@localhost mysql]# pvdisplay 
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               rl
  PV Size               <58.00 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              14847
  Free PE               0
  Allocated PE          14847
  PV UUID               29vJcC-MIan-Lfc5-Rc5F-e5Um-jPOd-Sktu1f
   
  "/dev/sdb1" is a new physical volume of "<8.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name               
  PV Size               <8.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               CdPpfu-irJu-NkEM-brKk-qp3V-GKbh-P9bpLY

 4. Create a volume group (VG).

When creating a volume group, you can also see the advantages of LVM. You can combine multiple disks into one space.
Assume: there are two partitions: /dev/sda1 and /dev/sdb1, but these two partitions are not on the same disk. .
We can use vgcreate [name] /dev/sda1 /dev/sdb1 to combine the two spaces into one space for use by the following logical volume (LV).

And I only have a space of /dev/sdb1, so I used one.

### vgcreate [name] [指定的物理卷]
[root@localhost mysql]# vgcreate vgtest /dev/sdb1
  Volume group "vgtest" successfully created

[root@localhost mysql]# vgdisplay 
  --- Volume group ---
  VG Name               vgtest
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <8.00 GiB
  PE Size               4.00 MiB
  Total PE              2047
  Alloc PE / Size       0 / 0   
  Free  PE / Size       2047 / <8.00 GiB
  VG UUID               K03kGL-RXk8-ZH89-NLFd-sVAV-S0mL-y4PZ6s

5. Create a logical volume (LV)

lvcreate -L [卷的大小] -n [卷的名字] 卷组的名字
[root@localhost mysql]# lvcreate -L 5G -n lvtest vgtest
  Logical volume "lvtest" created.


[root@localhost mysql]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/vgtest/lvtest
  LV Name                lvtest
  VG Name                vgtest
  LV UUID                DCnBvW-dBcP-tKke-unpZ-y1dT-jpeF-Ht3ujF
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2022-10-15 19:34:28 +0800
  LV Status              available
  # open                 0
  LV Size                5.00 GiB
  Current LE             1280
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2

6. Format LV

[root@localhost mysql]# mkfs.
mkfs.cramfs  mkfs.ext2    mkfs.ext3    mkfs.ext4    mkfs.minix   mkfs.xfs     
[root@localhost mysql]# mkfs.ext4 /dev/vgtest/lvtest 
mke2fs 1.45.6 (20-Mar-2020)
创建含有 1310720 个块(每块 4k)和 327680 个inode的文件系统
文件系统UUID:87851c60-c35b-4db2-b4c3-fc6765973a62
超级块的备份存储于下列块: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736

正在分配组表: 完成                            
正在写入inode表: 完成                            
创建日志(16384 个块)完成
写入超级块和文件系统账户统计信息: 已完成

The differences between each formatting type can be seen:

The difference between linux partition xlf and ext4 partition, detailed explanation of Linux partition types EXT2, EXT3, EXT4_weixin_39907526's blog-CSDN blog

7. Mount

### 这个/mnt/mysql_lv将是我们数据库数据放的目录
[root@localhost mnt]# mount -t ext4 /dev/vgtest/lvtest /mnt/mysql_lv/
[root@localhost mnt]# lsblk 
NAME              MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                 8:0    0   60G  0 disk 
├─sda1              8:1    0    2G  0 part /boot
└─sda2              8:2    0   58G  0 part 
  ├─rl-root       253:0    0   56G  0 lvm  /
  └─rl-swap       253:1    0    2G  0 lvm  [SWAP]
sdb                 8:16   0    8G  0 disk 
└─sdb1              8:17   0    8G  0 part 
  └─vgtest-lvtest 253:2    0    5G  0 lvm  /mnt/mysql_lv
sr0                11:0    1 1024M  0 rom  
[root@localhost mnt]# df -h
文件系统                   容量  已用  可用 已用% 挂载点
devtmpfs                   324M     0  324M    0% /dev
tmpfs                      343M     0  343M    0% /dev/shm
tmpfs                      343M  5.0M  338M    2% /run
tmpfs                      343M     0  343M    0% /sys/fs/cgroup
/dev/mapper/rl-root         56G  2.6G   54G    5% /
/dev/sda1                  2.0G  191M  1.9G   10% /boot
tmpfs                       69M     0   69M    0% /run/user/0
/dev/mapper/vgtest-lvtest  4.9G   20M  4.6G    1% /mnt/mysql_lv

This is a temporary mount. The mount will disappear next time you restart the server, so you need to mount it permanently.

8. Mount permanently.

[root@localhost mnt]# cat /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Tue Jun 28 13:43:47 2022
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/rl-root     /                       xfs     defaults        0 0
UUID=47f5a72c-9eb0-427a-9636-d80cf01eec7f /boot                   xfs     defaults        0 0
/dev/mapper/rl-swap     none                    swap    defaults        0 0
/dev/vgtest/lvtest /mnt/mysql_lv                ext4    defaults        0 0

9. Modify the owner and group of /mnt/mysql_lv.

[root@localhost mnt]# chown -R mysql:mysql /mnt/mysql_lv/
[root@localhost mnt]# ll
总用量 4
drwxr-xr-x. 2 root  root     6 6月  28 21:45 hgfs
drwxr-xr-x  3 mysql mysql 4096 10月 15 19:40 mysql_lv

10. Migrate the database data to /mnt/mysql_lv.

## 在迁移之前和修改默认文件之前,我希望你都可以备份一下原文件,养成好习惯。

mv  /var/lib/mysql/* /mnt/mysql_lv/

##因为我的mysql是8.0.26版本的,所以修改默认配置文件可能不一样,不过修改的内容都一样。

[root@localhost my.cnf.d]# pwd
/etc/my.cnf.d
[root@localhost my.cnf.d]# cat client.cnf 
[client]
port=3306
socket=/mnt/mysql_lv/mysql.sock
[client-mariadb]

[root@localhost my.cnf.d]# cat mysql-server.cnf
[mysqld]
#datadir=/var/lib/mysql
datadir=/mnt/mysql_lv
#socket=/var/lib/mysql/mysql.sock
socket=/mnt/mysql_lv/mysql.sock
#log-error=/var/log/mysql/mysqld.log
log-error=/mnt/mysql_lv/mysqld.log
#pid-file=/run/mysqld/mysqld.pid
pid-file=/mnt/mysql_lv/mysqld.pid

## 另一个文件不用改

Restart mysql

systemctl restart mysqld

10. Then finish

Guess you like

Origin blog.csdn.net/qq_48480384/article/details/127338920