本篇简单的介绍一下MySql主动-主动模式下的主主复制,虽然这种模式的复制会出现很多问题,最明显的就是自增主键的冲突问题,但是在某些特定的环境下,还是会用到这种复制模式。
假设我们的主从主从复制结构是由4台服务器构成,2台master,2台slave。
master服务器1:192.168.0.100 slave服务器1:192.168.0.101
master服务器2:192.168.0.200 slave服务器2:192.168.0.201
构成的结构图如下:
首先,分别在4台机器上安装MySql,并使用MySql自带的my-medium.cnf作为初始配置文件,在4台服务器里分别执行如下命令。
[root@m/s/m/s ~]# yum -y install mysql mysql-server
[root@m/s/m/s ~]# \cp -f /usr/share/doc/mysql-server-*/my-medium.cnf /etc/my.cnf
第一步,配置2台主(Master)服务器配置文件,此步分别在2台主(Master)里操作。
1.在192.168.0.100的/etc/my.cnf的[mysqld]域后里添加
server_id = 100
log_bin = mysql-bin
log_bin_index = mysql-bin.index
log_slave_updates = 1
relay_log = mysql-relay-bin
relay_log_index = mysql-relay-bin.index
sync_binlog = 1
max_binlog_size = 200M
auto_increment_increment = 2
auto_increment_offset = 1
slave-skip-errors = 1062,1053
2.在192.168.0.200的/etc/my.cnf的[mysqld]域后里添加
server_id = 200
log_bin = mysql-bin
log_bin_index = mysql-bin.index
log_slave_updates = 1
relay_log = mysql-relay-bin
relay_log_index = mysql-relay-bin.index
sync_binlog = 1
max_binlog_size = 200M
auto_increment_increment = 2
auto_increment_offset = 2
slave-skip-errors = 1062,1053
auto_increment_increment 定义AUTO_INCREMENT的步长
auto_increment_offset 定义AUTO_INCREMENT的起点值
第二步,配置2台从(Slave)服务器配置文件,此步分别在2台从(Slave)里操作。
1.在192.168.0.101的/etc/my.cnf的[mysqld]域后追加
server_id = 101
log_bin = mysql-bin
log_bin_index = mysql-bin.index
log_slave_updates = 1
relay_log = mysql-relay-bin
relay_log_index = mysql-relay-bin.index
max_binlog_size = 200M
read_only = 1
slave-skip-errors = 1062,1053
skip_slave_start = 1
*注意:以上的设置参数,如果设定文件/etc/my.cnf里本身就有的话,就先给删掉,再追加我们的设置。
第三步,启动MySql,为root用户设置密码,并创建复制账户,在4台服务器里分别执行:
[root@m/s/m/s ~]# /etc/init.d/mysqld start
[root@m/s/m/s ~]# /usr/bin/mysqladmin -u root password 'rootpassword'
[root@m/s/m/s ~]# mysql -uroot -p Enter password:
mysql> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'repluser'@'192.168. 0.%' IDENTIFIED BY 'replpass';
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
[root@master1
/slave2
~]
# mysql -uroot -p
Enter password:
mysql> CHANGE MASTER TO MASTER_HOST=
'192.168.0.200'
,
-> MASTER_PORT=3306,
-> MASTER_USER=
'repluser'
,
-> MASTER_PASSWORD=
'replpass'
,
-> MASTER_LOG_FILE=
'mysql-bin.000001'
,
-> MASTER_LOG_POS=0;
Query OK, 0 rows affected (0.20 sec)
mysql> START SLAVE;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW SLAVE STATUS\G
[root@master2
/slave1
~]
# mysql -uroot -p
Enter password:
mysql> CHANGE MASTER TO MASTER_HOST=
'192.168.0.100'
,
-> MASTER_PORT=3306,
-> MASTER_USER=
'repluser'
,
-> MASTER_PASSWORD=
'replpass'
,
-> MASTER_LOG_FILE=
'mysql-bin.000001'
,
-> MASTER_LOG_POS=0;
Query OK, 0 rows affected (0.20 sec)
mysql> START SLAVE;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW SLAVE STATUS\G