配置MYSQL基于GTID 主从复制详细解析及步骤
生活随笔
收集整理的這篇文章主要介紹了
配置MYSQL基于GTID 主从复制详细解析及步骤
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
GTID的概念
GTID的組成
GTID = source_id:transaction_idsource_id,用于鑒別原服務器,即mysql服務器唯一的的server_uuid,由于GTID會傳遞到slave,所以也可以理解為源ID。transaction_id,為當前服務器上已提交事務的一個序列號,通常從1開始自增長的序列,一個數(shù)值對應一個事務。示例: 3E11FA47-71CA-11E1-9E33-C80AA9429562:23 前面的一串為服務器的server_uuid,即3E11FA47-71CA-11E1-9E33-C80AA9429562,后面的23為transaction_idGTID的優(yōu)勢
1、更簡單的實現(xiàn)failover,不用以前那樣在需要找log_file和log_pos。 2、更簡單的搭建主從復制。 3、比傳統(tǒng)的復制更加安全。 4、GTID是連續(xù)的沒有空洞的,保證數(shù)據(jù)的一致性,零丟失。GTID的工作原理
1、當一個事務在主庫端執(zhí)行并提交時,產生GTID,一同記錄到binlog日志中。 2、binlog傳輸?shù)絪lave,并存儲到slave的relaylog后,讀取這個GTID的這個值設置gtid_next變量,即告訴Slave,下一個要執(zhí)行的GTID值。 3、sql線程從relay log中獲取GTID,然后對比slave端的binlog是否有該GTID。 4、如果有記錄,說明該GTID的事務已經執(zhí)行,slave會忽略。 5、如果沒有記錄,slave就會執(zhí)行該GTID事務,并記錄該GTID到自身的binlog,在讀取執(zhí)行事務前會先檢查其他session持有該GTID,確保不被重復執(zhí)行。 6、在解析過程中會判斷是否有主鍵,如果沒有就用二級索引,如果沒有就用全部掃描。配置基于GTID的復制
環(huán)境說明:
| 主數(shù)據(jù)庫 | 192.168.24.128 | centos7 myslq-5.7 |
| 從數(shù)據(jù)庫 | 192.168.24.130 | centos7 myslq-5.7 |
配置安裝二臺myslq服務
具體配置查看《mysql進階簡單解析》
在主數(shù)據(jù)庫服務器端操作如下
編輯配置文件
[root@linfan ~]# vim /etc/my.cnf [root@linfan ~]# cat /etc/my.cnf [mysqld] basedir = /usr/local/mysql datadir = /opt/data socket = /tmp/mysql.sock port = 3306 pid-file = /opt/data/mysql.pid user = mysql skip-name-resolve //添加以下內容 # GTID server-id=128 //主服務器id gtid-mode=on //開啟gtid模式 enforce-gtid-consistency=on //強制gtid一致性,開啟后對于特定create table不被支持 # binlog log_bin=master-binlog log-slave-updates=1 binlog_format=row //強烈建議,其他格式可能導致數(shù)據(jù)不一致 # relay log skip_slave_start=1重啟myslq服務
[root@linfan ~]# service mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL.. SUCCESS!在主數(shù)據(jù)庫上創(chuàng)建一個同步賬戶授權給從數(shù)據(jù)庫使用
[root@linfan ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.7.22 MySQL Community Server (GPL)Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> create user 'repl'@'192.168.24.130' identified by '123456'; Query OK, 0 rows affected (0.06 sec)mysql> grant replication slave on *.* to 'repl'@'192.168.24.130'; Query OK, 0 rows affected (0.01 sec)mysql> flush privileges; Query OK, 0 rows affected (0.03 sec)給主數(shù)據(jù)庫上讀鎖
另開一個終端,給數(shù)據(jù)庫上讀鎖,避免在備份期間有其他人在寫入導致數(shù)據(jù)同步的不一致
mysql> flush tables with read lock; Query OK, 0 rows affected (0.17 sec) 此鎖表的終端必須在備份完成以后才能退出(退出鎖表失效)在從數(shù)據(jù)庫服務器操作如下
編輯配置文件
[root@linfan ~]# cat /etc/my.cnf [mysqld] basedir = /usr/local/mysql datadir = /opt/data socket = /tmp/mysql.sock port = 3306 pid-file = /opt/data/mysql.pid user = mysql skip-name-resolve # GTID gtid_mode=on enforce_gtid_consistency=on server_id=130 # binlog log-bin=slave-binlog log-slave-updates=1 binlog-format=row //強烈建議,其他格式可能導致數(shù)據(jù)不一致 # relay log skip-slave-start=1重啟mysql服務器
[root@linfan ~]# service mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL... SUCCESS!配置并啟動主從復制
mysql> change master to-> master_host='192.168.24.128',-> master_user='repl',-> master_password='123456',-> master_port=3306,-> master_auto_position= 1 ; Query OK, 0 rows affected, 2 warnings (0.01 sec)mysql> start slave; Query OK, 0 rows affected (0.01 sec)查看從服務器的狀態(tài):
mysql> show slave status\G; *************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 192.168.24.128Master_User: replMaster_Port: 3306Connect_Retry: 60Master_Log_File: master-binlog.000001Read_Master_Log_Pos: 154Relay_Log_File: linfan-relay-bin.000002Relay_Log_Pos: 375Relay_Master_Log_File: master-binlog.000001Slave_IO_Running: Yes //此處必須為yesSlave_SQL_Running: Yes //此處必須為yesReplicate_Do_DB:Replicate_Ignore_DB:Replicate_Do_Table:Replicate_Ignore_Table:Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table:Last_Errno: 0Last_Error:Skip_Counter: 0Exec_Master_Log_Pos: 154Relay_Log_Space: 583Until_Condition: NoneUntil_Log_File:Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_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: NoLast_IO_Errno: 0Last_IO_Error:Last_SQL_Errno: 0Last_SQL_Error:Replicate_Ignore_Server_Ids:Master_Server_Id: 128Master_UUID: 3396fe35-b1e1-11e8-81bb-000c292340f6Master_Info_File: /opt/data/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind:Last_IO_Error_Timestamp:Last_SQL_Error_Timestamp:Master_SSL_Crl:Master_SSL_Crlpath:Retrieved_Gtid_Set:Executed_Gtid_Set:Auto_Position: 1Replicate_Rewrite_DB:Channel_Name:Master_TLS_Version: 1 row in set (0.00 sec)解除主庫的鎖表狀態(tài),直接退出交互式界面即可
mysql> quit Bye驗證:
在主數(shù)據(jù)庫創(chuàng)建一個庫doudou
mysql> create database doudou; Query OK, 1 row affected (0.01 sec)mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | doudou | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.03 sec)在從數(shù)據(jù)庫服務器上查看是否同步存在doudou庫
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | doudou | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.01 sec)轉載于:https://blog.51cto.com/13858192/2171750
總結
以上是生活随笔為你收集整理的配置MYSQL基于GTID 主从复制详细解析及步骤的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 面向对象简述--对象、引用、指针
- 下一篇: python基础学习11----函数