推荐 MySQL 双主单写
对 Master1 配置信息修改
修改/etc/my.cnf
vim /etc/my.cnf#添加或修改内容如下log_bin=mysql-binserver-id=1sync-binlog=1#指定哪些库不同步,其他库默认都同步binlog-ignore-db=information_schemabinlog-ignore-db=performance_schemabinlog-ignore-db=sys#指定哪些库同步,不需要#binlog-do-db=lanebin#双主配置relay_log=mysql-relay-binlog_slave_updates=1#双主单写可以不配置,双主双写要配置主键递增1,3,5,7auto_increment_offset=1auto_increment_increment=2
重启 mysql
systemctl restart mysqld
授权主库的 root 权限
#进入mysqlgrant replication slave on *.* to 'root'@'%' identified by 'root';grant all privileges on *.* to 'root'@'%' identified by 'root';flush privileges
查看 binlog 信息
mysql> show master status \G;*************************** 1. row ***************************File: mysql-bin.000004Position: 154Binlog_Do_DB:Binlog_Ignore_DB: information_schema,performance_schema,sysExecuted_Gtid_Set:1 row in set (0.00 sec)
对 Master2 配置信息修改
修改配置信息
vim /etc/my.cnf#添加或修改内容如下log_bin=mysql-binserver-id=12sync-binlog=1#指定哪些库不同步,其他库默认都同步binlog-ignore-db=information_schemabinlog-ignore-db=performance_schemabinlog-ignore-db=sys#指定哪些库同步,不需要#binlog-do-db=lanebin#双主配置relay_log=mysql-relay-binlog_slave_updates=1##双主单写可以不配置,双主双写要配置主键递增2,4,6,8auto_increment_offset=2auto_increment_increment=2#可以添加readonly
重启 mysql
systemctl restart mysqld
授权 root 权限
#进入mysqlgrant replication slave on *.* to 'root'@'%' identified by 'root';grant all privileges on *.* to 'root'@'%' identified by 'root';flush privileges
查看 binlog 信息
mysql> show master status \G;*************************** 1. row ***************************File: mysql-bin.000013Position: 884Binlog_Do_DB:Binlog_Ignore_DB: information_schema,performance_schema,sysExecuted_Gtid_Set:1 row in set (0.00 sec)
分别为 master1 和 master2 指定主库
进入 master1 的 mysql 界面
#指定m1的主库m2的ip端口用户密码binlog信息mysql> change master to master_host='172.16.94.9' ,master_port=3306,master_user='root',master_password='root',master_log_file='mysql-bin.000013',master_log_pos=884;mysql> start slave;#查看是否指定主库成功mysql> show slave status \G;*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 172.16.94.9Master_User: rootMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000013Read_Master_Log_Pos: 884Relay_Log_File: mysql-relay-bin.000002Relay_Log_Pos: 320Relay_Master_Log_File: mysql-bin.000013Slave_IO_Running: YesSlave_SQL_Running: Yes
进入 master2 的 mysql 界面
#指定m2的主库m1的ip端口用户密码binlog信息mysql> change master to master_host='172.16.94.5' ,master_port=3306,master_user='root',master_password='root',master_log_file='mysql-bin.000004',master_log_pos=154;mysql> start slave;#查看是否指定主库成功mysql> show slave status \G;*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 172.16.94.5Master_User: rootMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000004Read_Master_Log_Pos: 154Relay_Log_File: mysql-relay-bin.000002Relay_Log_Pos: 320Relay_Master_Log_File: mysql-bin.000004Slave_IO_Running: YesSlave_SQL_Running: Yes
测试双主是否成功
在 Master1 进行操作
create detabase mymaster1;use mymaster1;create table test1(id int primary key auto_increment,name varchar(200))engine=innodb charset=utf8;insert into test1(name)values('a');insert into test1(name)values('b');
在 Master2 进行操作
use mymaster1;insert into test1(name)values('x');insert into test1(name)values('y');
分别查看 master1 和 master2 的数据,可以看出实现了互为主从的双主模式
#master1mysql> select * from test1;+----+------+| id | name |+----+------+| 1 | a || 3 | b || 4 | x || 6 | y |+----+------+4 rows in set (0.00 sec)#master2mysql> select * from test1;+----+------+| id | name |+----+------+| 1 | a || 3 | b || 4 | x || 6 | y |+----+------+4 rows in set (0.00 sec)
