- 主库创建一个从库登录的用户:
mysql> create user 'slave'@'%' identified by '123456';
# 将replication slave, replication client 授权到 slave 用户:
mysql> grant replication slave,replication client on *.* to 'slave'@'%';
# 刷新权限:
mysql> flush privileges;
- 配置主库:
# 设置唯一的server_id
server-id=1
# 开启 binlog
log_bin=master-bin
# 需要同步的数据库,除此之外,其他不同步
binlog-do-db=test
# 自动清理7天前的log文件
expire_logs_days=7
- 重启后执行:
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 150 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
- 配置从库:
server-id=2
# 要同步的数据库
replicate-do-db=test
- 重启后执行:
mysql> change master to master_host='主库ip',
-> master_port=3306,
-> master_user='slave',
-> master_password='123456',
-> master_log_file='mysql-bin.000001',
-> master_log_pos=150;
- 启动同步:
mysql> start slave;
# 查看状态
mysql> show slave status \G
# 可以看到这两个参数都为 yes
Slave_IO_Running: Yes
Slave_SQL_Running: Yes