Moving MySQL InnoDB database to separate drive

一曲冷凌霜 提交于 2019-12-03 00:49:45
Morgan Tocker

I'm not sure your solution is the best idea. See my post here:

http://www.mysqlperformanceblog.com/2010/12/25/spreading-ibd-files-across-multiple-disks-the-optimization-that-isnt/

There is also this other thread here:

Innodb; multiple data directories

Turns out this works but my old enemy appArmor blocked MySQL from reading the moved directory.

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

add lines:

/new-db-path/ r,
/new-db-path/** rwk,

Thanks for helping out!

Norling Jr. saved my day with the AppArmor tip, but since I had some trouble configuring it, I'm writing a more detailed answer. I'm using Ubuntu 12.04.

Start becoming root to save the need to type all that sudos:

sudo su -

Following MySQL docs, you first move your already created database dir to another path:

mv /var/lib/mysql/yourdatabase /new/path/

Here was my first trap. Check if the mysql user has access to this new path:

sudo -u mysql ls /new/path/yourdatabase

If you got access denied, you should probably give execute permission to every parent dir:

chmod a+x  /new  /new/path/

Test to access the file again. If it still doesn't work, try asking a question in Stack Overflow:-)

Link the new dir location and give it the correct permissions:

 ln -s /new/path/yourdatabase /var/lib/mysql/
 chown mysql:mysql /var/lib/mysql/yourdatabase

Let's edit AppArmor local configuration file. You shoudn't change /etc/apparmor.d/usr.sbin.mysqld file. Edit the local conf so you won't loose it after system updates:

emacs /etc/apparmor.d/local/usr.sbin.mysqld

add Norling Jr. configurations:

/new/path/yourdatabase/ r,
/new/path/yourdatabase/** rwk,

Don't miss the last comma. Save the file and reload AppArmor configuration:

apparmor_parser -r /etc/apparmor.d/usr.sbin.mysqld

This will not just reload AppArmor MySql configuration, but also test it if there isn't any syntax error (a very important thing). If you don't run the parser, the new conf won't be applied.

Finally just open mysql client and type SHOW DATABASES. If your database appears, everything is probably fine. Type 'USE yourdatabase' for another check.

A more robust test would also reload the mysql service: 'service mysql restart' and try to access your database.

Now I'll remember next time I need to do it. Google and SO together are the best notebook in the world :-)

It should be possible to use local mounts (bindings) with appropriate permissions and mount oprions (including the selinux or apparmor contexts):

/dev/sdc on /var/lib/mysql/my-db/
/dev/sdd on /var/lib/mysql/her-db/
fs.example.com:/path/to/wherever on /var/lib/mysql/my-other-db/

Though I haven't tested this solution so use at your own risk.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!