MySQL-master2:192.168.72.129
OS版本:CentOS 5.4
MySQL版本:5.5.9(主从复制的master和slave服务器中的mysql版本相同或者master版本高于slave版本)
一、MySQL master-master配置
1、修改MySQL配置文件
两台MySQL均如要开启binlog日志功能,开启方法:在MySQL-master1 配置文件/etc/my.cnf
[MySQLd]段中加上
user = mysql
log-bin=mysql-bin
server-id = 1
binlog-do-db=test
binlog-ignore-db=mysql
replicate-do-db=test
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1
在MySQL-master2 配置文件/etc/my.cnf
[MySQLd]段中加上
user = mysql
log-bin=mysql-bin
server-id= 2
binlog-do-db=test
binlog-ignore-db=mysql
replicate-do-db=test
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=2
将两个配置文件保存,分别重启mysql服务器
server_id值必须为2到232–1之间的一个正整数值。ID值唯一的标识了复制群集中的主从服务器,因此它们必须各不相同。
binlog-do-db=database 是要记录日志的 数据库;
同步多个数据库重复设置选项binlog-do-db=test 和replicate-do-db=test
例如
binlog-do-db=test1
replicate-do-db=test1
replicate-do-db=test2
binlog-ignore-db 是不要记录日志的数据库名,多个数据库中间用逗号(,)隔开;
mysql配置文件my.cnf中 log-slave-updates表示 如果一个MASTER 挂掉的话,另外一个马上接管。
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1 指的是服务器频繁的刷新日志。这个保证了在其中一台挂掉的话,日志刷新到另外一台。从而保证了数据的同步 。
auto_increment_offset = 1
auto_increment_increment = 2
这样A的auto_increment字段产生的数值是:1, 3, 5, 7, …等奇数ID了
auto_increment_offset = 2
auto_increment_increment = 2
这样B的auto_increment字段产生的数值是:2, 4, 6, 8, …等偶数ID了
你的auto_increment字段在不同的服务器之间绝对不会重复,所以Master-Master结构就没有任何问题了。当然,你还可以使用3 台,4台,或者N台服务器,只要保证auto_increment_increment = N 再设置一下auto_increment_offset为适当的初始值就可以了,那样,我们的MySQL可以同时有几十台主服务器,而不会出现自增长ID 重复。
2、将192.168.72.128设为192.168.72.129的主服务器
在192.168.72.128上新建授权用户
MySQL> grant replication slave on *.* to 'replication'@'%' identified by 'replication';
Query OK, 0 rows affected (0.00 sec)
MySQL>flush privileges;
Query OK, 0 rows affected (0.00 sec)
MySQL> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| MySQL-bin.000003 | 374 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
------------------------------------------------------------------------------
在192.168.72.129将192.168.72.128为自己的主服务器
MySQL> change master to master_host='192.168.72.128',master_user='replication',master_password='replication',master_log_file='MySQL-bin.000003',master_log_pos=374;
Query OK, 0 rows affected (0.05 sec)
MySQL> start slave;
Query OK, 0 rows affected (0.00 sec)
MySQL> show slave status/G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.72.128
Master_User: replication
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: MySQL-bin.000003
Read_Master_Log