1、在两台主机上同时安装mysql,具体安装步骤
https://www.jianshu.com/p/7145991c9533
2、两台机器上都建立用户,并赋予复制权限:
master1:
mysql> CREATE USER 'test'@'%';
mysql> GRANT REPLICATION SLAVE ON . TO 'test'@'%' IDENTIFIED BY 'test';
master2:
mysql> CREATE USER 'test'@'%';
mysql> GRANT REPLICATION SLAVE ON . TO 'test'@'%' IDENTIFIED BY 'test';
3、
master1上的my.cnf添加:
[mysqld]
server-id = 1
log-bin = mysql-bin
auto-increment-increment= 2 # 应设为整个结构中服务器的总数
auto-increment-offset = 1 # 设定数据库中自动增长的起点,避免两台服务器数据同步时出现主键冲突
master2上的my.cnf添加:
[mysqld]
server-id = 2
log-bin = mysql-bin
auto-increment-increment= 2
auto-increment-offset = 1
重启两台机器上的服务。
4、
master1上指定master2为主:
mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.0.149',
-> MASTER_USER='test34',
-> MASTER_PASSWORD='test34';
Query OK, 0 rows affected (0.19 sec)
mysql> START SLAVE;
Query OK, 0 rows affected (0.00 sec)
master2上指定master1为主:
mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.5.143',
-> MASTER_USER='test12',
-> MASTER_PASSWORD='test12';
Query OK, 0 rows affected (0.18 sec)
mysql> START SLAVE;
Query OK, 0 rows affected (0.00 sec)
5、 测试
实验1:启动两个服务,在任一mysql上进行操作,在另一mysql上均能体现出来。