MySQL 5.7 使用GTID方式搭建複製環境
當使用GTIDs(global transaction identifiers),每個事務在提交時,都會被標記一個獨特的事務號,被用於在備庫上應用,這樣在搭建複製環境時,不用使用日誌檔案和日誌位置的傳統方式搭建,大大簡化了複製環境的搭建流程。可以使用語句級別和行級別的複製格式,建議使用行級別的複製格式。
GTID的格式如下
GTID = source_id:transaction_id
source_id代表源服務端,transaction_id代表事務的順序號。
限制:
因為以GTID為基礎的複製時基於事務的,一些特性在複製中會有限制。
不支援非事務性的表,如MyISAM表等。
不支援CREATE TABLE ... SELECT語句。CREATE TABLE ... SELECT對於語句級別的複製格式不安全。當使用行級別的複製格式時,這個語句在日誌中被記錄成兩個單獨的事件,一個是表的建立,另一個是表的插入操作。當這個語句在一個事務中被執行時,在某些情況下這兩個事件會被分配相同的事務號,這樣第2個執行插入操作的事務可能會被從庫跳過。
臨時表。在事務內部,GTID複製不支援CREATE TEMPORARY TABLE、DROP TEMPORARY TABLE語句。
GTID複製不支援sql_slave_skip_counter引數。如果需要跳過事務,使用主庫上的gtid_executed引數。
主庫gtid_purged引數包含了所有在主庫二進位制日誌中清除的所有事務。
搭建流程:
編輯主庫的配置檔案,並重啟主庫
# Log
server-id = 27100
log-bin = production-bin
#log-bin-index = /log/production-bin.index
binlog_format = row
log_slave_updates
gtid-mode = ON
enforce-gtid-consistency = ON
編輯從庫的配置檔案,並重啟從庫
# Log
server-id = 35100
log-bin = production-bin
#log-bin-index = /log/production-bin.index
binlog_format = row
log_slave_updates
gtid-mode = ON
enforce-gtid-consistency = ON
在主庫上匯出備份,並傳輸到從庫
[root@localhost 20160609]# mysqldump -uroot -p'System#2013' -S /var/lib/mysql/mysql.sock -A -R --single-transaction --default-character-set=utf8 > 20160609.sql
在從庫上應用備份
[root@localhost 20160609]# mysql -uroot -p'System#2013' < 20160609.sql
在Master資料庫上面建立複製專用賬戶
mysql> grant replication slave on *.* to 'repl'@'192.168.78.%' identified by 'Mysql#2015';
Query OK, 0 rows affected, 1 warning (0.17 sec)
在從庫上執行CHANGE MASTER命令
mysql> change master to
-> master_host='192.168.78.141',
-> master_port=3306,
-> master_user='repl',
-> master_password='Mysql#2015',
-> master_auto_position = 1;
Query OK, 0 rows affected, 2 warnings (0.31 sec)
--啟動IO和SQL執行緒
mysql> start slave;
Query OK, 0 rows affected (0.04 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.78.141
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: production-bin.000002
Read_Master_Log_Pos: 448
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 671
Relay_Master_Log_File: production-bin.000002
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: 448
Relay_Log_Space: 882
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: 27100
Master_UUID: cf291e84-2c89-11e6-b6f0-000c29631605
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: cf291e84-2c89-11e6-b6f0-000c29631605:1
Executed_Gtid_Set: cf291e84-2c89-11e6-b6f0-000c29631605:1
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
mysql> show processlist;
+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
| 4 | root | localhost | fire | Query | 0 | starting | show processlist |
| 6 | system user | | NULL | Connect | 480 | Waiting for master to send event | NULL |
| 7 | system user | | NULL | Connect | 153 | Slave has read all relay log; waiting for more updates | NULL |
+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
3 rows in set (0.02 sec)
GTID的格式如下
GTID = source_id:transaction_id
source_id代表源服務端,transaction_id代表事務的順序號。
限制:
因為以GTID為基礎的複製時基於事務的,一些特性在複製中會有限制。
不支援非事務性的表,如MyISAM表等。
不支援CREATE TABLE ... SELECT語句。CREATE TABLE ... SELECT對於語句級別的複製格式不安全。當使用行級別的複製格式時,這個語句在日誌中被記錄成兩個單獨的事件,一個是表的建立,另一個是表的插入操作。當這個語句在一個事務中被執行時,在某些情況下這兩個事件會被分配相同的事務號,這樣第2個執行插入操作的事務可能會被從庫跳過。
臨時表。在事務內部,GTID複製不支援CREATE TEMPORARY TABLE、DROP TEMPORARY TABLE語句。
GTID複製不支援sql_slave_skip_counter引數。如果需要跳過事務,使用主庫上的gtid_executed引數。
主庫gtid_purged引數包含了所有在主庫二進位制日誌中清除的所有事務。
搭建流程:
編輯主庫的配置檔案,並重啟主庫
# Log
server-id = 27100
log-bin = production-bin
#log-bin-index = /log/production-bin.index
binlog_format = row
log_slave_updates
gtid-mode = ON
enforce-gtid-consistency = ON
編輯從庫的配置檔案,並重啟從庫
# Log
server-id = 35100
log-bin = production-bin
#log-bin-index = /log/production-bin.index
binlog_format = row
log_slave_updates
gtid-mode = ON
enforce-gtid-consistency = ON
在主庫上匯出備份,並傳輸到從庫
[root@localhost 20160609]# mysqldump -uroot -p'System#2013' -S /var/lib/mysql/mysql.sock -A -R --single-transaction --default-character-set=utf8 > 20160609.sql
在從庫上應用備份
[root@localhost 20160609]# mysql -uroot -p'System#2013' < 20160609.sql
在Master資料庫上面建立複製專用賬戶
mysql> grant replication slave on *.* to 'repl'@'192.168.78.%' identified by 'Mysql#2015';
Query OK, 0 rows affected, 1 warning (0.17 sec)
在從庫上執行CHANGE MASTER命令
mysql> change master to
-> master_host='192.168.78.141',
-> master_port=3306,
-> master_user='repl',
-> master_password='Mysql#2015',
-> master_auto_position = 1;
Query OK, 0 rows affected, 2 warnings (0.31 sec)
--啟動IO和SQL執行緒
mysql> start slave;
Query OK, 0 rows affected (0.04 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.78.141
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: production-bin.000002
Read_Master_Log_Pos: 448
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 671
Relay_Master_Log_File: production-bin.000002
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: 448
Relay_Log_Space: 882
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: 27100
Master_UUID: cf291e84-2c89-11e6-b6f0-000c29631605
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: cf291e84-2c89-11e6-b6f0-000c29631605:1
Executed_Gtid_Set: cf291e84-2c89-11e6-b6f0-000c29631605:1
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
| 4 | root | localhost | fire | Query | 0 | starting | show processlist |
| 6 | system user | | NULL | Connect | 480 | Waiting for master to send event | NULL |
| 7 | system user | | NULL | Connect | 153 | Slave has read all relay log; waiting for more updates | NULL |
+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
3 rows in set (0.02 sec)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26506993/viewspace-2117892/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL 5.7 基於GTID搭建主從複製MySql
- MySQL 5.5使用Xtrabackup線上搭建複製環境MySql
- MySQL 5.7基於GTID的主從複製MySql
- MySQL 5.7搭建多源複製MySql
- MySQL搭建帶過濾的複製環境MySql
- 生產環境搭建MySQL複製的教程MySql
- MySQL GTID複製MySql
- MySQL5.7主從複製-半同步複製搭建MySql
- 【Mysql】Mysql5.7的多源複製搭建MySql
- 生產環境中MySQL複製的搭建KPMySql
- mysql 5.7 主從複製搭建及原理MySql
- MySQL 5.7 用mysqldump搭建gtid主從MySql
- MySQL 5.7 用xtrabackup搭建gtid主從MySql
- MySQL 8 複製(四)——GTID與複製MySql
- MySQL 8 複製(五)——配置GTID複製MySql
- MySQL主從複製之GTID複製MySql
- MysqL主從複製_模式之GTID複製MySql模式
- MySQL 5.7傳統複製到GTID線上切換(一主一從)MySql
- Centos7.5部署MySQL5.7基於GTID主從複製+並行複製+半同步複製+讀寫分離(ProxySQL) 環境- 運維筆記 (完整版)CentOSMySql並行運維筆記
- 快速搭建streams表級複製環境
- mysql GTID 主從複製概述MySql
- MySQL 5.7並行複製MySql並行
- mysql 5.7半同步複製MySql
- mysql 5.7多源複製MySql
- MySQL 5.7 並行複製MySql並行
- Mysql 8.4.0 結合 Docker 搭建GTID主從複製,以及傳統主從複製MySqlDocker
- mysql 5.7 多主一從的多源複製搭建MySql
- MySQL 複製全解析 Part 9 一步步搭建基於GTID的MySQL複製MySql
- [Mysql]Mysql5.7並行複製MySql並行
- 【Mysql】mysql5.7無損複製MySql
- GoldenGate簡單複製環境的搭建Go
- mysql5.7主從複製,主主複製MySql
- Mysql基於GTID的複製模式MySql模式
- Mysql 基於GTID主從複製MySql
- 【MySQL】主從GTID複製修復MySql
- Mysql5.7半同步複製MySql
- MySQL 5.7 延遲複製配置MySql
- MySQL 5.6 建立GTID主從複製 (GTID-based Replication)MySql