Galera Cluster是由第三方公司Codership所研發的一套免費開源的集羣高可用方案,實現了數據零丟失,官網地址爲http://galeracluster.com/。其在MySQLInnoDB存儲引擎基礎上打了wrep(虛擬全同步複製),Percona/MariaDB已捆綁在各自的發行版本中。node
MariaDB Galera Cluster是MariaDB同步多主機集羣。它僅支持XtraDB/InnoDB存儲引擎(雖然有對MyISAM實驗支持,具體看wsrep_replicate_myisam系統變量)。mysql
MariaDB Galera Cluster主要功能:sql
l 同步複製數據庫
l 真正的multi-master,即全部節點能夠同時讀寫數據庫bootstrap
l 自動的節點成員控制,失效節點自動被清除centos
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文件
# cat /etc/hosts
127.0.0.1 localhost.localdomain localhost
192.168.1.104 mariadb-a03
192.168.1.105 mariadb-a04
192.168.1.106 mariadb-a05
2. /etc/security/limits.conf
* soft nofile 65536
* hard nofile 65536
3. /etc/sysctl.conf
fs.file-max=655350
net.ipv4.ip_local_port_range = 1025 65000
net.ipv4.tcp_tw_recycle = 1
最後執行:
# sysctl -p
4. 安裝Percona XtraBackup熱備份工具
下載地址:
解壓縮:
# tar -zxvf percona-xtrabackup-2.4.6-Linux-x86_64.tar.gz
拷貝腳本到指定位置:
# cd percona-xtrabackup-2.4.6-Linux-x86_64/bin/
# cp -a * /usr/bin/
安裝依賴的一些包,好比lsof,socat,openssl,tar等
建立XtraBackup備份時用的用戶名和密碼:
MariaDB [(none)]> grant all on *.* to 'galera'@'localhost' identified by '123456';
從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)
# touch /etc/yum.repos.d/MariaDB-IDC.repo
添加以下內容:
[mariadb]
name = MariaDB
baseurl =http://yum.mariadb.org/10.1/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
因爲咱們的環境沒法訪問外網,單獨部署一個MariaDB的Yum源:
# cat /etc/yum.repos.d/MariaDB-IDC.repo
[MariaDB-10.1-IDC]
name=MariaDB-10.1-IDC
baseurl=http://192.168.1.100/repo/yum.mariadb.org/10.1/centos7-amd64
gpgcheck=0
enabled=1
步驟二:安裝MariaDB(192.168.1.104,192.168.1.105,192.168.1.106)
# yum install MariaDB-server MariaDB-client galera -y
下面咱們開始配置MariaDB Galera Cluster,分別修改MariaDB Galera集羣的每一個節點上的/etc/my.cnf.d/server.cnf文件,具體每一個節點的內容以下:
1. 192.168.1.104節點的/etc/my.cnf.d/server.cnf文件內容:
[root@mariadb-a03 ~]# cat /etc/my.cnf.d/server.cnf | grep -v "#" | grep -v "^$"
[server]
[mysqld]
server_id=128
datadir=/app/galera
user=mysql
skip-external-locking
skip-name-resolve
character-set-server=utf8
[galera]
wsrep_causal_reads=ON #節點應用完事務才返回查詢請求
wsrep_provider_options="gcache.size=4G"#同步複製緩衝池
wsrep_certify_nonPK=ON #爲沒有顯式申明主鍵的表生成一個用於certificationtest的主鍵,默認爲ON
#log-bin=/app/galera/mysql-bin #若是不接從庫,註釋掉
#log_slave_updates=1 #若是不接從庫,註釋掉
query_cache_size=0 #關閉查詢緩存
wsrep_on=ON #開啓全同步複製模式
wsrep_provider=/usr/lib64/galera/libgalera_smm.so#galera library
wsrep_cluster_name=MariaDB-Galera-Cluster
wsrep_cluster_address="gcomm://192.168.1.104,192.168.1.105,192.168.1.106" #galera cluster URL
wsrep_node_name=mariadb-a03
wsrep_node_address=192.168.1.104
binlog_format=row
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2 #主鍵自增模式修改成交叉模式
wsrep_slave_threads=8 #開啓並行複製線程,根據CPU核數設置
innodb_flush_log_at_trx_commit=0 #事務提交每隔1秒刷盤
innodb_buffer_pool_size=2G
wsrep_sst_method=rsync
[embedded]
[mariadb]
[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文件內容:
[root@mariadb-a04 ~]# cat /etc/my.cnf.d/server.cnf | grep -v "#" | grep -v "^$"
[server]
[mysqld]
server_id=129
datadir=/app/galera
user=mysql
skip-external-locking
skip-name-resolve
character-set-server=utf8
[galera]
wsrep_causal_reads=ON
wsrep_provider_options="gcache.size=4G"
wsrep_certify_nonPK=ON
query_cache_size=0
wsrep_on=ON
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_name=MariaDB-Galera-Cluster
wsrep_cluster_address="gcomm://192.168.1.104,192.168.1.105,192.168.1.106"
wsrep_node_name=mariadb-a04
wsrep_node_address=192.168.1.105
binlog_format=row
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
wsrep_slave_threads=8
innodb_flush_log_at_trx_commit=0
innodb_buffer_pool_size=2G
wsrep_sst_method=rsync
[embedded]
[mariadb]
[mariadb-10.1]
3. 192.168.1.106節點的/etc/my.cnf.d/server.cnf文件內容:
[root@mariadb-a05 yum.repos.d]# cat /etc/my.cnf.d/server.cnf | grep -v "#" | grep -v "^$"
[server]
[mysqld]
server_id=130
datadir=/app/galera
user=mysql
skip-external-locking
skip-name-resolve
character-set-server=utf8
[galera]
wsrep_causal_reads=ON
wsrep_provider_options="gcache.size=4G"
wsrep_certify_nonPK=ON
query_cache_size=0
wsrep_on=ON
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_name=MariaDB-Galera-Cluster
wsrep_cluster_address="gcomm://192.168.1.104,192.168.1.105,192.168.1.106"
wsrep_node_name=mariadb-a05
wsrep_node_address=192.168.1.106
binlog_format=row
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
wsrep_slave_threads=8
innodb_flush_log_at_trx_commit=0
innodb_buffer_pool_size=2G
wsrep_sst_method=rsync
[embedded]
[mariadb]
[mariadb-10.1]
MariaDB一個節點初始化安裝(192.168.1.104):
# 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)
/usr/bin/mysql_secure_installation
或
mysql_secure_installation
在192.168.1.105,192.168.1.106節點啓動MariaDB:
mysqld_safe --defaults-file=/etc/my.cnf.d/server.cnf --user=mysql &
192.168.1.104節點:
MariaDB [(none)]> SHOW STATUS LIKE 'wsrep_cluster_size';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| wsrep_cluster_size | 3 |
+--------------------+-------+
1 row in set (0.00 sec)
MariaDB [(none)]> show global status like 'ws%';
+------------------------------+-------------------------------------------------------------+
| Variable_name | Value |
+------------------------------+-------------------------------------------------------------+
| wsrep_apply_oooe | 0.000000 |
| wsrep_apply_oool | 0.000000 |
| wsrep_apply_window | 1.000000 |
| wsrep_causal_reads | 11 |
| wsrep_cert_deps_distance | 1.000000 |
| wsrep_cert_index_size | 2 |
| wsrep_cert_interval | 0.000000 |
| wsrep_cluster_conf_id | 3 |
| wsrep_cluster_size | 3 |
| wsrep_cluster_state_uuid |3108c722-ff29-11e6-a31f-bb500598d033 |
| wsrep_cluster_status | Primary |
| wsrep_commit_oooe | 0.000000 |
| wsrep_commit_oool | 0.000000 |
| wsrep_commit_window | 1.000000 |
| wsrep_connected | ON |
| wsrep_desync_count | 0 |
| wsrep_evs_delayed | |
| wsrep_evs_evict_list | |
| wsrep_evs_repl_latency | 0/0/0/0/0 |
| wsrep_evs_state | OPERATIONAL |
| wsrep_flow_control_paused | 0.000000 |
| wsrep_flow_control_paused_ns | 0 |
| wsrep_flow_control_recv | 0 |
| wsrep_flow_control_sent | 0 |
| wsrep_gcomm_uuid |3107a278-ff29-11e6-96d3-374133af7e21 |
| wsrep_incoming_addresses | 192.168.1.105:3306,192.168.1.106:3306,192.168.1.104:3306|
| wsrep_last_committed | 3 |
| wsrep_local_bf_aborts | 0 |
| wsrep_local_cached_downto | 1 |
| wsrep_local_cert_failures | 0 |
| wsrep_local_commits | 0 |
| wsrep_local_index | 2 |
| wsrep_local_recv_queue | 0 |
| wsrep_local_recv_queue_avg | 0.000000 |
| wsrep_local_recv_queue_max | 1 |
| wsrep_local_recv_queue_min | 0 |
| wsrep_local_replays | 0 |
| wsrep_local_send_queue | 0 |
| wsrep_local_send_queue_avg | 0.000000 |
| wsrep_local_send_queue_max | 1 |
| wsrep_local_send_queue_min | 0 |
| wsrep_local_state | 4 |
| wsrep_local_state_comment | Synced |
| wsrep_local_state_uuid |3108c722-ff29-11e6-a31f-bb500598d033 |
| wsrep_protocol_version | 7 |
| wsrep_provider_name | Galera |
| wsrep_provider_vendor | Codership Oy<info@codership.com> |
| wsrep_provider_version | 25.3.19(r3667) |
| wsrep_ready | ON |
| wsrep_received | 10 |
| wsrep_received_bytes | 806 |
| wsrep_repl_data_bytes | 1044 |
| wsrep_repl_keys | 3 |
| wsrep_repl_keys_bytes | 93 |
| wsrep_repl_other_bytes | 0 |
| wsrep_replicated | 3 |
| wsrep_replicated_bytes | 1329 |
| wsrep_thread_count | 9 |
+------------------------------+-------------------------------------------------------------+
58 rows in set (0.00 sec)
註釋:
wsrep_cluster_status爲Primary,表示節點爲主節點,正常讀寫。
wsrep_ready爲ON,表示集羣正常運行。
wsrep_cluster_size爲3,表示集羣有三個節點。
192.168.1.104節點:
[root@mariadb-a03 my.cnf.d]# mysql -uroot –pxxxxxx
MariaDB [(none)]> create databasetest_db;
Query OK, 1 row affected (0.01 sec)
192.168.1.105節點查看:
[root@mariadb-a04 my.cnf.d]# mysql -uroot -pxxxxxx
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test_db |
+--------------------+
192.168.1.106節點查看:
[root@mariadb-a05 my.cnf.d]# mysql -uroot -pxxxxxx
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test_db |
+--------------------+
4 rows in set (0.00 sec)
能夠看到集羣正常使用。
[root@mariadb-a03 my.cnf.d]# mysql -uroot –pxxxxxx
MariaDB [(none)]> use test_db;
Database changed
MariaDB [test_db]> create table myisam_tbl (id int,name text) ENGINE MyISAM;
Query OK, 0 rows affected (0.01 sec)
MariaDB [test_db]> insert into myisam_tbl values(1,'hive');
Query OK, 1 row affected (0.00 sec)
MariaDB [test_db]> insert into myisam_tbl values(2,'hbase');
Query OK, 1 row affected (0.00 sec)
其餘節點查看:
[root@mariadb-a04 my.cnf.d]# mysql -uroot -pxxxxxx
MariaDB [(none)]> use test_db;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [test_db]> select * from myisam_tbl;
Empty set (0.00 sec)
[root@mariadb-a05 my.cnf.d]# mysql -uroot –pxxxxxx
MariaDB [(none)]> use test_db;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [test_db]> select * from myisam_tbl;
Empty set (0.00 sec)
能夠看到MyISAM存儲的表,Galera不支持同步。它僅支持XtraDB/ InnoDB存儲引擎(雖然有對MyISAM實驗支持,具體看wsrep_replicate_myisam系統變量)。
[root@mariadb-a03 my.cnf.d]# mysql -uroot –pxxxxxx
MariaDB [test_db]> create table innodb_tbl(id int,name text) ENGINE InnoDB;
Query OK, 0 rows affected (0.04 sec)
MariaDB [test_db]> insert into innodb_tbl values(1,'hive');
Query OK, 1 row affected (0.00 sec)
MariaDB [test_db]> insert into innodb_tbl values(2,'hbase');
Query OK, 1 row affected (0.00 sec)
MariaDB [test_db]>
其餘節點查看:
[root@mariadb-a04 my.cnf.d]# mysql -uroot -pxxxxxx
MariaDB [(none)]> use test_db;
Reading table information for completion oftable and column names
You can turn off this feature to get aquicker startup with -A
Database changed
MariaDB [test_db]> select * from innodb_tbl;
+------+-------+
| id | name |
+------+-------+
| 1 | hive |
| 2 | hbase |
+------+-------+
2 rows in set (0.00 sec)
[root@mariadb-a05 my.cnf.d]# mysql -uroot –pxxxxxx
MariaDB [(none)]> use test_db;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [test_db]> select * from innodb_tbl;
+------+-------+
| id | name |
+------+-------+
| 1 | hive |
| 2 | hbase |
+------+-------+
2 rows in set (0.00 sec)
將192.168.1.104數據庫中止掉:
[root@mariadb-a03 system]# mysqladmin -uroot -p "shutdown"
而後在其餘節點192.168.1.105執行:
MariaDB [test_db]> show global status like 'wsrep%';
+------------------------------+-----------------------------------------------+
| Variable_name | Value |
+------------------------------+-----------------------------------------------+
| wsrep_apply_oooe | 0.000000 |
| wsrep_apply_oool | 0.000000 |
| wsrep_apply_window | 1.000000 |
| wsrep_causal_reads | 26 |
| wsrep_cert_deps_distance | 1.142857 |
| wsrep_cert_index_size |6 |
| wsrep_cert_interval | 0.000000 |
| wsrep_cluster_conf_id | 6 |
| wsrep_cluster_size | 2 |
| wsrep_cluster_state_uuid |3108c722-ff29-11e6-a31f-bb500598d033 |
| wsrep_cluster_status | Primary |
| wsrep_commit_oooe | 0.000000 |
| wsrep_commit_oool | 0.000000 |
| wsrep_commit_window | 1.000000 |
| wsrep_connected | ON |
| wsrep_desync_count | 0 |
| wsrep_evs_delayed | |
| wsrep_evs_evict_list | |
| wsrep_evs_repl_latency |0.000403989/0.000656768/0.0012094/0.0003239/4 |
| wsrep_evs_state | OPERATIONAL |
| wsrep_flow_control_paused | 0.000000 |
| wsrep_flow_control_paused_ns | 0 |
| wsrep_flow_control_recv | 0 |
| wsrep_flow_control_sent | 0 |
| wsrep_gcomm_uuid | 0ce8537e-ff2a-11e6-b037-8a383b6a8db5 |
| wsrep_incoming_addresses | 192.168.1.105:3306,192.168.1.106:3306 |
| wsrep_last_committed | 10 |
| wsrep_local_bf_aborts | 0 |
| wsrep_local_cached_downto | 4 |
| wsrep_local_cert_failures | 0 |
| wsrep_local_commits | 0 |
| wsrep_local_index | 0 |
| wsrep_local_recv_queue | 0 |
| wsrep_local_recv_queue_avg | 0.000000 |
| wsrep_local_recv_queue_max | 1 |
| wsrep_local_recv_queue_min | 0 |
| wsrep_local_replays | 0 |
| wsrep_local_send_queue | 0 |
| wsrep_local_send_queue_avg | 0.000000 |
| wsrep_local_send_queue_max | 1 |
| wsrep_local_send_queue_min | 0 |
| wsrep_local_state | 4 |
| wsrep_local_state_comment | Synced |
| wsrep_local_state_uuid |3108c722-ff29-11e6-a31f-bb500598d033 |
| wsrep_protocol_version | 7 |
| wsrep_provider_name | Galera |
| wsrep_provider_vendor | Codership Oy<info@codership.com> |
| wsrep_provider_version | 25.3.19(r3667) |
| wsrep_ready | ON |
| wsrep_received | 14 |
| wsrep_received_bytes | 3908 |
| wsrep_repl_data_bytes | 0 |
| wsrep_repl_keys | 0 |
| wsrep_repl_keys_bytes | 0 |
| wsrep_repl_other_bytes | 0 |
| wsrep_replicated | 0 |
| wsrep_replicated_bytes | 0 |
| wsrep_thread_count | 9 |
+------------------------------+-----------------------------------------------+
此時集羣爲自動將192.168.1.104故障節點剔除掉,而且正常提供服務。
最後咱們恢復失敗的節點:
[root@mariadb-a03 system]# mysqld_safe --defaults-file=/etc/my.cnf.d/server.cnf --user=mysql &
再次查看集羣環境:
MariaDB [test_db]> SHOW STATUS LIKE 'wsrep_cluster_size';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| wsrep_cluster_size | 3 |
+--------------------+-------+
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節點查看:
MariaDB [(none)]> show global statuslike 'ws%';
能夠看到下面的幾個值:
wsrep_cluster_size 1
wsrep_cluster_status non-Primary
wsrep_ready OFF
MariaDB [(none)]> use test_db;
ERROR 1047 (08S01): WSREP has not yetprepared node for application use
MariaDB [(none)]> select@@wsrep_node_name;
ERROR 1205 (HY000): Lock wait timeoutexceeded; try restarting transaction
如今已經出現腦裂的狀況,而且集羣沒法執行任何命令。
爲了解決這個問題,能夠執行:
set global wsrep_provider_options="pc.bootstrap=true";
經過這個命令來強制恢復出現腦裂的節點。
下面咱們來驗證一下
MariaDB [(none)]> select @@wsrep_node_name;
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
MariaDB [(none)]> set global wsrep_provider_options="pc.bootstrap=true";
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> select @@wsrep_node_name;
+-------------------+
| @@wsrep_node_name |
+-------------------+
| mariadb-a03 |
+-------------------+
1 row in set (0.27 sec)
MariaDB [(none)]> use test_db;
Reading table information for completion oft able and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [test_db]> show tables;
+-------------------+
| Tables_in_test_db |
+-------------------+
| innodb_tbl |
| myisam_tbl |
+-------------------+
最後咱們將節點192.168.1.105和192.168.1.106恢復一下,只要清理一下iptables表便可(由於個人是測試環境,生產環境須要刪除上面的規則便可):
iptables –F
各個節點驗證一下
192.168.1.104:
MariaDB [test_db]> SHOW STATUS LIKE 'wsrep_cluster_size';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| wsrep_cluster_size | 3 |
+--------------------+-------+
1 row in set (0.00 sec)
192.168.1.105:
MariaDB [(none)]> select @@wsrep_node_name;
+-------------------+
| @@wsrep_node_name |
+-------------------+
| mariadb-a04 |
+-------------------+
Galera Cluster不是真正意義上的全同步複製,存在延遲。咱們能夠在一個節點上面執行FLUSH TABLES WITH READ LOCK;全局讀鎖。
而後在其餘節點執行寫操做,觀察延遲狀況。
好比咱們在192.168.1.106節點執行全局讀鎖設置:
MariaDB [test_db]> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
MariaDB [test_db]> select * from innodb_tbl;
+------+------+
| id | name |
+------+------+
| 1 | hive |
+------+------+
1 row in set (0.00 sec)
而後在192.168.1.104節點插入操做:
MariaDB [test_db]> select @@wsrep_node_name;
+-------------------+
| @@wsrep_node_name |
+-------------------+
| mariadb-a03 |
+-------------------+
1 row in set (0.00 sec)
MariaDB [test_db]> insert into innodb_tbl values(2,'hbase');
Query OK, 1 row affected (0.00 sec)
MariaDB [test_db]> select * from innodb_tbl;
+------+-------+
| id | name |
+------+-------+
| 1 | hive |
| 2 | hbase |
+------+-------+
2 rows in set (0.00 sec)
在節點192.168.1.106上測試查詢操做:
MariaDB [test_db]> select * from innodb_tbl;
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
這裏之因此沒有讀取到髒數據,是由於我在MariaDB配置文件中設置了wsrep_causal_reads=ON;
咱們將wsrep_causal_reads修改成0或OFF來看一下效果
MariaDB [test_db]> set wsrep_causal_reads=0;
Query OK, 0 rows affected, 1 warning (0.00sec)
MariaDB [test_db]> select * from innodb_tbl;
+------+------+
| id | name |
+------+------+
| 1 | hive |
+------+------+
1 row in set (0.00 sec)
MariaDB [test_db]>
總結
經過上面的一系列測試,最後總結一下:
1. 在生產環境下應該避免使用大事務,不建議在高併發寫入場景下使用Galera Cluster架構,會致使集羣限流,從而引發整個集羣hang住,出現生產故障。針對這種狀況能夠考慮主從,實現讀寫分離等手段。
2. 對數據一致性要求較高,而且數據寫入不頻繁,數據庫容量也不大(50GB左右),網絡情況良好的狀況下,能夠考慮使用Galera方案
問題:
1.節點沒法加入:
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 & 其餘節點依次啓動