Cloning MySQL slave to another server

Many times we need to clone existing database for various purposes. Sometimes it should be live replicated copy. When there is existing slave that we can shutdown, it is pretty easy. Just follow these instructions.

One of most common mistakes is that we forget to set permissions on the copied MySQL files on the destination server. This is really hard to understand from MySQL logs that server doesn’t start because mysql user isn’t owner of the data directory.

But if every single thing in the above manual is done but you still see these errors in the new slave MySQL log:

101220 16:22:48 [ERROR] Failed to open the relay log ‘./old_server-relay-bin.000292’ (relay_log_pos 1038154170)
101220 16:22:48 [ERROR] Could not find target log during relay log initialization
101220 16:22:48 [ERROR] Failed to initialize the master info structure

and replication doesn’t start, most likely, you are changing my.cnf params WHILE THE NEW SLAVE IS RUNNING. Please note, if you are trying to change my.cnf parameters according to the doc while MySQL is running and then do “/etc/init.d/mysql restart”, it will not work.

Here is quick procedure check:

  1. Change data directory ownership. “chown -R mysql:mysql /var/lib/mysql”
  2. Stop the new slave. Do “slave stop” in the mysql shell and shutdown MySQL with “/etc/init.d/mysql stop”
  3. Edit /etc/my.cnf. The following options should be there:

    server_id = [unique integer]
    relay-log=[old_server_name]-relay-bin # old relay file basename
    relay-log-index=[old_server_name]-relay-bin # old relay file basename

  4. Start MySQL and do “start slave”

Validate that replication is working again with “show slave status”.

This is it, everything should be fixed now.

Leave a comment