mysql主主复制配置
server1 ip:192.168.0.231
server2 ip:192.168.0.234
更改两台主机的mysql配置文件
vim /etc/my.cnf
server1添加
server-id=1
log-bin=mysqlmaster-bin.log
auto-increment-increment=2
auto-increment-offset=1
server2添加
server-id=2
log-bin=mysqlmaster-bin.log
auto-increment-increment=2
auto-increment-offset=2
说明:auto-increment-increment的值设为整个结构中服务器的总数,此实验为两台服务器,所以值为2
auto-increment-offset是用来设定数据库中自动增长的起点的,因为服务器都设定了一次自动增长的值为2,所以他们的起点必须得不同,这样才能避免两台服务器数据同步时出现主键冲突
另可以设置replicate-do-db=database-name指定需要同步的数据库
重启mysql服务使配置生效
登陆mysql
添加同步数据所需要的用户
server1上
mysql>GRANT REPLICATION SLAVE ON *.* TO ‘systop1‘@‘192.168.0.%‘ IDENTIFIED BY ‘systop‘;
server2上
GRANT REPLICATION SLAVE ON *.* TO ‘systop2‘@‘192.168.0.%‘ IDENTIFIED BY ‘systop‘;
查看两台服务器的master状态
server1
SHOW MASTER STATUS;
+------------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------------+----------+--------------+------------------+-------------------+
| mysqlmaster-bin.000002 | 120 | | | |
server2
show master status;
+------------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------------+----------+--------------+------------------+-------------------+
| mysqlmaster-bin.000001 | 336 | | | |
说明二进制日志路径
server2上执行
CHANGE MASTER TO MASTER_HOST=‘192.168.0.231‘,MASTER_USER=‘systop1‘,MASTER_PASSWORD=‘systop‘,MASTER_LOG_FILE=‘mysql-bin.000002‘,MASTER_LOG_POS=120;
server1上执行
CHANGE MASTER TO MASTER_HOST=‘192.168.0.234‘,MASTER_USER=‘systop2‘,MASTER_PASSWORD=‘systop‘,MASTER_LOG_FILE=‘mysqlmaster-bin.000001‘,MASTER_LOG_POS=336;
开启复制功能
两台机器都执行
start slave;
查看复制连接状态
执行
show slave status \G;
查看Slave_IO_Running: Yes
Slave_SQL_Running: Yes
都显示yes表示启动正常
测试:两台服务器都做数据更新操作,查看数据是否更新