標籤:blog http 使用 os io strong 檔案 資料
---本文大綱
一、什麼是GTID
二、應用情境
三、多線程複製說明
四、實現過程
五、多源複製原理
六、實現過程
----------------------------------
一、什麼是GITD
自MySQL 5.6引入的GTID(Global Transaction IDs)使得其複製功能的配置、監控及管理變得更加易於實現,且更加健壯。官方文檔在這篇文檔裡,我們可以知道全域事務 ID 的官方定義是:GTID = source_id:transaction_id
gtid是一個 unique 唯一的表示符,他是由伺服器的uuid 全域唯一標識,是由128位的隨機符組成,mysql-5.6是依靠server-id和uuid 來標識複製架構中的每一個主機,因為是128位的隨機字串在全域都不會重複,server-id 和uuid以及每一個mysql事物的事物序號組成了唯一的gtid ,自從引進mysql-5.6之後每一個二進位日誌當中在每一個事物的首部都會寫上gtid 標記,因此gtid使得追蹤和比較複製事物變得非常簡單而且能夠實現從崩潰中快速恢複。尤其是innodb 引擎要想實現高可用功能必須要藉助於gtid來實現。
二、應用情境
當使用mariadb實現高可用時,GTID是顯得尤為重要,例如,在一個高可用環境中,一主多從的模式下,當主庫宕機後(寫延遲),叢集資源管理員可以在節點列表中的多個從庫中任選一個提升為主庫,而不會影響到業務本身,而其它的從庫將會以新提升起來為主庫為當前叢集中的主庫,以後將從這個庫上複製同步;雖然這樣做完成了“瞬間”業務切換,但可能在主庫未宕機之前,多個從庫的複製同步落後於主庫,這樣一來,當主庫宕機後,叢集資源管理員正好切換到一個與自己事務提交可能不一致的從庫(B)上,當有應用讀取之前己在宕機的主庫中提交的事務時,發現現在的主庫沒有,這樣就會出現事務不一致,而GTID就能很好的解決這個問題。首先當一個從庫被提升為一個主庫時,那麼之前指向原來主庫的從庫將全部指向新提升的主庫,來進行之後的複製,而後,剛提升的主庫B會整合所有從庫中己完成的的事務,來添補自己缺少的部分,而B從那裡知道,缺少的是那一個事務,有那麼多的事務,這個事務是那一個呢,這就是通過GTID來標識的,因為GTID標識了來原始伺服器的標識與第多少個事務(Source_id:transaction_id),每一個改變是事件都會與GTID相關連起來記錄於binlog日誌中以供其它從庫同步,從而做到全域唯一的標識。
註:
在多級複製中GTID是不會改變的。
在GTID中,如果主伺服器中有多個資料庫,要實現多線程複製是靠I/O複製到從庫的中繼日誌中由多個SQL thread來進行應用於本地的。
三、多線程複製說明
MySQL 5.6之前的版本,同步複製是單線程的,隊列的,只能一個一個執行,在5.6裡,可以做到多個庫之間的多線程複製,例如資料庫裡,存放著使用者表,商品表,價格表,訂單表,那麼將每個業務表單獨放在一個庫裡,這時就可以做到多線程複製,但一個庫裡的表,多線程複製是無效的。
註:
每個資料庫僅能使用一個線程,複製涉及到多個資料庫時多線程複製才有意義;
同一個庫的事務複製,就必須按先後順序複製。
在複製模型中,同一個線程(I/O線程)可以對多個資料庫提供服務。
四、實現過程
註:
此處僅是實現GTID複製。
1、資源分派
| 服務類型 |
版本類型 |
ip地址 |
OS
|
Centos6.5x86_64 |
無 |
| Mariadb Master |
10.0.10-MariaDB-log Source distribution |
192.168.1.122/24
|
| Mariadb Slave |
10.0.10-MariaDB-log Source distribution |
192.168.1.210/24 |
2、Mariadb Master配置清單
⑴、編輯設定檔/etc/my.cnf
[client]port = 3306socket = /tmp/maria.sock[mysqld]port = 3306socket = /tmp/maria.sockskip-external-lockingkey_buffer_size = 256Mmax_allowed_packet = 1Mtable_open_cache = 256sort_buffer_size = 1Mread_buffer_size = 1Mread_rnd_buffer_size = 4Mmyisam_sort_buffer_size = 64Mthread_cache_size = 8query_cache_size= 16Mthread_concurrency = 4datadir=/mydata/datalog-bin=mysql-binbinlog_format=rowserver-id=10log-slave-update =Truemaster-info-repository=TABLErelay-log-info-repository=TABLEsync-master-info=1slave-parallel-threads=2master-verify-checksum=CRC32slave-sql-verify-checksum=1binlog-rows-query-log-events=1report-port=3306report-host=master.mysql.com[mysqldump]quickmax_allowed_packet = 16M[mysql]no-auto-rehash[myisamchk]key_buffer_size = 128Msort_buffer_size = 128Mread_buffer = 2Mwrite_buffer = 2M[mysqlhotcopy]interactive-timeout
註解:
binlog-format
二進位日誌的格式,有row、statement和mixed幾種類型;
當設定隔離等級為READ-COMMITED必須設定二進位日誌格式為ROW,現在MySQL官方認為STATEMENT這個已經不再適合繼續使用;但mixed類型在預設的交易隔離等級下,可能會導致主從資料不一致;
log-slave-updates、report-port和report-host
用於啟動GTID及滿足附屬的其它需求;
master-info-repository和relay-log-info-repository
啟用此兩項,可用於實現在崩潰時保證二進位及從伺服器安全的功能;
sync-master-info
啟用之可確保無資訊丟失;
slave-paralles-threads
設定從伺服器的SQL線程數;0表示關閉多線程複製功能;
binlog-checksum、master-verify-checksum和slave-sql-verify-checksum
啟用複製有關的所有校正功能;
binlog-rows-query-log-events
啟用之可用於在二進位日誌記錄事件相關的資訊,可降低故障排除的複雜度;
log-bin
啟用二進位日誌,這是保證複製功能的基本前提;
server-id
同一個複寫拓撲中的所有伺服器的id號必須惟一
log_slave_updates
記錄從伺服器的更新
⑵、授權複製節點及使用者
MariaDB [(none)]> GRANT REPLICATION SLAVE ON*.* TO [email protected] IDENTIFIED BY ‘replpass‘;Query OK, 0 rows affected (0.01 sec)MariaDB [(none)]> flush privileges -> ;Query OK, 0 rows affected (0.00 sec)
⑶、備份Master節點的資料,同步於slave(此操作並非必須的,如果Master上有大量資料需要同步到新的Slave上,Slave是新上線的此操作才有意義)
# mysqldump -uroot -hmaster.mysql.com --all-databases --lock-all-tables --flush-logs --master-data=2 > /tmp/all.sql#scp /tmp/all.sql 192.168.1.210:/tmp
⑷、查看一下Master GTID標識
MariaDB [(none)]> show variables like "%gtid%" -> ;+------------------------+----------+| Variable_name | Value |+------------------------+----------+| gtid_binlog_pos | 0-1-2130 || gtid_binlog_state | 0-1-2130 || gtid_current_pos | 0-1-2130 || gtid_domain_id | 0 || gtid_ignore_duplicates | OFF || gtid_seq_no | 0 || gtid_slave_pos | || gtid_strict_mode | OFF || last_gtid | 0-1-2130 |+------------------------+----------+9 rows in set (0.00 sec)
3、Mariadb Slave配置清單
⑴、編輯設定檔/etc/my.cnf
[client]port = 3306socket = /tmp/maria.sock[mysqld]port = 3306socket = /tmp/maria.sockskip-external-lockingkey_buffer_size = 256Mmax_allowed_packet = 1Mtable_open_cache = 256sort_buffer_size = 1Mread_buffer_size = 1Mread_rnd_buffer_size = 4Mmyisam_sort_buffer_size = 64Mthread_cache_size = 8query_cache_size= 16Mthread_concurrency = 4datadir=/mydata/datalog-bin=mysql-binrelay-log=relay-log-binserver-id=20binlog-format=ROWlog-slave-updates=truemaster-info-repository=TABLErelay-log-info-repository=TABLEsync-master-info=1slave-parallel-threads=2binlog-checksum=CRC32master-verify-checksum=1slave-sql-verify-checksum=1binlog-rows-query-log_events=1report-port=3306report-host=slave.mysql.com[mysqldump]quickmax_allowed_packet = 16M[mysql]no-auto-rehash[myisamchk]key_buffer_size = 128Msort_buffer_size = 128Mread_buffer = 2Mwrite_buffer = 2M[mysqlhotcopy]interactive-timeout
註:
除了server-id,其它的都與主庫一樣。
⑵、應用完全備份
# mysql /all.sql
⑶、串連Master
MariaDB [(none)]> change master to master_host=‘192.168.1.122‘,master_user=‘repluser‘,master_password=‘replpass‘,master_use_gtid=current_pos;
⑷、看一下效果
⑸、測試一下
在Master建立一個資料庫,建立一張測試表
MariaDB [(none)]> use yydbDatabase changedMariaDB [yydb]> create table t1 (id int(4) not null,name char(30) not null);Query OK, 0 rows affected (0.03 sec)
在Slave的日誌查看一下,是否含有GTID標識
# at 793#140419 23:40:27 server id 1 end_log_pos 726 CRC32 0x7d8f09f0 GTID 0-1-2132/*!100001 SET @@session.gtid_seq_no=2132*//*!*/;# at 835#140419 23:40:27 server id 1 end_log_pos 852 CRC32 0x4b60d93e Query thread_id=4 exec_time=0 error_code=0use `yydb`/*!*/;SET TIMESTAMP=1397922027/*!*/;create table t1 (id int(4) not null,name char(30) not null)/*!*/;
註:
如果Master與Slave的GTID標識不符,就無法實現基於GTID的複製,只要將Slave庫中的GTID修改與Master一致就可以了
查看GTID標識的語句是
MariaDB [yydb]> show global variables like ‘gtid_current_pos‘;
修改GTID的語句是
MariaDB [(none)]> SET GLOBAL gtid_slave_pos =‘Master_GTID‘;
=================================基於GTID複製到此結束====================================
五、多源複製原理
首先,我們需要清楚 multi-master 與multi-source 複製不是一樣的. Multi-Master 複製通常是環形複製,你可以在任意主機上將資料複製給其他主機。
Multi-source 是不同的. MySQL5.7版本中修複了一個複製限制 , 這限制是一個從站只能有一個主站. 這是一個在我們設計複製環境中的限制因素,也有一些極客使它正常工作了。但是現在有一個官方的解決辦法了。所以。簡單的說, Multi-Source 意味著一個從站能有一個以上主站. 現在, 像一樣的複製環境是可能的:
這將協助我們建立一些複製的階層,這在過去是不可能的。 舉個例子,你可以 將一個從站放在你的辦公室裡。在辦公室裡從所有主站中複製資料傳播到世界各地。
而在微觀上說,各Master開啟一個binlog dump線程通知Slave端,由Slave開啟多個IO threads到各Master端上複製二進位日誌並記錄到本地的relay日誌中,由本地的SQL thread在讀取出來並在本地應用。從而實現複製功能。
六、配置過程
1、資源分派
| 服務類型 |
版本類型 |
IP地址 |
| OS |
Centos6.5x86_64 |
無 |
| Master 1 |
10.0.10-MariaDB-log Source distribution |
192.168.1.109 |
| Master 2 |
10.0.10-MariaDB-log Source distribution |
192.168.1.111 |
| Slave |
10.0.10-MariaDB-log Source distribution |
192.168.1.110 |
2、在各Master上開啟二進位日誌,授權複製使用者
⑴、Master 1
編輯設定檔
[mysqld]port = 3306socket = /tmp/mysql.sockskip-external-lockingkey_buffer_size = 256Mmax_allowed_packet = 1Mtable_open_cache = 256sort_buffer_size = 1Mread_buffer_size = 1Mread_rnd_buffer_size = 4Mmyisam_sort_buffer_size = 64Mthread_cache_size = 8query_cache_size= 16Mthread_concurrency = 4datadir=/mariadb/dataserver-id= 10log-bin=mysql-binbinlog-format=ROW
註:
binlog-format日誌格式使用預設就可以
只需要修改[mysqld]地區
授權複製使用者
MariaDB [(none)]> grant replication slave,replication client on *.* to ‘repluser‘@‘192.168.1.110‘ identified by ‘replpass‘;Query OK, 0 rows affected (0.00 sec)
⑵、Master 2
編輯設定檔
#vim /etc/my.cnf[mysqld]port = 3306socket = /tmp/maria.sockskip-external-lockingkey_buffer_size = 256Mmax_allowed_packet = 1Mtable_open_cache = 256sort_buffer_size = 1Mread_buffer_size = 1Mread_rnd_buffer_size = 4Mmyisam_sort_buffer_size = 64Mthread_cache_size = 8query_cache_size= 16Mthread_concurrency = 4datadir=/mydata/datalog-bin=mysql-binserver-id= 20binlog-format=ROW
註:
binlog-format日誌格式使用預設就可以
只需要修改[mysqld]地區
授權複製使用者
MariaDB [(none)]> grantreplication slave,replication client on*.* to‘repluser‘@‘192.168.1.110‘identified by‘replpass‘;Query OK, 0 rowsaffected (0.00 sec)
⑶、Slave
串連兩個Master
Master 1
MariaDB [(none)]> change master ‘Master1‘ to master_host=‘192.168.1.109‘,master_user=‘repluser‘,master_password=‘replpass‘,master_log_file=‘mysql-bin.000006‘,master_log_pos=326;
查看Slave的狀態
MariaDB [(none)]> show slave ‘Master1‘ status\G*************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.109 Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000007 Read_Master_Log_Pos: 326 Relay_Log_File: master-relay-bin-master1.000009 Relay_Log_Pos: 535 Relay_Master_Log_File: mysql-bin.000007 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: 326 Relay_Log_Space: 1128 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: 0Master_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: 10 Master_SSL_Crl: Master_SSL_Crlpath: Using_Gtid: No Gtid_IO_Pos:1 row in set (0.00 sec)
Master 2
MariaDB [(none)]> change master ‘Master2‘ to master_host=‘192.168.1.111‘,master_user=‘repluser‘,master_password=‘replpass‘,master_log_file=‘mysql-bin.000010‘,master_log_pos=342;
查看Slave的狀態
MariaDB [(none)]> show slave ‘Master2‘ status\G*************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.111 Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000010 Read_Master_Log_Pos: 342 Relay_Log_File: master-relay-bin-master2.000002 Relay_Log_Pos: 535 Relay_Master_Log_File: mysql-bin.000010 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: 342 Relay_Log_Space: 841 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: 0Master_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: 20 Master_SSL_Crl: Master_SSL_Crlpath: Using_Gtid: No Gtid_IO_Pos:1 row in set (0.00 sec)
⑷、測試一下
在Master1建立一個資料庫,在Master2也建立一個資料庫
Master 1
MariaDB [(none)]> show variables like ‘server_id‘;+---------------+-------+| Variable_name | Value |+---------------+-------+| server_id | 10 |+---------------+-------+1 row in set (0.00 sec)MariaDB [(none)]> create database m1db;Query OK, 1 row affected (0.01 sec)MariaDB [(none)]> show databases;+--------------------+| Database |+--------------------+| information_schema || jjdb || m1db || mysql || performance_schema || test || yydb |+--------------------+7 rows in set (0.00 sec)
Master 2
MariaDB [(none)]> show variables like ‘server_id‘;+---------------+-------+| Variable_name | Value |+---------------+-------+| server_id | 20 |+---------------+-------+1 row in set (0.00 sec)MariaDB [(none)]> create database m2db;Query OK, 1 row affected (0.00 sec)MariaDB [(none)]> show databases;+--------------------+| Database |+--------------------+| information_schema || m2db || mysql || performance_schema || test || xxdb |+--------------------+6 rows in set (0.00 sec)MariaDB [(none)]>
在Slave端顯示結果為
MariaDB [(none)]> show databases;+--------------------+| Database |+--------------------+| information_schema || m1db || m2db || mysql || performance_schema || test || xxdb |+--------------------+7 rows in set (0.00 sec)
註:
在此實驗中多源複製各Master中不能有同名庫,否則複製將失敗。
===========================================完=====================================
本文出自 “和風細雨” 部落格,請務必保留此出處http://essun.blog.51cto.com/721033/1398848