Fork me on GitHub

Ivan-Site.com

Moving MySQL data and logs on Ubuntu to a new location the hard way

There's lots of information online on how to move MySQL data and logs to a new location but I had to use a combination of several guides in order to get it fully working. This is a reference for me if I ever need to do it again or for anyone else that may find it useful.

Also, if I ever have to do it again, I would definitely choose a much simpler way of just binding the new directory to the default one in /etc/fstab. Something like:

echo "/new/path/for/mysql/data /var/lib/mysql none bind" | sudo tee -a /etc/fstab

as it eliminates all problems with apparmor and having to manually update binary log index files. This way you don't need to do steps 3 through 6, though you have to move the data and not copy in this case.

Step 1: stop mysql

sudo service mysql stop

Step 2: copy/move the data

sudo cp -rp /var/lib/mysql/ /new/path/for/mysql/data/
sudo cp -rp /var/log/mysql/ /new/path/for/mysql/logs/

Step 3: edit mysql config replacing old paths with new ones

sudo nano /etc/mysql/my.cnf

Step 4: edit apparmor config for mysql replacing old paths with new ones

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

Step 5: edit mysql binary log index file replacing old paths with new ones

sudo nano /new/path/for/mysql/logs/mysql-bin.index

Step 6: restart apparmor to make sure that it picks up the new config

sudo service apparmor restart

Step 7: finally, start mysql and pray that everything works

sudo service mysql start

Posted Sat 08 January 2011 by Ivan Dyedov in Linux (MySQL, Ubuntu, Linux)