Replication in MySQL

MySQL replication is used to synchronize a database which is chosen as master into the slave machines. Example Setup:

master : aaa.aaa.aaa.aaa 
slave1 : bbb.bbb.bbb.bbb 
slave2 : ccc.ccc.ccc.ccc 

We modify the my.cnf file in master node such as:

server-id = 1 
log-bin

For the changes to take affect, mysql should be restarted.

mysql> GRANT REPLICATION SLAVE ON *.* to [email protected]'bbb.bbb.bbb.bbb' identified by 'slave_password' 
mysql> GRANT RELOAD ON *.* to [email protected]'bbb.bbb.bbb.bbb' identified by 'slave_password'; 
mysql> GRANT SUPER,REPLICATION CLIENT ON *.* to [email protected]'bbb.bbb.bbb.bbb' identified by 'slave_password';
mysql> GRANT REPLICATION SLAVE ON *.* to [email protected]'ccc.ccc.ccc.ccc' identified by 'slave_password' 
mysql> GRANT RELOAD ON *.* to [email protected]'ccc.ccc.ccc.ccc' identified by 'slave_password'; 
mysql> GRANT SUPER,REPLICATION CLIENT ON *.* to [email protected]'ccc.ccc.ccc.ccc' identified by 'slave_password';

We modify the my.cnf file in slave1 machine such as follows (With replicate-do-table we specify which tables to replicate):

server-id = 2 
master-host =aaa.aaa.aaa.aaa 
master-user = slave_user 
master-password=slave_password 
master-port = 3306 
replicate-do-table=db.TABLE1 
replicate-do-table=db.TABLE2 
replicate-do-table=db.TABLE3 
replicate-do-table=db.TABLE4

We make the same changes in the my.cnf file on slave2

server-id = 3 
master-host =aaa.aaa.aaa.aaa 
master-user = slave_user 
master-password=slave_password 
master-port = 3306 
replicate-do-table=db.TABLE1 
replicate-do-table=db.TABLE2 
replicate-do-table=db.TABLE3 
replicate-do-table=db.TABLE4

For the changes to take effect, we restart mysql processes on both slaves. On both slaves,

mysql> load data from master 
mysql> slave start

You can check slave nodes and master node whether they are working synchronized as below respectively. On slave:

mysql> show slave status\G; 

************************** 1. row ************************** 
Slave_IO_State: Waiting for master to send event 
Master_Host: aaa.aaa.aaa.aaa 
Master_User: slave_user 
Master_Port: 3306 
Connect_Retry: 60 
Master_Log_File: bin-log.000001 
Read_Master_Log_Pos: 160158 
Relay_Log_File: smlcpurgw1-relay-bin.000001 
Relay_Log_Pos: 59186 
Relay_Master_Log_File: bin-log.000001 
Slave_IO_Running: Yes 
Slave_SQL_Running: Yes 
Replicate_Do_DB: 
Replicate_Ignore_DB: 
Replicate_Do_Table: db.TABLE1,db.TABLE2,db.TABLE3,db.TABLE4 
Replicate_Ignore_Table: 
Replicate_Wild_Do_Table: 
Replicate_Wild_Ignore_Table: 
Last_Errno: 0 
Last_Error: 
Skip_Counter: 0 
Exec_Master_Log_Pos: 160158 
Relay_Log_Space: 59186 
Until_Condition: None 
Until_Log_File: 
Until_Log_Pos: 0 
Master_SSL_Allowed: No 
Master_SSL_CA_File: 
Master_SSL_CA_Path: 
Master_SSL_Cert: 
Master_SSL_Cipher: 
Master_SSL_Key: 
Seconds_Behind_Master: 0 
1 row in set (0.00 sec) 

On master,

mysql> show master status; 
+-----------------------------------+------------------+ 
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | 
+-----------------------------------+------------------+ 
| bin-log.000001 | 160158 | | | 
+-----------------------------------+------------------+ 
1 row in set (0.00 sec)