MySQL replication – How-to

MySQL replication is quite straight forward. Here are the setup steps:

1. Create a user for replication at master.

1mysql> GRANT REPLICATION SLAVE ON *.* TO 'repluser'@'%' identified by '';

2. Copy master data directory to slave.

For Innodb, use mysqldump with option –single-transaction to get a consistent dump (you can get the log file and position in the dumpfile), or shutdown the source DB to get a cold consistent copy.

For all other DB engine, following these steps:
1) Lock the tables at master

01mysql> FLUSH TABLES WITH READ LOCK;
02Query OK, 0 rows affected (0.00 sec)
03 
04mysql> show master status;
05+----------------+----------+--------------+------------------+
06| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
07+----------------+----------+--------------+------------------+
08| bin-log.000074 | 915 | | |
09+----------------+----------+--------------+------------------+
101 row in set (0.00 sec)

–Copy the log file name and position number, it will be used later

2) Copy the data over

1$ rsync -avz /mysql/data :/mysql/data

Remove file master.info and relay-log.info if you build the slave from Master.

3) After the copy is done, unlock tables at Master:

1mysql> unlock tables;
2Query OK, 0 rows affected (0.00 sec)

or you can exist the session which will release the lock as well.

3. Setup slave

If slave binary is not setup, you can also copy them from master.

1) Startup mysql
a. Remember change the server_id to a different value in my.cnf
b. Binary logging is required for replication. And relay-log is recommended.

1log-bin=mysql-bin
2relay-log = relay-log
3relay-log-index = relay-log.index

2) Set master

1mysql> change master to
2-> master_host='blogs.silicontechnix.com',
3-> master_user='repluser',
4-> master_password='',
5-> master_port = 3306,
6-> master_log_file='bin-log.000074',
7-> master_log_pos=915;
8Query OK, 0 rows affected (0.00 sec)
9--use the log file name and postion we got previously
1mysql> start slave;
2Query OK, 0 rows affected (0.00 sec)
01mysql> show slave status\G;
02*************************** 1. row ***************************
03Slave_IO_State: Waiting for master to send event
04Master_Host: blogs.silicontechnix.com
05Master_User: repluser
06Master_Port: 3306
07Connect_Retry: 60
08Master_Log_File: bin-log.000074
09Read_Master_Log_Pos: 1199
10Relay_Log_File: relay-log.000003
11Relay_Log_Pos: 533
12Relay_Master_Log_File: bin-log.000074
13Slave_IO_Running: Yes
14Slave_SQL_Running: Yes
15Replicate_Do_DB:
16Replicate_Ignore_DB:
17Replicate_Do_Table:
18Replicate_Ignore_Table:
19Replicate_Wild_Do_Table:
20Replicate_Wild_Ignore_Table:
21Last_Errno: 0
22Last_Error:
23Skip_Counter: 0
24Exec_Master_Log_Pos: 1199
25Relay_Log_Space: 682
26Until_Condition: None
27Until_Log_File:
28Until_Log_Pos: 0
29Master_SSL_Allowed: No
30Master_SSL_CA_File:
31Master_SSL_CA_Path:
32Master_SSL_Cert:
33Master_SSL_Cipher:
34Master_SSL_Key:
35Seconds_Behind_Master: 0
36Master_SSL_Verify_Server_Cert: No
37Last_IO_Errno: 0
38Last_IO_Error:
39Last_SQL_Errno: 0
40Last_SQL_Error:
411 row in set (0.00 sec)

Now the Master-Slave replication is setup, is it easy 🙂

In addition,

A. If you want to setup Master-Master replication, continue the following steps:
At slave :

1mysql> show master status;
2+----------------+----------+--------------+------------------+
3| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
4+----------------+----------+--------------+------------------+
5| bin-log.000077 | 106 | | |
6+----------------+----------+--------------+------------------+
71 row in set (0.00 sec)

At master:

1mysql> change master to
2-> master_host='blogs.silicontechnix.com',
3-> master_user='repluser',
4-> master_password='',
5-> master_port = 3306,
6-> master_log_file='bin-log.000077',
7-> master_log_pos=106;
8Query OK, 0 rows affected (0.03 sec)
1mysql> start slave;
2Query OK, 0 rows affected (0.00 sec)

B. If you want to setup a slave from an existing slave, using the following steps:

* Shutdown the existing slave db

* Copy the data to new slave:

1$ rsync -avz /mysql/data <target_host>:/mysql/data

–Keep file master.info and relay-log.info if you build the slave from another slave.

* Startup mysql at new slave.
make sure it uses a different server_id in my.cnf

1(cd $MYSQL_BASE; ./bin/mysqld_safe &)
2start slave;

–Do NOT need to run “change master to …” statement as it already in master.info and relay-log.info