In this guide, we'll walk through the process of changing the MySQL data directory to a new location to free up space on the /var partition. This is particularly useful when the /var partition is running low on space, causing MySQL to fail or lose data. Follow these steps carefully to ensure a smooth transition.
Step 1: Backup the MySQL Databases
First, it's crucial to create a backup of all MySQL databases to ensure data security. Execute the following command to dump all databases into a single file and compress it:
mysqldump --add-drop-table --all-databases | gzip > /home/alldatabases.sql.gz
Step 2: Stop the MySQL Server
Before moving the data directory, stop the MySQL server using the following command:
/etc/init.d/mysql stop
Step 3: Move the Data Directory
Navigate to the current MySQL data directory (/var/lib) and move it to the new location (/home/mysql) using the following commands:
cd /var/lib mv mysql /home/mysql
Step 4: Create Symbolic Link
Create a symbolic link from the original data directory location to the new location to ensure compatibility with MySQL configurations:
ln -s /home/mysql mysql
Step 5: Set Ownership
Ensure that the MySQL user owns the new data directory by executing the following command:
chown -R mysql:mysql /home/mysql
Step 6: Start MySQL Server
Start the MySQL server using the following command:
/etc/init.d/mysql start
Step 7: Verify MySQL Status
Check the status of the MySQL server to ensure it's running without any issues:
ps aux | grep mysql
Conclusion
By following these steps, you've successfully moved the MySQL data directory from the /var partition to a new location (/home/mysql), thereby freeing up space on the /var partition. This ensures smooth operation of MySQL and prevents data loss or service failures due to insufficient disk space. Always remember to back up your databases before making significant changes to ensure data integrity. If you encounter any issues, feel free to seek assistance from a qualified administrator.