Move MySQL Datadir to free up some storage space with ease.
At Bobcares, we offer easy solutions for any query. Find out how our Server Management Service can make your life easier.
Let’s take a look at how our Support Techs helped a customer recently free up some storage space by moving the MySQL directory.
Why should we move MySQL datadir?
Did you run out of disk space? Well, you are not alone. This is a common problem, especially for the primary filesystem or partition on the server.
Fortunately, moving a director will help free up some much-needed storage. For instance, moving /var/lib/mysql to the home directory when the /root or / is low on disk space helps free up space at this location.
Our Support Engineers are here to help you move MySQL directory on a CentOS 7 or CentOS 6 server with cPanel.
How to move MySQL datadir?
- First, we will take a full mysqldump backup of all databases with the following command:
mysqldump -A --opt > /home/mysqldump.sql
- Next, we will disable WHM MySQL monitoring. This will prevent restarts from occurring during the process, which may lead to issues.
whmapi1 configureservice service=mysql enabled=1 monitored=0
- After that, we will stop MySQL with this command:
systemctl stop mysqld
- Then we will change the settings in the /etc/systemd/system/mysqld.service.d/override.conf file as seen below. We can also use the systemctl edit mysqld command to edit the file and add these settings:
[Service] ProtectHome=false ProtectSystem=off
After making the changes, we will reload the systemctl
systemctl daemon-reload
- Next, we will make a new directory for MySQL in the home directory and move it with the following commands:
mkdir /home/var_mysql mv /var/lib/mysql /home/var_mysql chown -R mysql:mysql /home/var_mysql/mysql
- Then, we will edit the /etc/my.cnf file and verify it has the following directives in the [mysqld] section:
socket=/var/lib/mysql/mysql.sock datadir=/home/var_mysql/mysql
If the socket file path exists, we can skip this change.
In some scenarios, the datadir file may also exist. In that case, we will add a comment or change the existing configuration option and add the new datadir value.
Furthermore, this is crucial if we have only one datadir value and one socket in the configuration file.
- After that, we will start MySQL with this command:
systemctl start mysqld
- Then, we will enable MySQL monitoring by executing this command:
whmapi1 configureservice service=mysql enabled=1 monitored=1
Once we are done with the above process, MySQL will run with the new datadir. We can verify this with the following command:
lsof -p `ps -fU mysql|awk '{print $2}' |grep -v PID`
[Looking for a helping hand? We are available 24/7.]
Conclusion
At the end of the day, we learned how to move the MYSQL datadir for additional free space under the guidance of the skilled Support Engineers at Bobcares.
PREVENT YOUR SERVER FROM CRASHING!
Never again lose customers to poor server speed! Let us help you.
Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.
0 Comments