原理说明
配置步骤
1、需要2台搭建完成的mysql。
2、两台机器上都建立用户,并赋予复制权限
GRANT REPLICATION SLAVE,RELOAD,SUPER ON *.* TO mysql_backup@'%' IDENTIFIED BY '123456';
GRANT REPLICATION SLAVE,RELOAD,SUPER ON *.* TO mysql_backup@'%' IDENTIFIED BY '123456';
修改配置文件
MASTER1:
[mysqld]
# for repl
server-id = 1
log-bin = mysql-bin
auto-increment-increment= 2 # 应设为整个结构中服务器的总数
auto-increment-offset = 1 # 设定数据库中自动增长的起点,避免两台服务器数据同步时出现主键冲突
MASTER2:
[mysqld]
# for repl
server-id = 2
log-bin = mysql-bin
auto-increment-increment= 2 # 应设为整个结构中服务器的总数
auto-increment-offset = 2 # 设定数据库中自动增长的起点,避免两台服务器数据同步时出现主键
配置主库
master1上指定master2为主:
CHANGE MASTER TO master_host = '192.168.1.201',
master_user = 'mysql_backup',
master_password = '123456',
master_log_file = 'mysql-bin.000001',
master_log_pos = 106;
mysql> START SLAVE;
Query OK, 0 rows affected (0.00 sec)
master2上指定master1为主:
CHANGE MASTER TO master_host = '192.168.1.200',
master_user = 'mysql_backup',
master_password = '123456',
master_log_file = 'mysql-bin.000001',
master_log_pos = 106;
Query OK, 0 rows affected (0.18 sec)
mysql> START SLAVE;
Query OK, 0 rows affected (0.00 sec)
测试主主复制
实验1:启动两个服务,在任一mysql上进行操作,在另一mysql上均能体现出来。
MySQL的主主复制实际上就是双向的主从复制。