Move MySQL Databases to Another Partition or Drive
Back to current version Restore this version

Table of Contents

Directions#

The following should apply to any recent Debian or Ubuntu distribution (7.10+).

First, stop MySQL Service

root@box:~/# /etc/init.d/mysql stop
* Stopping MySQL database server mysqld [ OK ]

Then go to your current mysql data directory, by default in Debian / Ubuntu it should be /var/lib/mysql. Check that your databases are there (in this example I have 2 bases - the default ‘mysql’ base and a user-created ‘wpdb’ base) :

root@box:~/# cd /var/lib/mysql
root@box:~/# ls
total 21M
-rw-rw---- 1 mysql 10M 2008-05-01 14:39 ibdata1
-rw-rw---- 1 mysql 5.0M 2008-05-01 14:39 ib_logfile0
-rw-rw---- 1 mysql 5.0M 2008-04-27 20:57 ib_logfile1
drwxr-xr-x 2 mysql 4.0K 2008-04-27 20:57 mysql
-rw------- 1 root 6 2008-04-27 20:57 mysql_upgrade_info
drwx------ 2 mysql 4.0K 2008-04-28 19:28 wpdb

Create a new directory for your data (in this example, the /var/www directory which is located on another partition) and give ownership on it to the mysql user :

root@box:~/# mkdir /var/www/mysql_datadir
root@box:~/# chown -R mysql:mysql /var/www/mysql_datadir

Copy your databases to the new dir and update ownership if needed. Only move the databases dirs, don’t touch the other files.

root@box:~/# cp -r mysql /var/www/mysql_datadir/
root@box:~/# cp -r wpdb /var/www/mysql_datadir/
root@box:~/# chown -R mysql:mysql /var/www/mysql_datadir/*

Then update your my.conf file to make it point to the new dir :

root@box:~/# nano /etc/mysql/my.conf

Find the following statement :

datadir = /var/lib/mysql

and update with the new location :

datadir = /var/www/mysql_datadir

check to see if you have this file: /etc/apparmor.d/usr.sbin.mysql

If you have this file you must also update this file to reflect your new location. You should find several lines that had the old location in this file, change them to point to your new location. I also found that I needed to reboot after this to have them take effect.

NOTE: Dealing with your pid file and port file. If you modify these in your my.conf you will also need to look at debian.cnf and also update again the /etc/apparmor.d/usr.sbin.mysql file for this.

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

add the new location:

/var/www/mysql_datadir/ r,
/var/www/mysql_datadir/** rwk,

restart apparmor:

/etc/init.d/apparmor restart

And finally restart the mysql service

root@box:~/# /etc/init.d/mysql start
* Starting MySQL database server mysqld [ OK ]

When restarting, mysql re-created files ibdata1, ib_logfile0, etc. in the new data dir.

Sources#


Linux - MySQL - Fixme