mysql主主配置和主从配置的不同点在于主从的master不监控slavebin-log的变化,导致slave有新数据不能更新到master

主主配置的关键参数:log-slave-updates,两台mysql都需添加


测试环境:

centos 6.5 x64

主1ip:192.168.0.1

主2ip:192.168.0.2


主主配置

主1

vim /etc/my.cnf

server-id=1

bin-log=mysql-bin

binlog-format=mixed

log-slave-updates

relay-log=mysql-relay

auto_increment_increment= 2

auto_increment_offset= 1


注:auto_increment_offset和auto_increment_increment这两个参数用于避免多master情况下,多个master同时存取字段类型为AUTO_INCREMENT的冲突


主2

server-id=2

bin-log=mysql-bin

binlog-format=mixed

log-slave-updates

relay-log=mysql-relay

auto_increment_increment= 2

auto_increment_offset= 1



修改配置后均重启mysql使配置生效

分别在主1和主2上做主从

一、给主1配置从

在主1上

1.授权登陆账号(两边要建立可复制的账号)

grant replication client,replication slave on *.* to 'salve'@'192.168.0.*' identified by 'test123';

2.show master status;

mysql> show master status;

+------------------+----------+--------------+------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000002 |      653 |              |                  |

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

在主2上

stop slave;

reset slave;

change master to master_host='192.168.0.1',master_user='slave',master_password='test123',master_log_file='mysql-bin.000002',master_log_pos=653;

start slave;

show salve status\G;


 Slave_IO_Running: Yes

 Slave_SQL_Running: Yes

即成功

一、给主2配置从

步骤通给主1配置从


测试:

分别在主1和主2添加数据,看在另一台是否能查询

在主1上

mysql> select * from t;

+---+

| s |

+---+

| 2 |

+---+

1 row in set (0.00 sec)


mysql> insert into t values (3);

Query OK, 1 row affected (0.01 sec)


mysql> select * from t;         

+---+

| s |

+---+

| 2 |

| 3 |

+---+

2 rows in set (0.00 sec)


mysql> select * from t;

+---+

| s |

+---+

| 2 |

| 3 |

| 4 |

+---+

3 rows in set (0.00 sec)

在主2上

mysql> use proxy_test;

Database changed

mysql> select * from t;

+---+

| s |

+---+

| 2 |

+---+

1 row in set (0.00 sec)


mysql> select * from t;

+---+

| s |

+---+

| 2 |

| 3 |

+---+

2 rows in set (0.00 sec)


mysql> insert into t values (4);

Query OK, 1 row affected (0.00 sec)


mysql> select * from t;         

+---+

| s |

+---+

| 2 |

| 3 |

| 4 |

+---+

3 rows in set (0.00 sec)


主主配置到此测试成功