Centos下切换MySQL数据到数据盘

centos 7对ECS进行挂载数据盘,并切换MySQL数据保存目录到数据盘

测试需要,申请了阿里云的ECS服务器,用来搭建MySQL,搭建时候没有注意,没想到运维大哥那么体贴的申请了数据盘,还是SSD的,这样也就需要把原来MySQL数据存储路径换到数据盘。

操作数据盘

首先查看当前服务器是否有数据盘:fdisk -l

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Disk /dev/vda: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0008d73a

Device Boot Start End Blocks Id System
/dev/vda1 * 2048 104855551 52426752 83 Linux

Disk /dev/vdb: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

可以看到当前服务上面有两个盘/dev/vda/dev/vdb,其中/dev/vda是系统盘,并且已经有分区/dev/vda1,经常使用的查看磁盘的命令是df -h,但是有个问题,如果你的数据盘没有分区、挂载,使用df -h是不会显示的,只会展示这样:

1
2
3
4
5
6
7
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1 50G 3.1G 44G 7% /
devtmpfs 16G 0 16G 0% /dev
tmpfs 16G 0 16G 0% /dev/shm
tmpfs 16G 520K 16G 1% /run
tmpfs 16G 0 16G 0% /sys/fs/cgroup
tmpfs 3.2G 0 3.2G 0% /run/user/0

数据盘分区

查找到数据盘/dev/vdb之后,对它进行分区:fdisk /dev/vdb

1
2
3
4
5
6
7
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x39e32289.

接下来是有几个选项,选择适合的选项即可:

1
2
3
4
5
Command (m for help):   输入'n',创建一个新分区
Partition type: 输入'p',选择主分区
Partition number (1-4, default 1): 输入'1',仅创建一个分区
First sector (2048-209715199, default 2048): 我用的默认
Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199): 同样默认

接下来wq保存退出即可。
然后这个时候fdisk -l,就能看见新的分区:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Disk /dev/vda: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0008d73a

Device Boot Start End Blocks Id System
/dev/vda1 * 2048 104855551 52426752 83 Linux

Disk /dev/vdb: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x39e32289

Device Boot Start End Blocks Id System
/dev/vdb1 2048 209715199 104856576 83 Linux

格式化分区

首先新建文件夹:cd /homemkdir mysql
挂载:mount /dev/vdb1 /home/mysql
在新分区上创建文件系统:mkfs.ext4 /dev/vdb1
备份/etc/fstabcp /etc/fstab /etc/fstab.bak
查找当前磁盘分区的UUID:blkid
向 /etc/fstab 写入新分区信息:vim /etc/fstab,写入UUID=ec7443b1-14e4-4174-ae8a-2194484a754b /home/mysql ext4 defaults 0 0(UUID是之前获得的)

复制MySQL数据

复制数据:mv /var/lib/mysql/* /home/mysql
修改配置:vim /etc/my.cnf,修改数据文件到/home/mysql
重启MySQL
OVER~

文章目录
  1. 操作数据盘
    1. 数据盘分区
  2. 格式化分区
  3. 复制MySQL数据
|