I moved the data directory of MySQL to a dedicated disk in my home labo (Virtual Machine running Ubuntu Linux 14.04 LTS) by using the following procedure:
Stop the MySQL daemon by initiating the command: sudo /etc/init.d/mysqld restart.
Change the ownership of the target folder (on the dedicated disk): sudo chown -R mysql /disk/folder. See remark below!
Change the group ownership of the target folder: sudo chgrp -R mysql /disk/folder.
Move the data from the old- to the new directory: sudo cp -Rp /var/lib/mysql/* /disk/folder/.
Change the MySQL configuration in the my.cnf file: sudo nano /etc/mysql/my.cnf. Change the parameter datadir to /disk/folder.
Start the MySQL dameon by executing: sudo /etc/init.d/mysql start.
Afterwards when I tried to list the databases in MySQL I was confronted with the following error:
SHOW databases;
ERROR 1018 (HY000): Can’t read dir of ‘.’ (errno: 13)
I spend some time in investigating and I came accoss some reference towards AppArmor.
AppArmor’s security model is to bind access control attributes to programs rather than to users. AppArmor differs from some other MAC systems on Linux: it is path-based, it allows mixing of enforcement and complain mode profiles, it uses include files to ease development, and it has a far lower barrier to entry than other popular MAC systems.
So I changed the values in the configuration so the databases can be trusted and the MySQL databases come back online. To do this, I deleted the “/var/lib/mysqld” lines in the /etc/apparmor.d/usr.sbin.mysqld file and added the following lines instead:
/disk/folder r,
/disk/folder/** rwk,
Remark: when I restarted the machine, I noticed the MySQL daemon was unable to start. In the errorlog (my location is: /var/log/mysql/error.log) I found the following errors:
140707 23:03:49 [ERROR] /usr/sbin/mysqld: Can’t find file: ‘./mysql/host.frm’ (errno: 13)
140707 23:03:49 [ERROR] Fatal error: Can’t open and lock privilege tables: Can’t find file: ‘./mysql/host.frm’ (errno: 13).
This type of error is clearly related to security settings on the folder. Eventually I executed the correction of the folder permissions for a second time by running the following command: sudo chown -R mysql:mysql /disk/folder. When restarting the MySQL daemon, the service runs again without any troubles and all databases are readable.
转载随意~:陶醉 » Ubuntu Mysql data目录迁移