preload preload preload preload

moving mysql data directory

In some cases you have to change the default MySql data directory, for doing this you have to follow some steps. Here I will explain each steps one by one.

  1. Stop MySql server using following command.
  2. $ sudo /etc/init.d/mysql stop
  3. Copy the existing data directory (which is located in /var/lib/mysql) using following command.
  4. $ sudo cp -R -p /var/lib/mysql /path/to/new/datadir
  5. Edit the MySQL configuration file with your favorite text editor.
  6. $ sudo vim /etc/mysql/my.cnf
  7. Find the entry for datadir, change the path to the new data directory.
  8. Some Linux distributions uses some security software called AppArmor that specifies the areas of your filesystem applications are allowed to access. Unless you modify the AppArmor profile for MySQL, you’ll never be able to restart MySQL with the new datadir location. So edit the file /etc/apparmor.d/usr.sbin.mysqld using your favorite editor.
  9. $ sudo vim /etc/apparmor.d/usr.sbin.mysqld
  10. Duplicate the lines beginning with /var/lib/mysql and replace duplicated strings with /new_datadir.
  11. /var/lib/mysql/ r,
    /var/lib/mysql/** rwk,
    /var/log/mysql/ r,
    /var/log/mysql/* rw,
    #
    /path/to/new/datadir r,
    /path/to/new/datadir** rwk,
    /path/to/new/datadir r,
    /path/to/new/datadir* rw,
  12. Restart the AppArmor profiles with the following command.
  13. $ sudo /etc/init.d/apparmor restart
  14. Restart the MySql server with the following command.
  15. $ sudo /etc/init.d/mysql start

MySQL should now start without any errors and your data will be stored in the new location.

  • Share/Bookmark
  • One response to "moving mysql data directory"

  • christian louboutin
    7:58 on June 7th, 2010

    I am learning the Mysql right now,and it’s useful to me,thank you for your post.

  • Leave a Reply

    * Required
    ** Your Email is never shared