How to move MySQL data directory in Ubuntu Server

By default, the MySQL data is placed in /var/lib/mysql, which is a reasonable default. However, sometimes you want to place it somewhere else, such as on an other file system. Using a symlink doesn’t seem to work, so you have follow this procedure.

To move the MySQL data directory from /var/lib to /mnt/mydata, run these commands as root:

  1. apt-get install mysql-server
  2. service mysql stop
  3. mv /var/lib/mysql /mnt/mydata/
  4. replace /var/lib/mysql with /mnt/mydata/mysql in
    • /etc/passwd – mysql
    • /etc/mysql/my.cnf – [mysqld] datadir
    • /etc/apparmor.d/usr.sbin.mysqld (twice)
  5. service mysql start
This entry was posted in Uncategorized. Bookmark the permalink.

10 Responses to How to move MySQL data directory in Ubuntu Server

  1. Thanks A tonne. I struggled with this for a whole afternoon only to learn that I was missing the the edit to etc/passwd. Seems to have made the difference.

  2. Mike B says:

    can you show me an example how to make this change:

    /etc/passwd – mysql

  3. Synaptech says:

    Don’t forget to change the folder permissions to 700 and owner/group to mysql/mysql for this to work properly.

  4. Pingback: Moving /var/lib to a different directory | Technology & Programming

  5. alextsil says:

    Make sure you restart apparmor after editing the file, othewise mysql won’t be able to access the new dir

  6. sk says:

    I tried to restart apparmor then didnt workI tried to restart apparmor then didnt work

  7. Pingback: mengganti defaul datadir pada mysql ubuntu server 14.04 | manual dan tutorial

  8. Rosel says:

    Great article. Is it require to check mysql port before restarting the services.

  9. Great article. Very well explained.

  10. Jan says:

    I also needed to reload apparmor
    sudo service apparmor reload

Leave a Reply

Your email address will not be published. Required fields are marked *

(this is a captcha)