- 主庫創(chuàng)建一個從庫登錄的用戶:
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
# 需要同步的數(shù)據(jù)庫,除此之外,其他不同步
binlog-do-db=test
# 自動清理7天前的log文件
expire_logs_days=7
- 重啟后執(zhí)行:
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
# 要同步的數(shù)據(jù)庫
replicate-do-db=test
- 重啟后執(zhí)行:
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;
# 查看狀態(tài)
mysql> show slave status \G
# 可以看到這兩個參數(shù)都為 yes
Slave_IO_Running: Yes
Slave_SQL_Running: Yes