MySQL主主复制结构区别于主从复制结构。在主主复制结构中,两台服务器的任何一台上面的数据库存发生了改变都会同步到另一台服务器上,这样两台服务器互为主从,并且都能向外提供服务。
有了上一节的主从复制,那么主主复制就很容易了。一、先修改配置文件
服务器A(192.168.1.254)配置如下
log-bin = mysql-binserver-id = 1 expire-logs-days = 100 replicate-do-db = testbinlog-ignore-db = mysqlbinlog-ignore-db = information_schemaauto-increment-increment = 2 auto-increment-offset = 1服务器B(192.168.1.252)配置
log-bin = mysql-binserver-id = 2expire-logs-days = 100replicate-do-db = testbinlog-ignore-db = mysqlbinlog-ignore-db = information_schemaauto-increment-increment = 2auto-increment-offset = 2两台服务器都重启
mysql> service mysqld restart注:二都只有server-id不同和 auto-increment- offset不同auto-increment-offset是用来设定数据库中自动增长的起点的,回为这两能服务器都设定了一次自动增长值2,所以它们的起点必须得不同,这样才能避免两台服务器数据同步时出现主键冲突replicate-do-db 指定同步的数据库,我们只在两台服务器间同步test数据库另:auto-increment-increment的值应设为整个结构中服务器的总数,本案例用到两台服务器,所以值设为2
二、同步数据
本文是用test做的实验,导出将test.sql文件从254服务器拷贝到252服务器
备份数据前先锁表,保证数据一致性 mysql> FLUSH TABLES WITH READ LOCK;# mysqldump -uroot -p123456 test> /tmp/test.sql;
mysql> UNLOCK TABLES;
scp /tmp/test.sql
三、相互授权用户(在A服务器授权一个允许B访问的用户,反之亦然)在服务器A(192.168.1.254)上
mysql> GRANT REPLICATION SLAVE ON *.* TO IDENTIFIED BY PASSWORD '123456';mysql> flush privileges;在服务器B(192.168.1.252)上mysql> GRANT REPLICATION SLAVE ON *.* TO IDENTIFIED BY PASSWORD '123456';mysql> flush privileges;四、互告bin-log信息
在服务器A(192.168.1.254)
mysql> show master status;+------------------+----------+--------------+--------------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+------------------+----------+--------------+--------------------------+| mysql-bin.000006 | 106 | | mysql,information_schema |+------------------+----------+--------------+--------------------------+在服务器A(192.168.1.252)mysql> show master status;+------------------+----------+--------------+--------------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+------------------+----------+--------------+--------------------------+| mysql-bin.000008 | 192 | | mysql,information_schema |+------------------+----------+--------------+--------------------------+在A服务器(192.168.1.254)上执行 mysql> change master to master_host='192.168.1.252',master_user='mysync',master_password='123456',master_log_file='mysql-bin.000008',master_log_pos=192;在B服务器(192.168.1.252)上执行 mysql> change master to master_host='192.168.1.254',master_user='mysync',master_password='123456',master_log_file='mysql-bin.000006',master_log_pos=106;五、在两服务器都执行以下命令
mysql> start slave;
六、查看状态
mysql> show slave status\G
A服务器(192.168.1.254)状态如下:Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.252 Master_User: mysync Master_Port: 3306Connect_Retry: 60 Master_Log_File: mysql-bin.000008 Read_Master_Log_Pos: 192 Relay_Log_File: mysqld-relay-bin.000009Relay_Log_Pos: 337Relay_Master_Log_File: mysql-bin.000008 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: testB服务器(192.168.1.252)状态如下: Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.254 Master_User: mysync Master_Port: 3306Connect_Retry: 60 Master_Log_File: mysql-bin.000006 Read_Master_Log_Pos: 106 Relay_Log_File: mysqld-relay-bin.000014Relay_Log_Pos: 251Relay_Master_Log_File: mysql-bin.000006 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: test当看到了两个yes,即:Slave_IO_Running: YesSlave_SQL_Running: Yes说明已经配置成功了接下来看可以做一下实验,测试一下是否同步
PS:
在测试的过程当中,我也遇到一些问题主要是两台机器互相通信的问题请注意,一定要保持两台的服务器的mysql端口都向对方打开,要不然是不能成功同步的。