Home >Database >Mysql Tutorial >How to Safely Change the MySQL Data Directory?

How to Safely Change the MySQL Data Directory?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-16 20:33:11755browse

How to Safely Change the MySQL Data Directory?

Change MySQL Data Directory: Relocating Your Databases

To modify the default MySQL data directory, it is crucial to follow a series of steps to ensure a seamless transition and maintain database accessibility.

  1. Halt MySQL Services:
    Start by stopping MySQL using the command:

    sudo /etc/init.d/mysql stop
  2. Data Directory Replication:
    Duplicate the existing data directory, typically located at /var/lib/mysql, to the new desired location:

    sudo cp -R -p /var/lib/mysql /newpath
  3. MySQL Configuration File Editing:
    Edit the MySQL configuration file, usually /etc/mysql/my.cnf, and modify the 'datadir' entry to reflect the new data directory's path.
  4. Modifying AppArmor Profiles:
    In /etc/apparmor.d/usr.sbin.mysqld, locate lines beginning with /var/lib/mysql and update them with the new path.
  5. Restart AppArmor:
    Reload the AppArmor profiles using the command:

    sudo /etc/init.d/apparmor reload
  6. MySQL Service Restoration:
    Restart MySQL:

    sudo /etc/init.d/mysql restart
  7. MySQL Login Verification:
    Log in to MySQL to confirm access to existing databases.

By completing these steps, you can successfully relocate MySQL's data directory to a new path while maintaining access to your databases.

The above is the detailed content of How to Safely Change the 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