MySQL Data Directory change in Centos




1.Check the current data directory location of mysql.

mysql>  select @@datadir 
+-----------------+
| @@datadir       |
+-----------------+
| /var/lib/mysql/ |
+-----------------+
1 row in set (0.00 sec)   

2.Stop Mysql service

[root@localhost ~]# systemctl stop mysqld

3. Now create a new location for mysql data directory by executing a following command.

[root@localhost ~]# mkdir /home/MysqlData/

4. Transfer the old mysql data’s into new location by creating a new mysql location and run the command as follows.

[root@localhost ~]# rsync -pavzxl /var/lib/mysql/ /home/MysqlData/

5.Take the backup of old mysql location and move the contents.

[root@localhost ~]# mv /var/lib/mysql/ /var/lib/mysql.bkp/

6. Open the my.cnf file and make changes as shown below. Save and exit from the configuration file


[client]
port=3306
socket=/home/MysqlData/mysql.sock


[mysqld]
datadir=/home/MysqlData/
socket=/home/MysqlData/mysql.sock


7. Give Write access to Mysql directory

chown -R mysql:mysql /home/MysqlData/

8. Now start the mysql service.

[root@localhost ~]# systemctl start mysqld

9. If you got below error. Please follow as shown here

Error: SELinux is preventing /usr/libexec/mysqld "write" access on /home/MysqlData.

Solution: 
run this command
semanage fcontext -a -t mysqld_db_t "/home/MysqlData(/.*)?"

10. Now use the restorecon command to apply this context mapping to the running system:
~]# restorecon -R -v /home/MysqlData

11. Now that the /mysql/ location has been labeled with the correct context for MySQL, the mysqld daemon starts:

[root@localhost ~]# systemctl start mysqld


Comments

Popular Posts