Dual Master Mysql Replication ( Circular Replication )


We have 2 servers that will be configured as Circular Replication.
It means both servers act as master and slave to another server.

After we install mysql-server on both servers, check /etc/hosts should be same as both servers.
# more /etc/hosts
127.0.0.1 localhost
192.168.0.21 servera
192.168.0.22 serverb
192.168.0.23 dbhost

After that try to look at /etc/my.cnf
At Server A :
# more /etc/my.cnf
[MYSQLD]
basedir = /usr/local/mysql
datadir = /data/mysql/data
max_connections=10000
default-character-set=utf8
default-collation=utf8_bin

# InnoDB parameters
set-variable = innodb_buffer_pool_size=1500M
set-variable = innodb_additional_mem_pool_size=500M
set-variable = innodb_log_buffer_size=10M

log-bin
binlog-do-db=test
binlog-ignore-db=mysql

server-id=1
replicate-same-server-id = 0
auto-increment_increment = 2
auto-increment_offset = 1
master-host = serverb
master-user = replica
master-password = slave123
master-port = 3306

expire_logs_days = 10
max_binlog_size = 500M

[mysql.server]
user=mysql
basedir=/usr/local/mysql

[mysqld_safe]
err-log=/usr/local/mysql/log/mysqld.log
pid-file=/usr/local/mysql/mysqld.pid

[mysql]
default-character-set=utf8

At Server B :
# more /etc/my.cnf
[MYSQLD]
basedir = /usr/local/mysql
datadir = /data/mysql/data
max_connections=10000
default-character-set=utf8
default-collation=utf8_bin

# InnoDB parameters
set-variable = innodb_buffer_pool_size=1500M
set-variable = innodb_additional_mem_pool_size=500M
set-variable = innodb_log_buffer_size=10M

log-bin=serverb-bin
binlog-do-db=test
binlog-ignore-db=mysql

server-id=2
replicate-same-server-id = 0
auto-increment_increment = 2
auto-increment_offset = 2
master-host = servera
master-user = replica
master-password = slave123
master-port = 3306

expire_logs_days = 10
max_binlog_size = 500M

[mysql.server]
user=mysql
basedir=/usr/local/mysql

[mysqld_safe]
err-log=/usr/local/mysql/log/mysqld.log
pid-file=/usr/local/mysql/mysqld.pid

[mysql]
default-character-set=utf8

If you want to add a database ( ex: dbex )that will be replicate, add this line at MYSQLD section.
-------------
binlog-do-db=dbex
-------------

start your sql server on both servers:
# /etc/init.d/mysql.server start

login to sql and add grant to user "replica" on both servers.
# mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.0.33-log Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'replica'@'%' IDENTIFIED BY 'slave123';

restart your sql server on both servers:
# /etc/init.d/mysql.server restart

setting slave on server A :
- on server A, stop and reset slave first
mysql> stop slave;
mysql> reset slave;
- on server B, show master log file and position, before this lock the tables first.
mysql> flush tables with read lock;
mysql> SHOW MASTER STATUS \G;
*************************** 1. row ***************************
File: serverb-bin.000001
Position: 98
Binlog_Do_DB: test
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)

ERROR:
No query specified

- on server A, set this master value, and then you can start slave.
mysql> CHANGE MASTER TO MASTER_LOG_FILE='serverb-bin.000001', MASTER_LOG_POS=98;
mysql> start slave;

- on server B, releasing read lock with
mysql> UNLOCK TABLES;

setting slave on server B :
- on server B, stop and reset slave first
mysql> stop slave;
mysql> reset slave;
- on server A, show master log file and position, before this lock the tables first.
mysql> flush tables with read lock;
mysql> SHOW MASTER STATUS \G;
*************************** 1. row ***************************
File: servera-bin.000001
Position: 98
Binlog_Do_DB: test
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)

ERROR:
No query specified

- on server B, set this master value, and then you can start slave.
mysql> CHANGE MASTER TO MASTER_LOG_FILE='servera-bin.000001', MASTER_LOG_POS=98;
mysql> start slave;

- on server A, releasing read lock with
mysql> UNLOCK TABLES;


Successful replication can be check with :

mysql> SHOW SLAVE STATUS \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: servera
Master_User: replica
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: servera-bin.000001
Read_Master_Log_Pos: 98
Relay_Log_File: serverb-relay-bin.000001
Relay_Log_Pos: 237
Relay_Master_Log_File: servera-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 98
Relay_Log_Space: 237
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)

ERROR:
No query specified

Popular posts from this blog

Howto configure boot device order on ILOM

SAN Switch Config Command

Howto cstm on HP-UX