Move Mariadb Data 2
From: https://askubuntu.com/questions/137424/how-do-i-move-the-mysql-data-directory
Stop the mysql server:
stop mysql
Create the new directory:
mkdir /array2/mysql
Copy over ONLY the database folders:
cp -R /var/lib/mysql /array2/mysql
cp -R /var/lib/mysql/users /array2/mysql
Backup the my.cnf file:
cp /etc/mysql/my.cnf /root/my.cnf.backup
Edit the my.cnf file:
nano /etc/mysql/my.cnf
Change all mentions of the old datadir and socket to your new location
Mine became:
datadir=/array2/mysql
socket=/array2/mysql/mysql.sock
Update the directory permissions:
chown -R mysql:mysql /array2/mysql
Rename the old directory:
mv /var/lib/mysql /var/lib/mysql-old
Create a symlink, just in case:
ln -s /array2/mysql /var/lib/mysql
Let AppArmor know about the new datadir:
echo "alias /var/lib/mysql/ -> /your/new/datadir/," >> /etc/apparmor.d/tunables/alias
Reload the apparmor profiles
sudo /etc/init.d/apparmor reload
Then start mysql:
start mysql
9 revs, 9 users 62% Jonny Flowers
This is a good answer but you may need one more step to allow clients to
connect to mysql. Add a new group with one line under it... [clients]
socket=/array2/mysql/mysql.sock where the socket= portion points to the new
location of your mysql.sock file. –
Night Owl Jan 30, 2013 at 5:20
2
If you don't want to be bothered to merge /etc/apparmor.d/usr.sbin.mysqld
each time mysql receives an update, you can add /array2/mysql/** rwk, to
/etc/apparmor.d/local/usr.sbin.mysqld instead. Mysql will still have access
to the old data dir, but that probably isn't an issue. –
drevicko
Jul 26, 2013 at 3:46
sed -i "s,datadir.*=.*,datadir=$new_dir,g" /etc/mysql/my.cnf. Why only the database folders? –
int_ua
Apr 26, 2014 at 16:36
For anyone moving their datadir to ZFS (like I did), make sure you also add:
innodb_use_native_aio=0 in the [mysqld] section of your my.cnf file, since
ZFS on linux does not support AIO. dev.mysql.com/doc/refman/5.5/en/… –
Andrew Ensley
Oct 3, 2014 at 20:04
With Apparmor
I found that AppArmor was the culprit by examining the syslog, and was able
to successfully change the mysql data location by following this process.
Please note that, in files edited below, lines starting with + were added,
and lines starting with - were removed. You should not actually type/paste
the + signs when adding lines to these files.
I cloned the mysql directory to the new location:
sudo rsync -av /var/lib/mysql /new_dir
Then I edited the datadir line in /etc/mysql/my.cnf:
sudo vi /etc/mysql/my.cnf
-datadir = /var/lib/mysql
+datadir = /new_dir/mysql
Then I edited /etc/apparmor.d/usr.sbin.mysqld:
sudo vi /etc/apparmor.d/usr.sbin.mysqld
- /var/lib/mysql/ r,
- /var/lib/mysql/** rwk,
+ /new_dir/mysql/ r,
+ /new_dir/mysql/** rwk,
Then I restarted mysql.
Be aware
If you have InnoDB tables you MUST copy over the ibdata* and ib_logfile*
files or you will not be able to use the the tables. You will get:
'Table 'databaseName.tableName' doesn't exist' errors.
Run this copy command to copy over the ibdata* and ib_logfile* files.
sudo cp -p /var/lib/mysql/ib* /array2/mysql/