MySQL database Migration data folder location detailed steps

  • 2020-06-12 10:49:20
  • OfStack

Since yum installed mysql, the database's data directory is under /var/lib by default, it needs to be moved to the /data partition for data security reasons. The steps are as follows:
1. Close apache and mysql.


service httpd stop
service mysqld stop

2. Move the mysql directory under /var/lib to the data directory.
Why use the mv command instead of the cp command? Because of the linux file system specificity, the mv command preserves all attributes and permissions of a file, especially the selinux attribute. If you use the cp command, you need to go back and set the selinux attribute of the mysql folder. I have a headache and selinux can avoid it.

mv -R /var/lib/mysql /data/mysql

3. Modify mysql configuration file /etc/ ES33en.cnf. Change the path to datadir and socket to the /data directory.

[mysqld]
#datadir=/var/lib/mysql                      ------ The default path of the original system 
datadir=/home/mysql ------ The existing path 
#socket=/var/lib/mysql/mysql.sock            ------ The original socket The path is 
socket=/home/mysql/mysql.sock                ------ The existing path 

[mysqld_safe]
socket=/home/mysql/mysql.sock                 ----- The existing path 
[client]
socket=/home/mysql/mysql.sock                 ----- The existing path 
[mysql.server]
socket=/home/mysql/mysql.sock                 ----- The existing path 

4. Modify the socket path in the php profile (/etc/ php.ini).
Don't forget that php.ini also specifies the path of socket, otherwise the php website will not be able to connect to the database. php.ini default socket path is empty, the default is to point to /var/lib/mysql, so change to /data/mysql.

[mysql]
mysql.default_socket = /home/mysql/mysql.sock
[mysqli]
mysql.default_socket = /home/mysql/mysql.sock

5. Start apache. mysql.

service httpd start
service mysqld start


Related articles: