The Schworak Site | Log In | Up One Level

Move MySQL binlog to another disk or folder

This one tripped me up so I hope this helps you. Moving the binlog, relaylog or even the data itself to another drive or folder is really easy as long as you know the secret.

First thing, shut down MySQL so you can copy the files and folders and make changes to the config.

sudo service mysql stop

Next copy the existing data to the new location. In my case, I keep data in one subfolder, binlogs in another and replication logs in yet another. This keeps my data area clean and easy to work with. It really helped when I made this move.

sudo cp -r -p /var/lib/mysql/binlog /new-folder/binlog

Now you need to modify the my.cnf file to tell it you moved the binlog (or other folders)

sudo vi /etc/mysql/my.cnf

Find the entry for "log-bin" to relocate the binlog files, "relay-log" for the relay log file location and of course "datadir" for the main data folder location. We will use the "log-bin" entry for this example where the "bin" at the end is the prefix that is added to each binlog file that is created. You may have a different prefix or none at all.

Changed from: log-bin = /var/lib/mysql/binlog/bin
Changed to:     log-bin = /new-folder/binlog/bin

Now you might think you are done because the permissions are properly set on the folders becaus we used the "-p" option when making the copy. But you would likely be mistaken. If you restart the server and get a "file not found" error even though the paths are correct, mysql simply can't reach the file.

This is what got me stuck for a whild. The permissions were correct and everything looked fine but mysql still couldn't access the file. Turns out that AppArmor needs to be updated. It is sitting in the background being an extra protector to keep mysql out of places it shouldn't go.

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

Find the lines (usually 2) that point to the old file location and copy them so the new file location is listed with the same properties.

/new-folder/ r,
/new-folder/** rwk,

Now we are ready to restart things. First we need to reload the AppArmor settings then start MySQL.

sudo /etc/init.d/apparmor reload
sudo service mysql start

The server should spring to life. If you get errors check the error log. If you don't see an error log then you need to specify the log file in the my.cnf file. It may already be there but could be commented out. You want this log.

Comments

<< MySQL and Bidirectional Replication (aka Master-Master)    |    Automatically boot when a RAID is degraded >>
All content on this site is copyright ©2004-2024 and is not to be reproduced without prior permission.