Home >Database >Mysql Tutorial >How Do I Safely Relocate My MySQL Data Directory?
Relocating MySQL Data Directory
Changing the default data directory for MySQL can be necessary in scenarios requiring additional storage space or organizational preferences. By following a series of well-defined steps, it's possible to relocate the data directory without compromising database accessibility.
To initiate the process, MySQL must be stopped using the command sudo /etc/init.d/mysql stop. Subsequently, the existing data directory, typically located at /var/lib/mysql, is copied to a new path using the command sudo cp -R -p /var/lib/mysql /newpath.
Next, the MySQL configuration file /etc/mysql/my.cnf (or /etc/mysql/mysql.conf.d/mysqld.cnf) is edited to change the datadir entry to the new directory path.
To update the AppArmor profiles, the command sudo gedit /etc/apparmor.d/usr.sbin.mysqld is executed, and any lines commencing with /var/lib/mysql are modified to reflect the new path. The file is saved and closed before restarting the AppArmor profiles with sudo /etc/init.d/apparmor reload.
Finally, MySQL is restarted with sudo /etc/init.d/mysql restart, allowing access to the same databases as before, now located in the relocated data directory.
The above is the detailed content of How Do I Safely Relocate My MySQL Data Directory?. For more information, please follow other related articles on the PHP Chinese website!