How to change the storage location of a MySQL database?
To change the storage location of the MySQL database, you can follow these steps:
- To stop the MySQL service, open the command prompt or terminal window and enter the following command.
sudo service mysql stop
- Copy the MySQL data directory: By default, MySQL’s data directory is located in /var/lib/mysql. You can copy this directory to a new storage location. For example, if you want to copy the data directory to a new location /home/mysql_data, you can use the following command:
sudo cp -R /var/lib/mysql /home/mysql_data
- Edit the MySQL configuration file by opening the my.cnf file. On Ubuntu, this file is usually located at /etc/mysql/my.cnf. Use a text editor to open this file.
sudo vi /etc/mysql/my.cnf
- Locate the datadir option in the configuration file and change its value to the path of the new data directory. For example, update it to /home/mysql_data.
datadir = /home/mysql_data
- Save and close the configuration file.
- Change file permissions: Make sure the owner and group of the new data directory and its contents match the MySQL user (usually mysql). You can modify permissions using the following command:
sudo chown -R mysql:mysql /home/mysql_data
- Start the MySQL service by inputting the following command.
sudo service mysql start
After completing the above steps, the storage location of the MySQL database will be changed to a new directory. Make sure to back up your data to prevent any unexpected situations.