Home >Database >Mysql Tutorial >How to Safely Relocate Your MySQL Data Directory?

How to Safely Relocate Your MySQL Data Directory?

Susan Sarandon
Susan SarandonOriginal
2024-12-12 20:34:16908browse

How to Safely Relocate Your MySQL Data Directory?

How to Relocate MySQL Data Directory

Is it possible to transfer my MySQL data directory to a different location? Will I still have access to my databases after the move?

Answer:

Follow these steps to change the MySQL data directory:

  1. Shut down MySQL:

    sudo /etc/init.d/mysql stop
  2. Copy the current data directory:

    sudo cp -R -p /var/lib/mysql /newpath
  3. Edit the MySQL configuration file:

    sudo gedit /etc/mysql/my.cnf 
  4. Locate the "datadir" entry and modify its path to the new data directory.
  5. Edit the AppArmor profile:

    sudo gedit /etc/apparmor.d/usr.sbin.mysqld
  6. Replace occurrences of "/var/lib/mysql" with the new path.
  7. Save and close the file.
  8. Restart AppArmor:

    sudo /etc/init.d/apparmor reload
  9. Restart MySQL:

    sudo /etc/init.d/mysql restart
  10. Log in to MySQL and confirm that you can access your previous databases.

The above is the detailed content of How to Safely Relocate Your MySQL Data Directory?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn