MySQL主主复制
服务器信息:
- Centos6.5
- M1: 192.168.41.150
- M2: 192.168.41.180
MySQL版本信息: 5.6.34-log MySQL Community Server (GPL)
开始搭建
第一步 新建好同步账号和设置权限
在 M1、M2服务都新建号同步账号和设置权限
mysql > GRANT REPLICATION SLAVE ON *.* to 'backup'@'%' identified by '123456';
mysql > FLUSH PRIVILEGES;
第二步 配置my.cnf
配置M1 MySQL的配置文件my.cnf
[root@master ~]# vi /etc/my.cnf
## mm conf start
log-bin=mysql-bin
binlog_format=MIXED #binlog日志格式,mysql默认采用statement,建议使用mixed
auto_increment_increment=2 #步进值auto_imcrement。一般有n台主MySQL就填n
auto_increment_offset=1 #起始值。一般填第n台主MySQL。此时为第一台主MySQL
## mm conf end
#binlog的配置策略(可选)
expire_logs_days= 7 #binlog过期清理时间
max_binlog_size= 100m #binlog每个日志文件大小
binlog_cache_size= 4m #binlog缓存大小
max_binlog_cache_size=512m #最大binlog缓存大小
配置M2 MySQL的配置文件my.cnf
[root@master ~]# vi /etc/my.cnf
## mm conf start
log-bin=mysql-bin
binlog_format=MIXED #binlog日志格式,mysql默认采用statement,建议使用mixed
auto_increment_increment=2 #步进值auto_imcrement。一般有n台主MySQL就填n
auto_increment_offset=2 #起始值。第二台配置2
## mm conf end
#binlog的配置策略(可选)
expire_logs_days= 7 #binlog过期清理时间
max_binlog_size= 100m #binlog每个日志文件大小
binlog_cache_size= 4m #binlog缓存大小
max_binlog_cache_size=512m #最大binlog缓存大小
配置完重启2台MYSQL服务.
第三步 配置同步信息
在 M2 通过命令mysql > show master status
,得到binlog文件名和binlog偏移量,然后在M1执行
M1 mysql > change master to master_host='192.168.41.180',master_user='backup',master_password='123456',master_log_file='mysql-bin.000024',master_log_pos=120;
同理,在 M1查看binlog 日志和偏移量,在 M2配置
在 M2 通过命令```show master status```,得到binlog文件名和binlog偏移量,然后在M1执行
M2 mysql > change master to master_host='192.168.41.150',master_user='backup',master_password='123456',master_log_file='mysql-bin.000044',master_log_pos=1130;
第四步 启动
- 在 M1
mysql > start slave;
- 在 M2
mysql > start slave;
第五步 查看 Slave_IO_Running、Slave_SQL_Running 状态
- 在 M1
mysql > show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.41.180
Master_User: backup
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000024
Read_Master_Log_Pos: 371
Relay_Log_File: master-relay-bin.000002
Relay_Log_Pos: 534
Relay_Master_Log_File: mysql-bin.000024
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
#后面其它信息省略
1 row in set (0.00 sec)
- 在 M2
mysql > show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.41.150
Master_User: backup
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000074
Read_Master_Log_Pos: 338
Relay_Log_File: localhost-relay-bin.000007
Relay_Log_Pos: 501
Relay_Master_Log_File: mysql-bin.000074
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
互为主从到此安装成功了。新建数据库表,插入数据,进行验证。