How to change the MySQL data default directory

Sponsored Link
MySQL is a widely used and fast SQL database server. It is a client/server implementation that consists of a server daemon (mysqld) and many different client programs/libraries.

If you want to install Mysql database server in Ubuntu check this tutorial.

What is Mysql Data Directory?

Mysql data directory is important part where all the mysql databases storage location.By default MySQL data default directory located in /var/lib/mysql.If you are running out of space in /var partition you need to move this to some other location.

Note:- This is only for advanced users and before moving default directory make a backup of your mysal databases.

Procedure to follow

Open the terminal

First you need to Stop MySQL using the following command

sudo /etc/init.d/mysql stop

Now Copy the existing data directory (default located in /var/lib/mysql) using the following command

sudo cp -R -p /var/lib/mysql /path/to/new/datadir

All you need are the data files, so delete the others with the command

sudo rm /path/to/new/datadir

Note:- You will get a message about not being able to delete some directories, but that's what you want.

Now edit the MySQL configuration file with the following command

gksu gedit /etc/mysql/my.cnf

Look for the entry for "datadir", and change the path (which should be "/var/lib/mysql") to the new data directory.

Important Note:-From Ubuntu 7.10 (Gutsy Gibbon) forward, Ubuntu 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.

In the terminal, enter the command

sudo gedit /etc/apparmor.d/usr.sbin.mysqld

Copy the lines beginning with "/var/lib/mysql", comment out the originals with hash marks ("#"), and paste the lines below the originals.

Now change "/var/lib/mysql" in the two new lines with "/path/to/new/datadir". Save and close the file.

Restart the AppArmor profiles with the command

sudo /etc/init.d/apparmor reload

Restart MySQL with the command

sudo /etc/init.d/mysql restart

Now MySQL should start with no errors, and your data will be stored in the new data directory location.

Sponsored Link

You may also like...

44 Responses

  1. Craig Hollabaugh says:

    Thanks. I fussed with this for a couple hours then found this post.

  2. Krikke says:

    Thanks, top tip for the apparmor! you saved me an evening of fiddling 🙂

  3. hotel says:

    I’m running ubuntu 12.10 with mysql installed (and working) but I do not have a usr.sbin.mysqld inside the directory etc/apparmor.d

    I’m trying to change the data location as described, but mysql fails to restart. I could not locate a file named usr/sbin/mysqld anywhere on the host. Thoughts?

  4. Mauricio says:

    It looks like you’re removing the new data dir on the rm command

  5. David says:

    Really helpful — you save me much time and frustration. Thanks!!

  6. David says:

    er… *saved*

  7. Zorro says:

    Thanks a lot, dude !

  8. leandro says:

    hello guys I need do that in a linux debian, so the commands are the same?

    Thanks in advance

    Leandro

  9. Bjorn says:

    This worked on Ubuntu 12.04 but I also needed to run this command:

    mysql_install_db –user=mysql –ldata=/path/to/new/datadir

  10. hamiljf says:

    I’m pretty sure I did this right, running 13.04, and it did not work: mysql would not restart and system would not reboot to desktop. Using root console login, backed out changes to my.cnf and usr.sbin.mysqld and the system rebooted successfully. error logs showed the server found tables to be read only, but the files were ok: correct owner (mysql) and permissions (rw). Could try again with Bjorn’s additional command: to be executed at what point during the process ?

  11. Peter says:

    Thank you, very useful, but be aware running this over an ssh-link (as I did right now) it will mess up the ownership of files and folders.

    CU Peter
    ____________
    DEBIAN Squeeze, GA-P35C, 4GB, 1TB net SW-Raid1

  12. erik says:

    yeah also no go here on 12.04 (mysql would not restart) quickly undo all changes and hope my desktop will remain…

  13. Xcaliburs says:

    Thanks heaps very useful. Works as expected tested on Ubuntu 14.04 (Trusty)

  14. mohammad says:

    many thanks for this post, but i need change mysql data dir with using xampp on ubuntu 15.10
    i not found the usr.sbin.mysqld

Leave a Reply

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