I had seen many cPanel servers running out of disk space due to MySQL data directory on “/var” partition.To solve this issue you need to move your MySQL data directory to a new location.There are also other situations like moving your MySQL data’s to a new standalone database server or moving it to a separate solid-state-drive partition for increasing MySQL server performance.Whatever the reason, moving MySQL data directory is simple and has no impact on cPanel functionality.
In this article I am moving MySQL data directory to “/home” partition.You can proceed with the following steps for moving MySQL data directory:
1.Create a backup
Please make full database backup(including system tables) before moving your data directory. This action will prevent data losing in case if something goes wrong.
# tar -cvf mysql.tar /var/lib/mysql
2.Edit the my.cnf file
# vi /etc/my.cnf
Now in the mysqld section add the following. Don’t restart MySQL after adding new entry.
datadir=/home/mysql
3.Create the new MySQL data directory
# mkdir /home/mysql
4.Now migrate the data to the new location using rsync command.
# nohup rsync -avp /var/lib/mysql/ /home/mysql
The nohup will keep rsync running even when your session with the server end, the other part “
# tail -f nohup.out
Notice that you have to do the syncing process twice , because when moving large size of data can take some time to complete and the tables may have changed in between. When we run it the second time we hopefully get it so that when the switch over happens there is very little, if any, lost data. If you can afford the downtime simply shut down MySQL before running this command.If you cannot though running it twice then quickly copy/pasting the other commands is a valid substitute.
5.Typically you want to stop MySQL for syncing data completely.
# /etc/init.d/mysqld stop
6.Start the re-sync process once again to copy data’s completely.
# rsync -avp --delete /var/lib/mysql/ /home/mysql/
7. Change ownership of new created MySQL data directory to MySQL.
# chown -R mysql:mysql /home/mysql/
8. Now, re-link the socket file to /tmp:
# rm -rf /tmp/mysql.sock # ln -sf /home/mysql/mysql.sock /tmp/mysql.sock
9. Since you already added the data directory entry to my.cnf , all you need to do is restart again and everything should be working.
# /etc/init.d/mysqld start
Check whether your MySQL logs are written at the new location (Eg: /home/mysql/hostname.err)
10. Create a sample database named “foo” for checking.
# mysqladmin create foo
11. Check whether new database is created at new data directory.
# ls -d /home/mysql/foo
12. After confirming every thing works properly.You can remove the old data directory.
# rm -rf /var/lib/mysql
That’s it !
you rock!
This doesn’t quite work with latest version of MySQL (5.7 & possibly 5.6), at least not for me. You get permission errors when you try to restart MySQL.
There’s a service called apparmor – you need to add the following into /etc/apparmor.d/local/usr.sbin.mysqld :-
/data/ r,
/data/** rwk,
Where /data is the location you’ve moved your databases to.
(Got this from https://askubuntu.com/questions/758898/mysql-wont-start-after-changing-the-datadir-14-04-mysql-5-7)
Hope this helps anyone who googles and hits this page..
Cheers,
Chris.
Ok, in my server cPanel/WHM have the same problem. Thanks for this. But in 12 step crash…when
# /etc/init.d/mysql start
Starting MySQL.180410 23:38:26 mysqld_safe Directory ‘/var/lib/mysql’ for UNIX socket file don’t exists.
ERROR! The server quit without updating PID file (/home/mysql/XXXXX.pid).
Then i create folder and asign user, and now is 100% functional
# mkdir -p /var/lib/mysql
# chown mysql:mysql /var/lib/mysql
Very thanks Vipin.
Cheers Vipin. Great article, helped me move to other folder safely.
Great article. Is it require to check mysql port before restarting the services.
No it won’t need to check the port
# cd /var/lib/
# service mysqld stop
# mkdir /home/_system/
# mv mysql /home/_system/_mysql
# ln -s /home/_system/_mysql/ mysql
# service mysqld start
–>> That’s all folks
Hi.
Thanks for this tutorial, but it’s not working for me at step 9 with the error:
[root@ssd2 ~]# tail -f /var/log/mysqld.log
2023-12-19T00:38:00.188329Z 0 [System] [MY-013172] [Server] Received SHUTDOWN from user . Shutting down mysqld (Version: 8.0.35).
2023-12-19T00:38:04.971432Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.35) MySQL Community Server – GPL.
2023-12-19T00:39:09.851152Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.35) starting as process 15099
2023-12-19T00:39:09.885492Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-12-19T00:39:09.885612Z 1 [ERROR] [MY-012271] [InnoDB] The innodb_system data file ‘ibdata1’ must be writable
2023-12-19T00:39:09.885629Z 1 [ERROR] [MY-012278] [InnoDB] The innodb_system data file ‘ibdata1’ must be writable
2023-12-19T00:39:09.885647Z 1 [ERROR] [MY-010334] [Server] Failed to initialize DD Storage Engine
2023-12-19T00:39:09.885742Z 0 [ERROR] [MY-010020] [Server] Data Dictionary initialization failed.
2023-12-19T00:39:09.885759Z 0 [ERROR] [MY-010119] [Server] Aborting
2023-12-19T00:39:09.886973Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.35) MySQL Community Server – GPL
and
[root@ssd2 ~]# systemctl status mysqld.service -l
● mysqld.service – MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Drop-In: /etc/systemd/system/mysqld.service.d
└─limits.conf, protecthome.conf
Active: failed (Result: exit-code) since Mon 2023-12-18 19:41:47 EST; 7ms ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 24939 ExecStart=/usr/sbin/mysqld $MYSQLD_OPTS (code=exited, status=1/FAILURE)
Process: 24909 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 24939 (code=exited, status=1/FAILURE)
Status: “Server shutdown complete”
Dec 18 19:41:45 ssd2.legardeur.net systemd[1]: Starting MySQL Server…
Dec 18 19:41:45 ssd2.legardeur.net mysqld_pre_systemd[24909]: /usr/sbin/restorecon set context /home/mysql->unconfined_u:object_r:user_home_dir_t:s0 failed:’Read-only file system’
Dec 18 19:41:47 ssd2.legardeur.net systemd[1]: mysqld.service: main process exited, code=exited, status=1/FAILURE
Dec 18 19:41:47 ssd2.legardeur.net systemd[1]: Failed to start MySQL Server.
Dec 18 19:41:47 ssd2.legardeur.net systemd[1]: Unit mysqld.service entered failed state.
Dec 18 19:41:47 ssd2.legardeur.net systemd[1]: mysqld.service failed.
I tried Chris solution, but I don’t have any /etc/apparmor.d folder on this centos.
Any suggestion?