title: Windows配置mysql主從複製
date: 2019-09-21 08:45:18
tags: MySQL
環境:
mysql 8.0.17
windows 10
1. 在同一臺機器上安裝兩個mysql
安裝略…
這裏是採用不同的端口,分別爲3306,3307
2. master配置:
1. 配置my.ini
my.ini配置文件:
[mysqld]
port = 3306 #master庫端口號
server_id = 1 #服務器的唯一ID號,主從之間不能衝突,默認是1
log-bin=mysql-bin #啓動binlog 日誌功能
# binlog-do-db=dongyi #要同步的數據庫,默認所有庫
binlog-ignore-db=mysql
binlog-format = mixed #日誌的記錄格式,默認是mixed,推薦也是這個
2. 配置給從庫使用的用戶
說明: mgw爲我的用戶名,這裏mgw賬號必須要有操作數據庫的權限,ip是從庫的ip,由於在同一臺機器,所以寫127.0.0.1
CREATE USER 'mgw'@'127.0.0.1' IDENTIFIED BY 'PASSWORD';
GRANT REPLICATION SLAVE ON *.* TO 'mgw'@'127.0.0.1';
FLUSH PRIVILEGES; # 刷新權限,立即生效
SHOW MASTER STATUS; #查看主服務器的狀態,並記錄 File和Position字段,從服務器要使用到
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 2658 | dongyi | mysql | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
3. slave配置
1.配置從庫my.ini
my.ini配置文件:
[mysqld]
port = 3307
server-id = 2 #服務器唯一ID號,不能重複
log-bin = mysql-bin
binlog-format = mixed #可以不用配置,默認是mixed,在沒有配置log-bin時,這個值並不生效
#從庫的本地事務日誌更新,默認從主庫同步過來的操作記錄並不寫入從庫的本地事務日誌,
#會影響到我們在從庫上做 事務日誌 備份功能,此處開啓
#如果不會在從庫上做事務日誌備份(增量備份)功能,建議不開啓,減少磁盤IO
log-slave-updates = 1
2.添加主庫信息
CHANGE MASTER TO MASTER_HOST='127.0.0.1',MASTER_PORT=3306,MASTER_USER='mgw',MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=2658;
# MASTER_LOG_FILE 和 MASTER_LOG_POS爲master的日誌文件和位置
3.啓動從庫複製功能
START SLAVE; #啓動從複製功能
STOP SLAVE; #停止從複製功能的命令
RESET SLAVE; #重置從複製功能的配置,會清除 master.info 和 relay-log.info 兩個文件
4.查看從庫複製狀態
SHOW SLAVE STATUS\G;
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 127.0.0.1
Master_User: mgw
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 2658
Relay_Log_File: MY-20190430BUDR-relay-bin.000003
Relay_Log_Pos: 523
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 2658
Relay_Log_Space: 741
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: eb57479b-a6d0-11e9-b4e1-507b9d99d971
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:
1 row in set (0.00 sec)
**注意:**這兩項都爲Yes, 即爲配置成功
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
4.測試
master中:
mysql> create table mgw_test(
-> id int not null primary key auto_increment,
-> name varchar(10),
-> age int);
Query OK, 0 rows affected (0.03 sec)
slave中:
mysql> show tables;
+------------------+
| Tables_in_dongyi |
+------------------+
| mgw_test |
+------------------+
1 rows in set (0.00 sec)
採坑: 由於server_id 重複,導致Slave_IO_Running: No
設置全局server_id即可:
SET GLOBAL server_id=2;
微信公衆號:一丁點技術