ubuntu mysql挂载盘变更
注:本章介绍,只是单纯修改数据库存储位置,其他mysql功能等功能未修改
1.查看服务器基本信息
命令:df -h
[email protected]:/home# df -h
Filesystem Size Used Avail Use% Mounted on
udev 7.9G 0 7.9G 0% /dev
tmpfs 1.6G 3.5M 1.6G 1% /run
/dev/vda1 40G 11G 27G 28% / <====a
tmpfs 7.9G 0 7.9G 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 7.9G 0 7.9G 0% /sys/fs/cgroup
tmpfs 1.6G 0 1.6G 0% /run/user/0
/dev/vdb1 939G 319G 573G 36% /media/data <====b
<====a 中为原有mysql挂载位置
<====b 为需要转移挂载位置
2.查看mysql的配置文件
进入目录
[email protected]:/etc/mysql/mysql.conf.d# pwd
/etc/mysql/mysql.conf.d
cd /etc/mysql/mysql.conf.d
vim mysqld.cnf
[mysqld]
#
# * Basic Settings
#
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
[datadir = /var/lib/mysql] 即为当前mysql存储路径
迁移
1.创建文件夹
[email protected]:/var/lib# ll /var/lib/
total 184
drwxr-xr-x 46 root root 4096 Nov 23 16:31 ./
drwxr-xr-x 12 root root 4096 Nov 23 16:31 ../
drwxr-x--- 2 root root 4096 Aug 28 15:59 metricbeat/
drwxr-xr-x 2 root root 4096 Apr 13 2016 misc/
drwxr-xr-x 2 root root 4096 Dec 3 06:25 mlocate/
drwx------ 10 mysql mysql 4096 Nov 5 20:53 mysql/
drwx------ 2 mysql mysql 4096 Aug 28 14:21 mysql-files/
drwx------ 2 mysql mysql 4096 Aug 28 14:21 mysql-keyring/
drwxr-xr-x 2 root root 4096 Jul 28 01:55 mysql-upgrade/
看清楚 mysql文件的权限 为 700 且 用户和用户组 都为 mysql,接下来的创建 文件夹必须和这个一样:
修改所属用户 和所属用户组为 msyql
sudo chown -vR mysql:mysql /mysqldata/
修改权限
sudo chmod -vR 700 /mysqldata/
迁移文件
进入 /var/lib 建议使用cp命令 防止万一出现问题
使用su命令 切换到 root 因为 权限 为 700
[email protected]:~# cd /media/data/
[email protected]:/media/data# mkdir mysqldata
[email protected]:/media/data# chown -vR mysql:mysql mysqldata/
changed ownership of ‘mysqldata/‘ from root:root to mysql:mysql
[email protected]:/media/data# chmod -vR 700 mysqldata/
mode of ‘mysqldata/‘ changed from 0755 (rwxr-xr-x) to 0700 (rwx------)
[email protected]:/media/data# ll
total 40
drwxr-xr-x 7 root root 4096 Dec 3 18:21 ./
drwxr-xr-x 5 root root 4096 Aug 30 14:08 ../
drwx------ 2 root root 16384 Aug 30 14:08 lost+found/
drwx------ 2 mysql mysql 4096 Dec 3 18:21 mysqldata/ <====
drwxr-xr-x 5 root root 4096 Nov 14 19:02 Tianqiao_liu/
drwxr-xr-x 14 root root 4096 Dec 1 21:06 workplace/
drwxr-xr-x 14 root root 4096 Oct 28 11:48 workplace_backup/
2.复制文件到新文件中
为了保证 目录权限和属性 和以前的一致 ,加 av 后缀
[email protected]:/media/data# cp -av /var/lib/mysql/* /media/data/mysqldata/
3.修改mysql的配置文件:
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
[mysqld]
#
# * Basic Settings
#
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
修改: datadir = /media/data/mysqldata
[mysqld]
#
# * Basic Settings
#
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /media/data/mysqldata
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
4.修改usr.sbin.mysqld的配置文件:
以上mysql的配置修改完毕
但是 还有 在ubuntu中 有些敏感操作受到了apparmor.d 的限制 ,mysql也受到了限制 所以要修改这个
vim /etc/apparmor.d/usr.sbin.mysqld
在这个文件里找到了修改之前的 mysql 数据库路径:/var/lib/mysql。
改成修改后的数据目录地址
按照示例修改
# Allow data dir access
/var/lib/mysql/ r,
/var/lib/mysql/** rwk,
修改为:
# Allow data dir access
/media/data/mysqldata/ r,
/media/data/mysqldata/** rwk,
5.重启
这里 配置和迁移已经修改完成
从新启动 apparmor 和 mssql
sudo service apparmor reload
sudo service mysql start
6.检测迁移成功
测试 是否迁移成功
a.navicat 建数据库zgc_test_temp
b. 看是否在 修改的 目录中 有 创建 文件夹,如果有说明迁移成功,同样 为了查看 要使用root 删除也要用root
[email protected]:/media/data/mysqldata# ll
total 188480
drwx------ 11 mysql mysql 4096 Dec 3 18:45 ./
drwxr-xr-x 7 root root 4096 Dec 3 18:21 ../
-rw-r----- 1 mysql mysql 56 Aug 28 14:21 auto.cnf
-rw-r--r-- 1 root root 0 Aug 28 14:21 debian-5.7.flag
drwxr-x--- 2 mysql mysql 4096 Sep 6 22:52 dev_koushuti/
-rw-r----- 1 mysql mysql 14097 Dec 3 18:12 ib_buffer_pool
-rw-r----- 1 mysql mysql 79691776 Dec 3 18:42 ibdata1
-rw-r----- 1 mysql mysql 50331648 Dec 3 18:42 ib_logfile0
-rw-r----- 1 mysql mysql 50331648 Dec 3 18:42 ib_logfile1
-rw-r----- 1 mysql mysql 12582912 Dec 3 18:46 ibtmp1
drwxr-x--- 2 mysql mysql 4096 Aug 28 14:27 kou_shu_ti_url/
drwxr-x--- 2 mysql mysql 4096 Aug 28 14:21 mysql/
drwxr-x--- 2 mysql mysql 4096 Aug 28 14:21 performance_schema/
drwxr-x--- 2 mysql mysql 4096 Sep 5 08:54 test/
drwxr-x--- 2 mysql mysql 4096 Nov 25 21:26 test_koushuti/
drwxr-x--- 2 mysql mysql 4096 Sep 26 18:03 user/
drwxr-x--- 2 mysql mysql 4096 Dec 3 18:45 zgc_test_temp/
drwxr-x--- 2 mysql mysql 4096 Nov 7 20:59 zlgc/
参考文献 https://blog.csdn.net/wang794686714/article/details/39273385
https://blog.csdn.net/pony_maggie/article/details/78753325
原文地址:https://www.cnblogs.com/zgcblog/p/10192126.html