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.

5 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

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

(this is a captcha)