How to change the MySQL data default directory
Sponsored Link
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.
Thanks. I fussed with this for a couple hours then found this post.
Thanks, top tip for the apparmor! you saved me an evening of fiddling 🙂
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?
It looks like you’re removing the new data dir on the rm command
Really helpful — you save me much time and frustration. Thanks!!
er… *saved*
Thanks a lot, dude !
hello guys I need do that in a linux debian, so the commands are the same?
Thanks in advance
Leandro
This worked on Ubuntu 12.04 but I also needed to run this command:
mysql_install_db –user=mysql –ldata=/path/to/new/datadir
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 ?
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
yeah also no go here on 12.04 (mysql would not restart) quickly undo all changes and hope my desktop will remain…
Thanks heaps very useful. Works as expected tested on Ubuntu 14.04 (Trusty)
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