本文主要介绍在CentOS下通过yum命令安装mysql之后,如何移动默认数据目录到指定位置。
Mysql 安装之后默认的数据文件存储目录为/var/lib/mysql。
假如要把目录移到/home/data下需要进行下面几步:
1、home目录下建立data目录
cd /home mkdir data
2、把MySQL服务进程停掉:
service mysqld stop
3、把/var/lib/mysql 整个目录移到 /home/data
mv /var/lib/mysql /home/data/
这样就把MySQL的数据文件移动到了/home/data/mysql下
然后再次创建一个新的空目录,并制作一个软连接到新的sock路径:
mkdir /var/lib/mysql ln -s /home/data/mysql/mysql.sock /var/lib/mysql/mysql.sock
4、vim /etc/my.cnf
如果/etc/目录下没有my.cnf配置文件,请到/usr/share/mysql/下找到*.cnf文件,拷贝其中一个到/etc/并改名为my.cnf。
为保证MySQL能够正常工作,需要指明mysql.sock文件的产生位置。修改
datadir=/home/data/mysql socket=/home/data/mysql/mysql.sock
另外还要注意目录的属主和权限,代码如下:
chown -R mysql:mysql /home/data/mysql/ chmod -R 700 /home/data/mysql/
5、重新启动MySQL服务
service mysqld start
如果工作正常,则移动就成功了。
最后,查看日志tail -300f /var/log/mysqld.log,如果发现:
2021-12-18T06:27:46.683340Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables 2021-12-18T06:27:46.683479Z 0 [ERROR] InnoDB: Operating system error number 13 in a file operation. 2021-12-18T06:27:46.683491Z 0 [ERROR] InnoDB: The error means mysqld does not have the access rights to the directory. 2021-12-18T06:27:46.683504Z 0 [ERROR] InnoDB: Operating system error number 13 in a file operation. 2021-12-18T06:27:46.683512Z 0 [ERROR] InnoDB: The error means mysqld does not have the access rights to the directory. 2021-12-18T06:27:46.683521Z 0 [ERROR] InnoDB: Cannot open datafile './ibtmp1' 2021-12-18T06:27:46.683530Z 0 [ERROR] InnoDB: Unable to create the shared innodb_temporary 2021-12-18T06:27:46.683541Z 0 [ERROR] InnoDB: Plugin initialization aborted with error Cannot open a file 2021-12-18T06:27:47.286272Z 0 [ERROR] Plugin 'InnoDB' init function returned error. 2021-12-18T06:27:47.286312Z 0 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed. 2021-12-18T06:27:47.286321Z 0 [ERROR] Failed to initialize builtin plugins. 2021-12-18T06:27:47.286329Z 0 [ERROR] Aborting 2021-12-18T06:27:47.286450Z 0 [Note] Binlog end 2021-12-18T06:27:47.286514Z 0 [Note] Shutting down plugin 'CSV' 2021-12-18T06:27:47.286774Z 0 [Note] /usr/sbin/mysqld: Shutdown complete
这里是因为没有权限创建或读取文件。
解决办法就是使用setenforce 0 这个命令,让系统关闭权限校验,然后再运行命令 service mysqld start 发现OK了。
另外一个办法是restorecon -FRv /home/data/mysql,或者chcon -R -t mysqld_db_t /home/data/mysql。