主從配置可參考:【MySQL5.7主從同步配置】mysql
主從從,也稱爲級聯主從,數據流向:A(主)->B(從)->C(從從),主從從級聯複製。sql
注:使用docker部署mysql實例,方便快速搭建演示環境。但本文重點是講解主從配置,所以簡略描述docker環境構建mysql容器實例。docker
[client] port = 3306 default-character-set = utf8mb4 [mysql] port = 3306 default-character-set = utf8mb4 [mysqld] ########################## # summary ########################## #bind-address = 0.0.0.0 #port = 3306 #datadir=/datavol/mysql/data #數據存儲目錄 ########################## # log bin ########################## server-id = 200 #必須惟一 log_bin = mysql-bin #開啓及設置二進制日誌文件名稱 binlog_format = MIXED sync_binlog = 1 expire_logs_days =7 #二進制日誌自動刪除/過時的天數。默認值爲0,表示不自動刪除。 #binlog_cache_size = 128m #max_binlog_cache_size = 512m #max_binlog_size = 256M binlog-do-db = test #要同步的數據庫 binlog-ignore-db = mysql #不須要同步的數據庫 binlog_ignore_db = information_schema binlog_ignore_db = performation_schema binlog_ignore_db = sys ########################## # character set ########################## character-set-server = utf8mb4 collation-server = utf8mb4_unicode_ci
[root@localhost ~]# docker run -d -p 4200:3306 --name=mysql-200 -v /datavol/mysql-200/conf:/etc/mysql/conf.d -v /datavol/mysql-200:/datavol/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7 5691bac538e646db00273e3cad5b350dbe6cce0bd176346b7eefd9a6f9e3a9ad [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5691bac538e6 mysql:5.7 "docker-entrypoint..." 44 seconds ago Up 43 seconds 33060/tcp, 0.0.0.0:4200->3306/tcp mysql-200 [root@localhost ~]# docker exec -it mysql-200 /bin/bash root@5691bac538e6:/# mysql -u root -p Enter password:
注:若不熟悉docker,可以使用傳統方式安裝mysql,效果相同。數據庫
鏈接mysql主數據庫,鍵入命令mysql -u root -p,輸入密碼後登陸數據庫。建立用戶用於從庫同步複製,授予複製、同步訪問的權限安全
mysql> CREATE USER 'slave'@'%' IDENTIFIED BY '123456'; Query OK, 0 rows affected (0.01 sec) mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%'; Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'log_bin'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | log_bin | ON | +---------------+-------+ 1 row in set
mysql> show master status; +------------------+----------+--------------+--------------------------------------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+--------------------------------------------------+-------------------+ | mysql-bin.000001 | 154 | test | mysql,information_schema,performation_schema,sys | | +------------------+----------+--------------+--------------------------------------------------+-------------------+ 1 row in set
[client] port = 3306 default-character-set = utf8mb4 [mysql] port = 3306 default-character-set = utf8mb4 [mysqld] ########################## # summary ########################## #bind-address = 0.0.0.0 #port = 3306 #datadir=/datavol/mysql/data #數據存儲目錄 ########################## # log bin ########################## server-id = 210 #必須惟一 log_bin = mysql-bin #開啓及設置二進制日誌文件名稱 binlog_format = MIXED sync_binlog = 1 expire_logs_days =7 #二進制日誌自動刪除/過時的天數。默認值爲0,表示不自動刪除。 log_slave_updates = 1 #【關鍵點】從主服務器接收到的更新同時要寫入二進制日誌 #binlog_cache_size = 128m #max_binlog_cache_size = 512m #max_binlog_size = 256M binlog-do-db = test #要同步的數據庫 binlog-ignore-db = mysql #不須要同步的數據庫 binlog_ignore_db = information_schema binlog_ignore_db = performation_schema binlog_ignore_db = sys ########################## # character set ########################## character-set-server = utf8mb4 collation-server = utf8mb4_unicode_ci
log_slave_updates = 1 #【關鍵點】從主服務器接收到的更新同時要寫入二進制日誌bash
[root@localhost ~]# docker run -d -p 4210:3306 --name=mysql-210 -v /datavol/mysql-210/conf:/etc/mysql/conf.d -v /datavol/mysql-210:/datavol/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7 19e93b6d93ca4e6ca0d540e3f6c831b835cdbb35362733867c3977aee4d33bf7 [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 19e93b6d93ca mysql:5.7 "docker-entrypoint..." 4 seconds ago Up 3 seconds 33060/tcp, 0.0.0.0:4210->3306/tcp mysql-210 [root@localhost ~]# docker exec -it mysql-210 /bin/bash root@19e93b6d93ca:/# mysql -u root -p Enter password:
登陸【從數據庫】,進入mysql命令行。服務器
mysql> stop slave; Query OK, 0 rows affected mysql> CHANGE MASTER TO MASTER_HOST='192.168.10.212', MASTER_PORT=4200, MASTER_USER='slave', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin.000003', MASTER_LOG_POS=617; Query OK, 0 rows affected mysql> start slave; Query OK, 0 rows affected
stop slave; //中止同步
start slave; //開始同步
//必須和【主數據庫】的信息匹配。
CHANGE MASTER TO
MASTER_HOST='192.168.10.212', //主庫IP
MASTER_PORT=4200, //主庫端口
MASTER_USER='slave', //訪問主庫且有同步複製權限的用戶
MASTER_PASSWORD='123456', //登陸密碼
//【關鍵處】從主庫的該log_bin文件開始讀取同步信息,主庫show master status返回結果
MASTER_LOG_FILE='mysql-bin.000001',
//【關鍵處】從文件中指定位置開始讀取,主庫show master status返回結果
MASTER_LOG_POS=154;網絡
mysql> show slave status \G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.10.212 Master_User: slave Master_Port: 4200 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 617 Relay_Log_File: 5f0d9861bcb1-relay-bin.000002 Relay_Log_Pos: 320 Relay_Master_Log_File: mysql-bin.000003 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: 617 Relay_Log_Space: 534 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: 200 Master_UUID: b6df98cf-e71f-11e8-9987-0242ac110007 Master_Info_File: /var/lib/mysql/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: 1 row in set (0.00 sec)
只有【Slave_IO_Running】和【Slave_SQL_Running】都是Yes,則同步是正常的。
若是是No或者Connecting都不行,可查看mysql-error.log,以排查問題。tcp
mysql> show variables like 'log_error%'; +---------------------+--------+ | Variable_name | Value | +---------------------+--------+ | log_error | stderr | | log_error_verbosity | 3 | +---------------------+--------+ 2 rows in set
配置完成,則主從數據庫開始自動同步。此外,【從庫】至關於【從從庫】的主庫,也須要設置帳號及同步權限。性能
建立用戶用於從庫同步複製,授予複製、同步訪問的權限
mysql> CREATE USER 'slave'@'%' IDENTIFIED BY '123456'; Query OK, 0 rows affected (0.01 sec) mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%'; Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'log_bin'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | log_bin | ON | +---------------+-------+ 1 row in set
mysql> show master status; +------------------+----------+--------------+--------------------------------------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+--------------------------------------------------+-------------------+ | mysql-bin.000003 | 154 | test | mysql,information_schema,performation_schema,sys | | +------------------+----------+--------------+--------------------------------------------------+-------------------+ 1 row in set
[client] port = 3306 default-character-set = utf8mb4 [mysql] port = 3306 default-character-set = utf8mb4 [mysqld] ########################## # summary ########################## #bind-address = 0.0.0.0 #port = 3306 #datadir=/datavol/mysql/data #數據存儲目錄 ########################## # log bin ########################## server-id = 211 ########################## # character set ########################## character-set-server = utf8mb4 collation-server = utf8mb4_unicode_ci
[root@localhost ~]# docker run -d -p 4211:3306 --name=mysql-211 -v /datavol/mysql-211/conf:/etc/mysql/conf.d -v /datavol/mysql-211:/datavol/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7 19e93b6d93ca4e6ca0d540e3f6c831b835cdbb35362733867c3977aee4d33bf7 [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 19e93b6d93ca mysql:5.7 "docker-entrypoint..." 4 seconds ago Up 3 seconds 33060/tcp, 0.0.0.0:4211->3306/tcp mysql-211 [root@localhost ~]# docker exec -it mysql-211 /bin/bash root@19e93b6d93ca:/# mysql -u root -p Enter password:
登陸【從數據庫】,進入mysql命令行。
mysql> stop slave; Query OK, 0 rows affected mysql> CHANGE MASTER TO MASTER_HOST='192.168.10.212', MASTER_PORT=4210, MASTER_USER='slave', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin.000003', MASTER_LOG_POS=617; Query OK, 0 rows affected mysql> start slave; Query OK, 0 rows affected
stop slave; //中止同步
start slave; //開始同步
//必須和【主數據庫】的信息匹配。
CHANGE MASTER TO
MASTER_HOST='192.168.10.212', //主庫IP
MASTER_PORT=4210, //主庫端口
MASTER_USER='slave', //訪問主庫且有同步複製權限的用戶
MASTER_PASSWORD='123456', //登陸密碼
//【關鍵處】從主庫的該log_bin文件開始讀取同步信息,主庫show master status返回結果
MASTER_LOG_FILE='mysql-bin.000003',
//【關鍵處】從文件中指定位置開始讀取,主庫show master status返回結果
MASTER_LOG_POS=617;
mysql> show slave status \G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.10.212 Master_User: slave Master_Port: 4210 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 617 Relay_Log_File: a3d52e314640-relay-bin.000002 Relay_Log_Pos: 783 Relay_Master_Log_File: mysql-bin.000003 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: 617 Relay_Log_Space: 997 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: 210 Master_UUID: 088c2c93-e720-11e8-8e80-0242ac110009 Master_Info_File: /var/lib/mysql/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: 1 row in set (0.00 sec)
只有【Slave_IO_Running】和【Slave_SQL_Running】都是Yes,則同步是正常的。
若是是No或者Connecting都不行,可查看mysql-error.log,以排查問題。
mysql> show variables like 'log_error%'; +---------------------+--------+ | Variable_name | Value | +---------------------+--------+ | log_error | stderr | | log_error_verbosity | 3 | +---------------------+--------+ 2 rows in set
配置完成,則從從數據庫開始自動同步。
使用root帳號登陸【主服務器】,建立test數據庫
mysql> CREATE DATABASE test; Query OK, 1 row affected (0.00 sec) mysql> USE test; Database changed
在【主庫】中建立user表
CREATE TABLE `user` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL, `age` tinyint(3) unsigned NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
在【主庫】中向user表插入一條數據:
mysql> use test; Database changed mysql> INSERT INTO user (id, name, age) VALUES (1, '張三', 18); Database changed mysql> SELECT * FROM user; +----+------+-----+ | id | name | age | +----+------+-----+ | 1 | 張三 | 18 | +----+------+-----+ 1 row in set
在【從庫】及【從從庫】中查詢user表數據:
mysql> use test; Database changed mysql> SELECT * FROM user; +----+------+-----+ | id | name | age | +----+------+-----+ | 1 | 張三 | 18 | +----+------+-----+ 1 row in set
新增記錄同步成功。
在【主庫】中修改剛纔插入的數據:
mysql> UPDATE user SET name='李四' where id=1; Database changed Rows matched: 1 Changed: 1 Warnings: 0 mysql> SELECT * FROM user; +----+------+-----+ | id | name | age | +----+------+-----+ | 1 | 李四 | 18 | +----+------+-----+ 1 row in set
在【從庫】及【從從庫】中查詢user表數據:
mysql> SELECT * FROM user; +----+------+-----+ | id | name | age | +----+------+-----+ | 1 | 李四 | 18 | +----+------+-----+ 1 row in set
更新記錄同步成功。
在【主庫】中刪除剛纔更新的數據:
mysql> DELETE FROM user WHERE id=1; Database changed mysql> SELECT * FROM user; Empty set
在【從庫】及【從從庫】中查詢user表數據:
mysql> SELECT * FROM user; Empty set
刪除記錄同步成功。
補充: