CentOS 7.2部署MariaDB Galera Cluster(10.1.21-MariaDB

MariaDB Galera Cluster 介紹

Galera Cluster是由第三方公司Codership所研發的一套免費開源的集羣高可用方案,實現了數據零丟失,官網地址爲http://galeracluster.com/。其在MySQLInnoDB存儲引擎基礎上打了wrep(虛擬全同步複製),Percona/MariaDB已捆綁在各自的發行版本中。

 

MariaDB Galera Cluster是MariaDB同步多主機集羣。它僅支持XtraDB/InnoDB存儲引擎(雖然有對MyISAM實驗支持,具體看wsrep_replicate_myisam系統變量)。

 

MariaDB Galera Cluster主要功能:

l  同步複製

l  真正的multi-master,即所有節點可以同時讀寫數據庫

l  自動的節點成員控制,失效節點自動被清除

l  新節點加入數據自動複製

l  真正的並行複製,行級

l  用戶可以直接連接集羣,使用感受上與MySQL完全一致

 

優勢:

l  因爲是多主,所以不存在Slavelag(延遲)

l  不存在丟失事務的情況

l  同時具有讀和寫的擴展能力

l  更小的客戶端延遲

l  節點間數據是同步的,而Master/Slave模式是異步的,不同slave上的binlog可能是不同的

 

缺點:

l  加入新節點時開銷大,需要複製完整的數據

l  不能有效地解決寫擴展的問題,所有的寫操作都發生在所有的節點

l  有多少個節點,就有多少份重複的數據

l  由於事務提交需要跨節點通信,即涉及分佈式事務操作,因此寫入會比主從複製慢很多,節點越多,寫入越慢,死鎖和回滾也會更加頻繁

l  對網絡要求比較高,如果網絡出現波動不穩定,則可能會造成兩個節點失聯,Galera Cluster集羣會發生腦裂,服務將不可用

 

還有一些地方存在侷限:

l  僅支持InnoDB/XtraDB存儲引擎,任何寫入其他引擎的表,包括mysql.*表都不會被複制。但是DDL語句可以複製,但是insert into mysql.user(MyISAM存儲引擎)之類的插入數據不會被複制

l  Delete操作不支持沒有主鍵的表,因爲沒有主鍵的表在不同的節點上的順序不同,如果執行select … limit …將出現不同的結果集

l  LOCK/UNLOCK TABLES/FLUSH TABLES WITH READ LOCKS不支持單表所鎖,以及鎖函數GET_LOCK()、RELEASE_LOCK(),但FLUSH  TABLES WITH READ LOCK支持全局表鎖

l  General Query Log日誌不能保存在表中,如果開始查詢日誌,則只能保存到文件中

l  不能有大事務寫入,不能操作wsrep_max_ws_rows=131072(行),且寫入集不能超過wsrep_max_ws_size=1073741824(1GB),否則客戶端直接報錯

l  由於集羣是樂觀鎖併發控制,因此,在commit階段會有事務衝突發生。如果兩個事務在集羣中的不同節點上對同一行寫入並提交,則失敗的節點將回滾,客戶端返回死鎖報錯

l  XA分佈式事務不支持Codership Galera Cluster,在提交時可能會回滾

l  整個集羣的寫入吞吐量取決於最弱的節點限制,集羣要使用同一的配置

 

技術:

Galera集羣的複製功能是基於認證的複製,其流程如下:


當客戶端發出一個commit的指令,在事務被提交之前,所有對數據庫的更改都會被write-set收集起來,並且將write-set 記錄的內容發送給其他節點。

write-set 將在每個節點上使用搜索到的主鍵進行確認性認證測試,測試結果決定着節點是否應用write-set更改數據。如果認證測試失敗,節點將丟棄 write-set ;如果認證測試成功,則事務提交,工作原理如下圖:


 

關於新節點的加入,流程如下:


新加入的節點叫做Joiner,給Joiner提供複製的節點叫Donor。在該過程中首先會檢查本地grastate.dat文件的seqno事務號是否在遠端donor節點galera.cache文件裏,如果存在,那麼進行Incremental State Transfer(IST)增量同步複製,將剩餘的事務發送過去;如果不存在那麼進行State Snapshot Transfer(SST)全量同步複製。SST有三種全量拷貝方式:mysqldump、rsync和xtrabackup。SST的方法可以通過wsrep_sst_method這個參數來設置。

 


備註:

SST是指從donor到joiner的數據全量拷貝,它通常使用在一個新的節點加入時,爲了與集羣同步,新的節點不得不去一個已經在集羣中的節點上拷貝數據,在PXC(Percona Xtradb Cluster)中,有三種SST的方法,mysqldump,rsync,Xtrabackup。

 

建議使用XtraBackup,另外對XtraBackup補充說明:

在XtraBackup 2.1.x版本里,使用innobackupex備份時,備份流程如下:

1.      備份InnoDB表數據

2.      執行全局表讀鎖FLUSH TABLES WITH READ LOCKS

3.      拷貝.frm和MyISAM表數據

4.      得到當前的binlog文件名和position點

5.      完成redo log事務日誌的後臺複製

6.      解鎖UNLOCK TABLES

 

由上面可以看出如果備份好幾張MyISAM存儲的大表時,將會進行鎖表。

環境信息

MariaDB Server:10.1.21-MariaDB

CentOS:CentOS Linux release7.2.1511 (Core)

 

MariaDB Galera Cluster 三個集羣節點主機名和IP地址信息:

192.168.1.104  mariadb-a03

192.168.1.105  mariadb-a04

192.168.1.106  mariadb-a05

 

 

環境準備

1.      配置hosts文件


  1. # cat /etc/hosts  

  2. 127.0.0.1 localhost.localdomain localhost  

  3. 192.168.1.104 mariadb-a03  

  4. 192.168.1.105 mariadb-a04  

  5. 192.168.1.106 mariadb-a05  



2.      /etc/security/limits.conf



  1. * soft nofile 65536  

  2. * hard nofile 65536  



3.      /etc/sysctl.conf


  1. fs.file-max=655350  

  2. net.ipv4.ip_local_port_range = 1025 65000  

  3. net.ipv4.tcp_tw_recycle = 1  




 

最後執行:

# sysctl -p

 

4.      安裝Percona XtraBackup熱備份工具

下載地址:

https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.6/binary/tarball/percona-xtrabackup-2.4.6-Linux-x86_64.tar.gz

 

解壓縮:

  1. # tar -zxvf percona-xtrabackup-2.4.6-Linux-x86_64.tar.gz  




 

拷貝腳本到指定位置:

  1. # cd percona-xtrabackup-2.4.6-Linux-x86_64/bin/  

  2. # cp -a * /usr/bin/  


安裝依賴的一些包,比如lsof,socat,openssl,tar等

 

創建XtraBackup備份時用的用戶名和密碼:

  1. MariaDB [(none)]> grant all on *.* to 'galera'@'localhost' identified by '123456';  




 

部署MariaDB

從MariaDB 10.1版本開始,Galera Cluster就已經包含在MariaDB包裏面了,不需要單獨部署MariaDB-Galera-server 和galera 包。

 MariaDB 下載地址: https://downloads.mariadb.org/mariadb/+releases/

這裏演示使用YUM方式部署MariaDB Galera Cluster。

步驟一:配置Yum源(192.168.1.104,192.168.1.105,192.168.1.106)


  1. # touch /etc/yum.repos.d/MariaDB-IDC.repo  

  2. 添加如下內容:  

  3. [mariadb]  

  4. name = MariaDB  

  5. baseurl =http://yum.mariadb.org/10.1/centos7-amd64  

  6. gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB  

  7. gpgcheck=1  




 

由於我們的環境無法訪問外網,單獨部署一個MariaDB的Yum源:


  1. # cat /etc/yum.repos.d/MariaDB-IDC.repo  

  2. [MariaDB-10.1-IDC]  

  3. name=MariaDB-10.1-IDC  

  4. baseurl=http://192.168.1.100/repo/yum.mariadb.org/10.1/centos7-amd64  

  5. gpgcheck=0  

  6. enabled=1  




 

步驟二:安裝MariaDB(192.168.1.104,192.168.1.105,192.168.1.106)

  1. # yum install MariaDB-server MariaDB-client galera  -y  




 

配置 MariaDB Galera Cluster

下面我們開始配置MariaDB Galera Cluster,分別修改MariaDB Galera集羣的每個節點上的/etc/my.cnf.d/server.cnf文件,具體每個節點的內容如下:

 

1.      192.168.1.104節點的/etc/my.cnf.d/server.cnf文件內容:

  1. [root@mariadb-a03 ~]# cat /etc/my.cnf.d/server.cnf | grep -v "#" | grep -v "^$"  

  2. [server]  

  3. [mysqld]  

  4. server_id=128  

  5. datadir=/app/galera  

  6. user=mysql  

  7. skip-external-locking  

  8. skip-name-resolve  

  9. character-set-server=utf8  

  10.    

  11. [galera]  

  12. wsrep_causal_reads=ON  #節點應用完事務才返回查詢請求  

  13. wsrep_provider_options="gcache.size=4G"#同步複製緩衝池  

  14. wsrep_certify_nonPK=ON   #爲沒有顯式申明主鍵的表生成一個用於certificationtest的主鍵,默認爲ON  

  15. #log-bin=/app/galera/mysql-bin  #如果不接從庫,註釋掉  

  16. #log_slave_updates=1         #如果不接從庫,註釋掉  

  17. query_cache_size=0           #關閉查詢緩存  

  18. wsrep_on=ON   #開啓全同步複製模式  

  19. wsrep_provider=/usr/lib64/galera/libgalera_smm.so#galera library  

  20. wsrep_cluster_name=MariaDB-Galera-Cluster  

  21. wsrep_cluster_address="gcomm://192.168.1.104,192.168.1.105,192.168.1.106"  #galera cluster URL  

  22. wsrep_node_name=mariadb-a03  

  23. wsrep_node_address=192.168.1.104  

  24. binlog_format=row  

  25. default_storage_engine=InnoDB  

  26. innodb_autoinc_lock_mode=2   #主鍵自增模式修改爲交叉模式  

  27. wsrep_slave_threads=8  #開啓並行複製線程,根據CPU核數設置  

  28. innodb_flush_log_at_trx_commit=0   #事務提交每隔1秒刷盤  

  29. innodb_buffer_pool_size=2G  

  30. wsrep_sst_method=rsync  

  31. [embedded]  

  32. [mariadb]  

  33. [mariadb-10.1]  




 

# 上面配置使用的是rsync方式同步數據,如果要使用xtrabackup方式(建議使用),需要設置:

wsrep_sst_auth=galera:123456

wsrep_sst_method=xtrabackup-v2 #默認是rsync全量拷貝,但是需要在donor節點上執行全局讀鎖(flushtables with read lock),建議採用xtrabackup熱備份方式,只有在備份.frm表結構文件纔會鎖表

 

2.      192.168.1.105節點的/etc/my.cnf.d/server.cnf文件內容:

  1. [root@mariadb-a04 ~]# cat /etc/my.cnf.d/server.cnf | grep -v "#" | grep -v "^$"  

  2. [server]  

  3. [mysqld]  

  4. server_id=129  

  5. datadir=/app/galera  

  6. user=mysql  

  7. skip-external-locking  

  8. skip-name-resolve  

  9. character-set-server=utf8  

  10.    

  11. [galera]  

  12. wsrep_causal_reads=ON  

  13. wsrep_provider_options="gcache.size=4G"  

  14. wsrep_certify_nonPK=ON  

  15. query_cache_size=0  

  16. wsrep_on=ON  

  17. wsrep_provider=/usr/lib64/galera/libgalera_smm.so  

  18. wsrep_cluster_name=MariaDB-Galera-Cluster  

  19. wsrep_cluster_address="gcomm://192.168.1.104,192.168.1.105,192.168.1.106"  

  20. wsrep_node_name=mariadb-a04  

  21. wsrep_node_address=192.168.1.105  

  22. binlog_format=row  

  23. default_storage_engine=InnoDB  

  24. innodb_autoinc_lock_mode=2  

  25. wsrep_slave_threads=8  

  26. innodb_flush_log_at_trx_commit=0  

  27. innodb_buffer_pool_size=2G  

  28. wsrep_sst_method=rsync  

  29. [embedded]  

  30. [mariadb]  

  31. [mariadb-10.1]  

3.      192.168.1.106節點的/etc/my.cnf.d/server.cnf文件內容:

  1. [root@mariadb-a05 yum.repos.d]# cat /etc/my.cnf.d/server.cnf | grep -v "#" | grep -v "^$"  

  2. [server]  

  3. [mysqld]  

  4. server_id=130  

  5. datadir=/app/galera  

  6. user=mysql  

  7. skip-external-locking  

  8. skip-name-resolve  

  9. character-set-server=utf8  

  10.    

  11. [galera]  

  12. wsrep_causal_reads=ON  

  13. wsrep_provider_options="gcache.size=4G"  

  14. wsrep_certify_nonPK=ON  

  15. query_cache_size=0  

  16. wsrep_on=ON  

  17. wsrep_provider=/usr/lib64/galera/libgalera_smm.so  

  18. wsrep_cluster_name=MariaDB-Galera-Cluster  

  19. wsrep_cluster_address="gcomm://192.168.1.104,192.168.1.105,192.168.1.106"  

  20. wsrep_node_name=mariadb-a05  

  21. wsrep_node_address=192.168.1.106  

  22. binlog_format=row  

  23. default_storage_engine=InnoDB  

  24. innodb_autoinc_lock_mode=2  

  25. wsrep_slave_threads=8  

  26. innodb_flush_log_at_trx_commit=0  

  27. innodb_buffer_pool_size=2G  

  28. wsrep_sst_method=rsync  

  29. [embedded]  

  30. [mariadb]  

  31. [mariadb-10.1]  


MariaDB一個節點初始化安裝(192.168.1.104):

  1. # mysql_install_db --defaults-file=/etc/my.cnf.d/server.cnf --user=mysql  


在192.168.1.104節點上通過bootstrap啓動(第一次啓動一定要使用--wsrep-new-cluster,再次啓動就不需要)

# mysqld_safe --defaults-file=/etc/my.cnf.d/server.cnf --user=mysql  --wsrep-new-cluster &  


在192.168.1.104節點上設置root密碼以及安全設置(192.168.1.104,192.168.1.105,192.168.1.106)

  1. /usr/bin/mysql_secure_installation  

  2. 或  

  3. mysql_secure_installation  

 

在192.168.1.105,192.168.1.106節點啓動MariaDB:

mysqld_safe --defaults-file=/etc/my.cnf.d/server.cnf --user=mysql  &  


驗證操作

登錄三個節點查看

  1. 192.168.1.104節點:  

  2. MariaDB [(none)]> SHOW STATUS LIKE 'wsrep_cluster_size';  

  3. +--------------------+-------+  

  4. | Variable_name      | Value |  

  5. +--------------------+-------+  

  6. | wsrep_cluster_size | 3     |  

  7. +--------------------+-------+  

  8. 1 row in set (0.00 sec)  

  9.    

  10. MariaDB [(none)]> show global status like 'ws%';  

  11. +------------------------------+-------------------------------------------------------------+  

  12. | Variable_name                | Value                                                      |  

  13. +------------------------------+-------------------------------------------------------------+  

  14. | wsrep_apply_oooe             | 0.000000                                                   |  

  15. | wsrep_apply_oool             | 0.000000                                                   |  

  16. | wsrep_apply_window           | 1.000000                                                    |  

  17. | wsrep_causal_reads           | 11                                                         |  

  18. | wsrep_cert_deps_distance     | 1.000000                                                   |  

  19. | wsrep_cert_index_size        | 2                                                           |  

  20. | wsrep_cert_interval          | 0.000000                                                   |  

  21. | wsrep_cluster_conf_id        | 3                                                          |  

  22. | wsrep_cluster_size           | 3                                                          |  

  23. | wsrep_cluster_state_uuid     |3108c722-ff29-11e6-a31f-bb500598d033                        |  

  24. | wsrep_cluster_status         | Primary                                                     |  

  25. | wsrep_commit_oooe            | 0.000000                                                   |  

  26. | wsrep_commit_oool            | 0.000000                                                   |  

  27. | wsrep_commit_window          | 1.000000                                                    |  

  28. | wsrep_connected              | ON                                                         |  

  29. | wsrep_desync_count           | 0                                                          |  

  30. | wsrep_evs_delayed            |                                                            |  

  31. | wsrep_evs_evict_list         |                                                            |  

  32. | wsrep_evs_repl_latency       | 0/0/0/0/0                                                   |  

  33. | wsrep_evs_state              | OPERATIONAL                                                |  

  34. | wsrep_flow_control_paused    | 0.000000                                                   |  

  35. | wsrep_flow_control_paused_ns | 0                                                           |  

  36. | wsrep_flow_control_recv      | 0                                                          |  

  37. | wsrep_flow_control_sent      | 0                                                          |  

  38. | wsrep_gcomm_uuid             |3107a278-ff29-11e6-96d3-374133af7e21                        |  

  39. | wsrep_incoming_addresses     | 192.168.1.105:3306,192.168.1.106:3306,192.168.1.104:3306|  

  40. | wsrep_last_committed         | 3                                                           |  

  41. | wsrep_local_bf_aborts        | 0                                                          |  

  42. | wsrep_local_cached_downto    | 1                                                          |  

  43. | wsrep_local_cert_failures    | 0                                                           |  

  44. | wsrep_local_commits          | 0                                                          |  

  45. | wsrep_local_index            | 2                                                          |  

  46. | wsrep_local_recv_queue       | 0                                                          |  

  47. | wsrep_local_recv_queue_avg   | 0.000000                                                   |  

  48. | wsrep_local_recv_queue_max   | 1                                                          |  

  49. | wsrep_local_recv_queue_min   | 0                                                          |  

  50. | wsrep_local_replays          | 0                                                          |  

  51. | wsrep_local_send_queue       | 0                                                           |  

  52. | wsrep_local_send_queue_avg   | 0.000000                                                   |  

  53. | wsrep_local_send_queue_max   | 1                                                          |  

  54. | wsrep_local_send_queue_min   | 0                                                          |  

  55. | wsrep_local_state            | 4                                                          |  

  56. | wsrep_local_state_comment    | Synced                                                     |  

  57. | wsrep_local_state_uuid       |3108c722-ff29-11e6-a31f-bb500598d033                        |  

  58. | wsrep_protocol_version       | 7                                                          |  

  59. | wsrep_provider_name          | Galera                                                      |  

  60. | wsrep_provider_vendor        | Codership Oy<[email protected]>                           |  

  61. | wsrep_provider_version       | 25.3.19(r3667)                                              |  

  62. | wsrep_ready                  | ON                                                         |  

  63. | wsrep_received               | 10                                                         |  

  64. | wsrep_received_bytes         | 806                                                        |  

  65. | wsrep_repl_data_bytes        | 1044                                                       |  

  66. | wsrep_repl_keys              | 3                                                          |  

  67. | wsrep_repl_keys_bytes        | 93                                                          |  

  68. | wsrep_repl_other_bytes       | 0                                                          |  

  69. | wsrep_replicated             | 3                                                          |  

  70. | wsrep_replicated_bytes       | 1329                                                        |  

  71. | wsrep_thread_count           | 9                                                          |  

  72. +------------------------------+-------------------------------------------------------------+  

  73. 58 rows in set (0.00 sec)  




 

註釋:

wsrep_cluster_status爲Primary,表示節點爲主節點,正常讀寫。

wsrep_ready爲ON,表示集羣正常運行。

wsrep_cluster_size爲3,表示集羣有三個節點。

 

創建數據庫測試

  1. 192.168.1.104節點:  

  2. [root@mariadb-a03 my.cnf.d]# mysql -uroot –pxxxxxx  

  3. MariaDB [(none)]> create databasetest_db;  

  4. Query OK, 1 row affected (0.01 sec)  



  1. 192.168.1.105節點查看:  

  2. [root@mariadb-a04 my.cnf.d]# mysql -uroot -pxxxxxx  

  3. MariaDB [(none)]> show databases;  

  4. +--------------------+  

  5. | Database           |  

  6. +--------------------+  

  7. | information_schema |  

  8. | mysql              |  

  9. | performance_schema |  

  10. | test_db            |  

  11. +--------------------+  


192.168.1.106節點查看:  

  1. [root@mariadb-a05 my.cnf.d]# mysql -uroot -pxxxxxx  

  2. MariaDB [(none)]> show databases;  

  3. +--------------------+  

  4. | Database           |  

  5. +--------------------+  

  6. | information_schema |  

  7. | mysql              |  

  8. | performance_schema |  

  9. | test_db            |  

  10. +--------------------+  

  11. 4 rows in set (0.00 sec)  

可以看到集羣正常使用。

 

創建MyISAM表測試

  1. [root@mariadb-a03 my.cnf.d]# mysql -uroot –pxxxxxx  

  2. MariaDB [(none)]> use test_db;  

  3. Database changed  

  4. MariaDB [test_db]> create table myisam_tbl (id int,name text) ENGINE MyISAM;  

  5. Query OK, 0 rows affected (0.01 sec)  

  6.    

  7. MariaDB [test_db]> insert into myisam_tbl values(1,'hive');  

  8. Query OK, 1 row affected (0.00 sec)  

  9.    

  10. MariaDB [test_db]> insert into myisam_tbl values(2,'hbase');  

  11. Query OK, 1 row affected (0.00 sec)  

其他節點查看:

  1. [root@mariadb-a04 my.cnf.d]# mysql -uroot -pxxxxxx  

  2. MariaDB [(none)]> use test_db;  

  3. Reading table information for completion of table and column names  

  4. You can turn off this feature to get a quicker startup with -A  

  5.    

  6. Database changed  

  7. MariaDB [test_db]> select * from myisam_tbl;  

  8. Empty set (0.00 sec)  

  9.    

  10. [root@mariadb-a05 my.cnf.d]# mysql -uroot –pxxxxxx  

  11. MariaDB [(none)]> use test_db;  

  12. Reading table information for completion of table and column names  

  13. You can turn off this feature to get a quicker startup with -A  

  14.    

  15. Database changed  

  16. MariaDB [test_db]> select * from myisam_tbl;  

  17. Empty set (0.00 sec)  


可以看到MyISAM存儲的表,Galera不支持同步。它僅支持XtraDB/ InnoDB存儲引擎(雖然有對MyISAM實驗支持,具體看wsrep_replicate_myisam系統變量)。

 

驗證InnoDB存儲的表

  1. [root@mariadb-a03 my.cnf.d]# mysql -uroot  –pxxxxxx  

  2. MariaDB [test_db]> create table innodb_tbl(id int,name text) ENGINE InnoDB;  

  3. Query OK, 0 rows affected (0.04 sec)  

  4.    

  5. MariaDB [test_db]> insert into innodb_tbl values(1,'hive');  

  6. Query OK, 1 row affected (0.00 sec)  

  7.    

  8. MariaDB [test_db]> insert into innodb_tbl values(2,'hbase');  

  9. Query OK, 1 row affected (0.00 sec)  

  10.    

  11. MariaDB [test_db]>  


其他節點查看:

[root@mariadb-a04 my.cnf.d]# mysql -uroot -pxxxxxx  

  1. MariaDB [(none)]> use test_db;  

  2. Reading table information for completion oftable and column names  

  3. You can turn off this feature to get aquicker startup with -A  

  4.    

  5. Database changed  

  6. MariaDB [test_db]> select * from innodb_tbl;  

  7. +------+-------+  

  8. | id  | name  |  

  9. +------+-------+  

  10. |   1 | hive  |  

  11. |   2 | hbase |  

  12. +------+-------+  

  13. 2 rows in set (0.00 sec)  

  14.    

  15. [root@mariadb-a05 my.cnf.d]# mysql -uroot –pxxxxxx  

  16. MariaDB [(none)]> use test_db;  

  17. Reading table information for completion of table and column names  

  18. You can turn off this feature to get a quicker startup with -A  

  19.    

  20. Database changed  

  21. MariaDB [test_db]> select * from innodb_tbl;  

  22. +------+-------+  

  23. | id  | name  |  

  24. +------+-------+  

  25. |   1 | hive  |  

  26. |   2 | hbase |  

  27. +------+-------+  

  28. 2 rows in set (0.00 sec)  

  29.    


模擬節點故障

將192.168.1.104數據庫停止掉:

  1. [root@mariadb-a03 system]# mysqladmin -uroot -p "shutdown"  


然後在其他節點192.168.1.105執行:

MariaDB [test_db]> show global status like 'wsrep%';  

  1. +------------------------------+-----------------------------------------------+  

  2. | Variable_name                | Value                                         |  

  3. +------------------------------+-----------------------------------------------+  

  4. | wsrep_apply_oooe             | 0.000000                                      |  

  5. | wsrep_apply_oool             | 0.000000                                      |  

  6. | wsrep_apply_window           | 1.000000                                      |  

  7. | wsrep_causal_reads           | 26                                            |  

  8. | wsrep_cert_deps_distance     | 1.142857                                      |  

  9. | wsrep_cert_index_size        |6                                            |  

  10. | wsrep_cert_interval          | 0.000000                                      |  

  11. | wsrep_cluster_conf_id        | 6                                             |  

  12. | wsrep_cluster_size          | 2                                             |  

  13. | wsrep_cluster_state_uuid     |3108c722-ff29-11e6-a31f-bb500598d033         |  

  14. | wsrep_cluster_status         | Primary                                       |  

  15. | wsrep_commit_oooe            | 0.000000                                      |  

  16. | wsrep_commit_oool            | 0.000000                                      |  

  17. | wsrep_commit_window          | 1.000000                                      |  

  18. | wsrep_connected              | ON                                            |  

  19. | wsrep_desync_count           | 0                                             |  

  20. | wsrep_evs_delayed            |                                              |  

  21. | wsrep_evs_evict_list         |                                              |  

  22. | wsrep_evs_repl_latency       |0.000403989/0.000656768/0.0012094/0.0003239/4 |  

  23. | wsrep_evs_state              | OPERATIONAL                                   |  

  24. | wsrep_flow_control_paused    | 0.000000                                      |  

  25. | wsrep_flow_control_paused_ns | 0                                             |  

  26. | wsrep_flow_control_recv      | 0                                             |  

  27. | wsrep_flow_control_sent      | 0                                             |  

  28. | wsrep_gcomm_uuid             | 0ce8537e-ff2a-11e6-b037-8a383b6a8db5          |  

  29. | wsrep_incoming_addresses    | 192.168.1.105:3306,192.168.1.106:3306       |  

  30. | wsrep_last_committed         | 10                                            |  

  31. | wsrep_local_bf_aborts        | 0                                            |  

  32. | wsrep_local_cached_downto    | 4                                             |  

  33. | wsrep_local_cert_failures    | 0                                             |  

  34. | wsrep_local_commits          | 0                                             |  

  35. | wsrep_local_index            | 0                                             |  

  36. | wsrep_local_recv_queue       | 0                                             |  

  37. | wsrep_local_recv_queue_avg   | 0.000000                                      |  

  38. | wsrep_local_recv_queue_max   | 1                                             |  

  39. | wsrep_local_recv_queue_min   | 0                                             |  

  40. | wsrep_local_replays          | 0                                             |  

  41. | wsrep_local_send_queue       | 0                                             |  

  42. | wsrep_local_send_queue_avg   | 0.000000                                      |  

  43. | wsrep_local_send_queue_max   | 1                                             |  

  44. | wsrep_local_send_queue_min   | 0                                             |  

  45. | wsrep_local_state            | 4                                             |  

  46. | wsrep_local_state_comment    | Synced                                        |  

  47. | wsrep_local_state_uuid       |3108c722-ff29-11e6-a31f-bb500598d033         |  

  48. | wsrep_protocol_version       | 7                                             |  

  49. | wsrep_provider_name          | Galera                                        |  

  50. | wsrep_provider_vendor        | Codership Oy<[email protected]>             |  

  51. | wsrep_provider_version       | 25.3.19(r3667)                                |  

  52. | wsrep_ready                  | ON                                            |  

  53. | wsrep_received               | 14                                            |  

  54. | wsrep_received_bytes         | 3908                                          |  

  55. | wsrep_repl_data_bytes        | 0                                             |  

  56. | wsrep_repl_keys              | 0                                             |  

  57. | wsrep_repl_keys_bytes        | 0                                             |  

  58. | wsrep_repl_other_bytes       | 0                                             |  

  59. | wsrep_replicated             | 0                                             |  

  60. | wsrep_replicated_bytes       | 0                                             |  

  61. | wsrep_thread_count           | 9                                             |  

  62. +------------------------------+-----------------------------------------------+  


此時集羣爲自動將192.168.1.104故障節點剔除掉,並且正常提供服務。

 

最後我們恢復失敗的節點:

  1. [root@mariadb-a03 system]# mysqld_safe --defaults-file=/etc/my.cnf.d/server.cnf --user=mysql &  



 

再次查看集羣環境:

  1. MariaDB [test_db]> SHOW STATUS LIKE 'wsrep_cluster_size';  

  2. +--------------------+-------+  

  3. | Variable_name      | Value |  

  4. +--------------------+-------+  

  5. | wsrep_cluster_size | 3     |  

  6. +--------------------+-------+  

  7. 1 row in set (0.00 sec  


模擬腦裂後的處理

下面模擬在網絡抖動發生丟包的情況下,兩個節點失聯導致腦裂。首先,在192.168.1.105和192.168.1.106兩個節點上分別執行:

iptables -A INPUT -p tcp --sport 4567 -j DROP

iptables -A INPUT -p tcp --dport 4567 -j DROP

以上命令用來禁止wsrep全同步複製4567端口通信。

 

然後我們在192.168.1.104節點查看:


  1. MariaDB [(none)]> show global statuslike 'ws%';  

  2. 可以看到下面的幾個值:  

  3. wsrep_cluster_size    1  

  4. wsrep_cluster_status  non-Primary  

  5. wsrep_ready         OFF  

  6.    

  7. MariaDB [(none)]> use test_db;  

  8. ERROR 1047 (08S01): WSREP has not yetprepared node for application use  

  9.    

  10. MariaDB [(none)]> select@@wsrep_node_name;  

  11. ERROR 1205 (HY000): Lock wait timeoutexceeded; try restarting transaction  





 

現在已經出現腦裂的情況,並且集羣無法執行任何命令。

 

爲了解決這個問題,可以執行:

set global wsrep_provider_options="pc.bootstrap=true";

通過這個命令來強制恢復出現腦裂的節點。

 

下面我們來驗證一下

  1. MariaDB [(none)]> select @@wsrep_node_name;  

  2. ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction  

  3. MariaDB [(none)]> set global wsrep_provider_options="pc.bootstrap=true";  

  4. Query OK, 0 rows affected (0.00 sec)  

  5.    

  6. MariaDB [(none)]> select @@wsrep_node_name;  

  7. +-------------------+  

  8. | @@wsrep_node_name |  

  9. +-------------------+  

  10. | mariadb-a03     |  

  11. +-------------------+  

  12. 1 row in set (0.27 sec)  

  13.    

  14. MariaDB [(none)]> use test_db;  

  15. Reading table information for completion oft able and column names  

  16. You can turn off this feature to get a quicker startup with -A  

  17.    

  18. Database changed  

  19. MariaDB [test_db]> show tables;  

  20. +-------------------+  

  21. | Tables_in_test_db |  

  22. +-------------------+  

  23. | innodb_tbl        |  

  24. | myisam_tbl        |  

  25. +-------------------+  




最後我們將節點192.168.1.105和192.168.1.106恢復一下,只要清理一下iptables表即可(因爲我的是測試環境,生產環境需要刪除上面的規則即可):

iptables –F

 

各個節點驗證一下

  1. 192.168.1.104:  

  2. MariaDB [test_db]> SHOW STATUS LIKE  'wsrep_cluster_size';  

  3. +--------------------+-------+  

  4. | Variable_name      | Value |  

  5. +--------------------+-------+  

  6. | wsrep_cluster_size | 3     |  

  7. +--------------------+-------+  

  8. 1 row in set (0.00 sec)  

  9.    

  10.    

  11. 192.168.1.105:  

  12. MariaDB [(none)]> select @@wsrep_node_name;  

  13. +-------------------+  

  14. | @@wsrep_node_name |  

  15. +-------------------+  

  16. | mariadb-a04     |  

  17. +-------------------+  

 

避免髒讀

Galera Cluster不是真正意義上的全同步複製,存在延遲。我們可以在一個節點上面執行FLUSH TABLES WITH READ LOCK;全局讀鎖。

然後在其他節點執行寫操作,觀察延遲情況。

比如我們在192.168.1.106節點執行全局讀鎖設置:

  1. MariaDB [test_db]> flush tables with read lock;  

  2. Query OK, 0 rows affected (0.00 sec)  

  3.    

  4. MariaDB [test_db]> select * from innodb_tbl;  

  5. +------+------+  

  6. | id  | name |  

  7. +------+------+  

  8. |   1 | hive |  

  9. +------+------+  

  10. 1 row in set (0.00 sec)  

  11.    

  12.    

  13. 然後在192.168.1.104節點插入操作:  

  14. MariaDB [test_db]> select @@wsrep_node_name;  

  15. +-------------------+  

  16. | @@wsrep_node_name |  

  17. +-------------------+  

  18. | mariadb-a03     |  

  19. +-------------------+  

  20. 1 row in set (0.00 sec)  

  21.    

  22. MariaDB [test_db]> insert into innodb_tbl values(2,'hbase');  

  23. Query OK, 1 row affected (0.00 sec)  

  24.    

  25. MariaDB [test_db]> select * from innodb_tbl;  

  26. +------+-------+  

  27. | id  | name  |  

  28. +------+-------+  

  29. |   1 | hive  |  

  30. |   2 | hbase |  

  31. +------+-------+  

  32. 2 rows in set (0.00 sec)  

  33.    

  34. 在節點192.168.1.106上測試查詢操作:  

  35. MariaDB [test_db]> select * from innodb_tbl;  

  36. ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction  



這裏之所以沒有讀取到髒數據,是因爲我在MariaDB配置文件中設置了wsrep_causal_reads=ON;

我們將wsrep_causal_reads修改爲0或OFF來看一下效果

  1. MariaDB [test_db]> set wsrep_causal_reads=0;  

  2. Query OK, 0 rows affected, 1 warning (0.00sec)  

  3.    

  4. MariaDB [test_db]> select * from innodb_tbl;  

  5. +------+------+  

  6. | id  | name |  

  7. +------+------+  

  8. |   1 | hive |  

  9. +------+------+  

  10. 1 row in set (0.00 sec)  

  11.    

  12. MariaDB [test_db]>  


總結

通過上面的一系列測試,最後總結一下:

1.       在生產環境下應該避免使用大事務,不建議在高併發寫入場景下使用Galera Cluster架構,會導致集羣限流,從而引起整個集羣hang住,出現生產故障。針對這種情況可以考慮主從,實現讀寫分離等手段。

2.       對數據一致性要求較高,並且數據寫入不頻繁,數據庫容量也不大(50GB左右),網絡狀況良好的情況下,可以考慮使用Galera方案


問題:

1.節點無法加入:

[ERROR] WSREP: failed to open gcomm backend connection: 131: invalid UUID: 00000000 (FATAL) at gcomm/src/pc.cpp:PC():271


Solution:

rename /var/lib/mysql/gvwstate.dat to /var/lib/mysql/gvwstate.dat.bak

systemctl start mariadb


2.數據庫集羣宕機,在運行/bin/galera_new_cluster啓動第一個節點時報錯:

It may not be safe to bootstrap the cluster from this node

數據庫集羣宕機,在運行/bin/galera_new_cluster啓動第一個節點時報錯,意思是該節點不是最後一個停掉的,不能安全啓動

我們不需需要強制從該節點啓動,我們逐一排查每個節點下的grastate.dat文件(該文件在data目錄下),找到safe_to_bootstrap=1的節點,然後在該節點上啓動即可

如果報錯都相同,則需要從3個節點中選取一個主節點,修改/var/lib/mysql/grastate.dat,把其中safe_to_bootstrap的值改爲1即可

然後運行# mysqld_safe --defaults-file=/etc/my.cnf.d/server.cnf --user=mysql  --wsrep-new-cluster &   其他節點依次啓動




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章