一. 準備工作
- 準備兩臺服務器(電腦),接入局域網中,使互相ping得通對方
- 兩臺服務器都安裝mysql,必須保證mysql的版本一致
- 假設,服務器master:192.168.0.2,服務器slave:192.168.0.3
二. 創建同步用戶
在主服務器上爲從服務器建立一個連接賬戶,該賬戶必須授予replication slave權限。因爲一主一從,所以只需要在服務器master建立一個同步用戶就可以了。
服務器master:
創建賬號:jun
密碼:0000
權限:replication slave
mysql> grant replication slave on *.* to 'jun'@'%' identified by '0000';
mysql> flush privileges;
三. 配置服務器master
1、編輯my.ini文件
開啓二進制日誌
指定需要備份的庫
指定日誌格式
[mysqld]
server-id=1
log-bin="mysql-bin"
binlog-do-db="my_db_test"
binlog-do-db="my_db_test2"##多個庫就寫多行
binlog_format="MIXED"
2、重啓mysql服務
3、進入服務器master查看是否啓用了日誌
mysql> show variables like 'log_bin';
±--------------±------+
| Variable_name | Value |
±--------------±------+
| log_bin | ON |
±--------------±------+
1 row in set (0.00 sec)
4、進入服務器master主庫上查看數據結點
mysql> show master status;
±---------------±---------±-------------±-----------------±------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
±---------------±---------±-------------±-----------------±------------------+
| mysql-bin.000007 | 120 | | | |
±---------------±---------±-------------±-----------------±------------------+
1 row in set (0.00 sec)
四. 配置服務器slave
1、編輯my.ini文件
[mysqld]
server-id=2
log_slave_updates = 1
read_only = 1
2、重啓mysql服務
3、從庫上面開啓同步
mysql>CHANGE MASTER TO MASTER_HOST='192.168.0.2',MASTER_USER='jun',MASTER_PASSWORD='0000',MASTER_LOG_FILE='mysql-bin.000007',MASTER_LOG_POS=120;
mysql>stop slave;##關閉slave模式
mysql>start slave;##開啓slave模式
4、查看是否實現同步成功
mysql> show slave status\G
*************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.0.2 Master_User: jun Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000007 Read_Master_Log_Pos: 120 Relay_Log_File: szthdb02-relay-bin.000002 Relay_Log_Pos: 281 Relay_Master_Log_File:mysql-bin.000007 Slave_IO_Running: Yes Slave_SQL_Running: Yes
##可以發現Slave_IO_Running,和Slave_SQL_Running已經都爲yes說明主從複製配置成功
參考資料
Mysql主從複製以及常見錯誤問題分析
https://blog.51cto.com/xiaozhagn/2063693
MySQL主從僅同步指定庫
https://www.cnblogs.com/new-journey/p/11319527.html
mysql主從複製(一主一從)
https://www.2cto.com/net/201902/796213.html
mysql主從複製常見問題
https://www.cnblogs.com/shixiuxian/p/11218686.html
windows下使用mysql雙機熱備功能
https://www.cnblogs.com/Mr-kevin/p/5590542.html
mysql主從複製 主主複製
https://segmentfault.com/a/1190000009724090
MySQL日誌格式 binlog_format
https://blog.csdn.net/mycwq/article/details/17136997
在window配置MySQL5.7主從複製遇到錯誤及解決方法
https://blog.csdn.net/xiaozhenzi66/article/details/81220624