主要功能:php
優點:
由於是多主,因此不存在Slavelag(延遲)
不存在丟失交易的狀況
同時具備讀和寫的擴展能力
更小的客戶端延遲
節點間數據是同步的,而Master/Slave模式是異步的,不一樣slave上的binlog多是不一樣的html
1.建立集羣用戶(全部節點)node
systemctl start mysqlmysql
mysql_secure_installationlinux
GRANT ALL ON *.* TO 'galera'@'192.168.8.%' IDENTIFIED BY 'mypassword';web
FLUSH PRIVILEGES;
說明:在選用的初始化節點上建立該用戶便可,其它節點加入集羣后會自動同步數據庫
https://mariadb.com/kb/en/mariadb/galera-cluster-system-variables/#wsrep_onbootstrap
cat >/etc/my.cnf.d/galera_common.cnf <<HEREcentos
[mysqld]
wsrep-cluster-name = "test_cluster"
wsrep-provider =
wsrep-provider-options = "gcache.size=256M;gcache.page_size=128M"
wsrep-sst-auth = "galera:mypassword"
binlog-format = row
default-storage-engine = InnoDB
innodb-doublewrite = 1
innodb-autoinc-lock-mode = 2
innodb-flush-log-at-trx-commit = 2
innodb-locks-unsafe-for-binlog = 1
HERE
sed -i '1i
提示:配置集羣的最簡配置能夠參看/opt/mariadb/support-files/wsrep.cnf,且有相關解釋,很是人性化
# Options that need to be customized:
#
#
#
# The rest of defaults should work out of the box.
3.初始化集羣(db00)
db00: 192.168.8.100
cat >/etc/my.cnf.d/galera_db00.cnf <<HERE
[mysqld]
wsrep-node-name = "db00"
wsrep-sst-receive-address = 192.168.8.100
wsrep-node-incoming-address = 192.168.8.100
bind-address = 192.168.8.100
wsrep_sst_method = rsync
wsrep-cluster-address =
HERE
yum -y install lsof rsync
/etc/init.d/mysql stop
/etc/init.d/mysql start
SHOW STATUS LIKE 'wsrep%';
[root@db00 ~]# /etc/init.d/mysql stop
Shutting down MySQL.. SUCCESS!
[root@db00 ~]# /etc/init.d/mysql start --wsrep-new-cluster
Starting MySQL.. SUCCESS!
說明:用哪一個節點來初始化無所謂,早期的版本mariadb-galera-10.0.x初始化,是用空值gcomm://來表示新的集羣,mariadb-10.1.x版本已經廢止了,由wsrep-new-cluster參數取代
wsrep_sst_method
wsrep_sst_
will then be called, so a custom script can be written for this purpose. See rsync
mysqldump
, xtrabackup
xtrabackup-v2
, which use those respective methods to perform the state snapshop transfer, or skip
. skip
rsync
mysqldump
xtrabackup-v2
and xtrabackup
xtrabackup-v2
rsync
rsync
, mysqldump
, xtrabackup
, xtrabackup-v2
提示:rsync(須要安裝rsync,lsof), rsync,mysqldump都會有READ LOCK的狀況,xtrabackup-v2沒有,但也有若干限制,請根據實際業務來選定或調整同步方式
xtrabackup安裝請參看MariaDB備份還原修復
實測,開始是以rsync方式同步的集羣,後改成xtrabackup-v2,很是順暢,能夠無縫切換同步方式
4.配置並啓動其他集羣節點(db01,db02)
db01: 192.168.8.101
cat >/etc/my.cnf.d/galera_db01.cnf <<HERE
[mysqld]
wsrep-node-name = "db01"
wsrep-sst-receive-address = 192.168.8.101
wsrep-node-incoming-address = 192.168.8.101
bind-address = 192.168.8.101
wsrep-sst-method = rsync
wsrep-cluster-address =
HERE
yum -y install lsof rsync
/etc/init.d/mysql restart
[root@db01 ~]# /etc/init.d/mysql restart
Shutting down MySQL... SUCCESS!
Starting MySQL..SST in progress, setting sleep higher. SUCCESS!
db02: 192.168.8.102
cat >/etc/my.cnf.d/galera_db02.cnf <<HERE
[mysqld]
wsrep-node-name = "db02"
wsrep-sst-receive-address = 192.168.8.102
wsrep-node-incoming-address = 192.168.8.102
bind-address = 192.168.8.102
wsrep-sst-method = rsync
wsrep-cluster-address =
HERE
yum -y install lsof rsync
/etc/init.d/mysql restart
[root@db02 ~]# /etc/init.d/mysql restart
Shutting down MySQL... SUCCESS!
Starting MySQL..SST in progress, setting sleep higher. SUCCESS!
5.查看集羣狀態
[root@db00 ~]# netstat -tunlp|grep mysqld
tcp
tcp
3306負載數據存儲,4567負載集羣維護
SHOW STATUS LIKE 'wsrep%';
[root@db00 ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor.
Your MariaDB connection id is 7
Server version: 10.0.24-MariaDB-wsrep MariaDB Server, wsrep_25.13.raf7f02e
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> SHOW STATUS LIKE 'wsrep%';
+------------------------------+----------------------------------------------------------+
| Variable_name
+------------------------------+----------------------------------------------------------+
| wsrep_local_state_uuid
| wsrep_protocol_version
| wsrep_last_committed
| wsrep_replicated
| wsrep_replicated_bytes
| wsrep_repl_keys
| wsrep_repl_keys_bytes
| wsrep_repl_data_bytes
| wsrep_repl_other_bytes
| wsrep_received
| wsrep_received_bytes
| wsrep_local_commits
| wsrep_local_cert_failures
| wsrep_local_replays
| wsrep_local_send_queue
| wsrep_local_send_queue_max
| wsrep_local_send_queue_min
| wsrep_local_send_queue_avg
| wsrep_local_recv_queue
| wsrep_local_recv_queue_max
| wsrep_local_recv_queue_min
| wsrep_local_recv_queue_avg
| wsrep_local_cached_downto
| wsrep_flow_control_paused_ns | 0
| wsrep_flow_control_paused
| wsrep_flow_control_sent
| wsrep_flow_control_recv
| wsrep_cert_deps_distance
| wsrep_apply_oooe
| wsrep_apply_oool
| wsrep_apply_window
| wsrep_commit_oooe
| wsrep_commit_oool
| wsrep_commit_window
| wsrep_local_state
| wsrep_local_state_comment
| wsrep_cert_index_size
| wsrep_causal_reads
| wsrep_cert_interval
| wsrep_incoming_addresses
| wsrep_evs_delayed
| wsrep_evs_evict_list
| wsrep_evs_repl_latency
| wsrep_evs_state
| wsrep_gcomm_uuid
| wsrep_cluster_conf_id
| wsrep_cluster_size
| wsrep_cluster_state_uuid
| wsrep_cluster_status
| wsrep_connected
| wsrep_local_bf_aborts
| wsrep_local_index
| wsrep_provider_name
| wsrep_provider_vendor
| wsrep_provider_version
| wsrep_ready
| wsrep_thread_count
+------------------------------+----------------------------------------------------------+
57 rows in set (0.00 sec)
wsrep_ready爲ON,則說明MariaDB Galera 集羣已經正確運行了。
wsrep_cluster_size 標明瞭組成集羣的節點個數
如上,3個集羣節點都己成功正常運行。
6.統一修改集羣地址並重啓
wsrep-cluster-address = gcomm://192.168.8.100,192.168.8.101,192.168.8.102
MariaDB [(none)]> SHOW VARIABLES LIKE 'wsrep%'\G
*************************** 1. row ***************************
Variable_name: wsrep_auto_increment_control
*************************** 2. row ***************************
Variable_name: wsrep_causal_reads
*************************** 3. row ***************************
Variable_name: wsrep_certify_nonpk
*************************** 4. row ***************************
Variable_name: wsrep_cluster_address
*************************** 5. row ***************************
Variable_name: wsrep_cluster_name
*************************** 6. row ***************************
Variable_name: wsrep_convert_lock_to_trx
*************************** 7. row ***************************
Variable_name: wsrep_data_home_dir
*************************** 8. row ***************************
Variable_name: wsrep_dbug_option
*************************** 9. row ***************************
Variable_name: wsrep_debug
*************************** 10. row ***************************
Variable_name: wsrep_desync
*************************** 11. row ***************************
Variable_name: wsrep_dirty_reads
*************************** 12. row ***************************
Variable_name: wsrep_drupal_282555_workaround
*************************** 13. row ***************************
Variable_name: wsrep_forced_binlog_format
*************************** 14. row ***************************
Variable_name: wsrep_load_data_splitting
*************************** 15. row ***************************
Variable_name: wsrep_log_conflicts
*************************** 16. row ***************************
Variable_name: wsrep_max_ws_rows
*************************** 17. row ***************************
Variable_name: wsrep_max_ws_size
*************************** 18. row ***************************
Variable_name: wsrep_mysql_replication_bundle
*************************** 19. row ***************************
Variable_name: wsrep_node_address
*************************** 20. row ***************************
Variable_name: wsrep_node_incoming_address
*************************** 21. row ***************************
Variable_name: wsrep_node_name
*************************** 22. row ***************************
Variable_name: wsrep_notify_cmd
*************************** 23. row ***************************
Variable_name: wsrep_on
*************************** 24. row ***************************
Variable_name: wsrep_osu_method
*************************** 25. row ***************************
Variable_name: wsrep_provider
*************************** 26. row ***************************
Variable_name: wsrep_provider_options
*************************** 27. row ***************************
Variable_name: wsrep_recover
*************************** 28. row ***************************
Variable_name: wsrep_replicate_myisam
*************************** 29. row ***************************
Variable_name: wsrep_restart_slave
*************************** 30. row ***************************
Variable_name: wsrep_retry_autocommit
*************************** 31. row ***************************
Variable_name: wsrep_slave_fk_checks
*************************** 32. row ***************************
Variable_name: wsrep_slave_threads
*************************** 33. row ***************************
Variable_name: wsrep_slave_uk_checks
*************************** 34. row ***************************
Variable_name: wsrep_sst_auth
*************************** 35. row ***************************
Variable_name: wsrep_sst_donor
*************************** 36. row ***************************
Variable_name: wsrep_sst_donor_rejects_queries
*************************** 37. row ***************************
Variable_name: wsrep_sst_method
*************************** 38. row ***************************
Variable_name: wsrep_sst_receive_address
*************************** 39. row ***************************
Variable_name: wsrep_start_position
*************************** 40. row ***************************
Variable_name: wsrep_sync_wait
40 rows in set (0.00 sec)
三.測試
A.任意節點讀寫
1.在節點db02上導入了一個zabbix.sql的庫
[root@db02 ~]# mysql -uroot -p -e "CREATE DATABASE IF NOT EXISTS zabbix CHARACTER SET utf8"
Enter password:
[root@db02 ~]# mysql -uroot -p zabbix
Enter password:
2.在db01上查看
[root@db01 ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor.
Your MariaDB connection id is 10
Server version: 10.0.24-MariaDB-wsrep MariaDB Server, wsrep_25.13.raf7f02e
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database
+--------------------+
| information_schema |
| mysql
| performance_schema |
| zabbix
+--------------------+
4 rows in set (0.01 sec)
MariaDB [(none)]> USE zabbix;
Database changed
MariaDB [zabbix]> SELECT alias,name,surname FROM users;
+-------+--------+---------------+
| alias | name
+-------+--------+---------------+
| Admin | Zabbix | Administrator |
| guest |
+-------+--------+---------------+
2 rows in set (0.00 sec)
B.節點宕機
[root@db02 ~]# mysql -uroot -p -e "SHOW STATUS LIKE 'wsrep_local_state_comment'"
Enter password:
+---------------------------+--------+
| Variable_name
+---------------------------+--------+
| wsrep_local_state_comment | Synced |
+---------------------------+--------+
[root@db02 ~]# service mysql stop
Shutting down MySQL..... SUCCESS!
[root@db00 ~]# mysql -uroot -p -e "SHOW STATUS LIKE 'wsrep_local_state_comment'"
Enter password:
+---------------------------+--------+
| Variable_name
+---------------------------+--------+
| wsrep_local_state_comment | Synced |
+---------------------------+--------+
當節點恢復後,會自動加入到集羣,很是方便
四.關閉/啓動整個集羣
A.關閉
在全部集羣節點上執行service mysql stop便可
注意:最後關閉的節點很是重要,由於它維護着集羣最新的狀態,因此在啓動的時候必須最早啓動,不然,在啓動其它節點的會因找不到初始節點而報鏈接超時的錯誤
160415
160415
160415
} joined {
} left {
} partitioned {
})
160415
160415
160415
160415
160415
160415
160415
160415
160415
160415
160415
160415 01:21:00 mysqld_safe mysqld from pid file /opt/mariadb/mysqld.pid ended
B.啓動
最早啓動最後關閉的mysql節點,實測,仍然沒法啓動集羣
若是忘記了哪一個最後關閉的,能夠經過集羣初始化的節點重啓初始化下,如:
[root@db00 ~]# /etc/init.d/mysql start --wsrep-new-cluster
Starting MySQL.. SUCCESS!
[root@db00 ~]# mysql -uroot -p -e "SHOW STATUS LIKE 'wsrep_local_state_comment'"
Enter password:
+---------------------------+--------+
| Variable_name
+---------------------------+--------+
| wsrep_local_state_comment | Synced |
+---------------------------+--------+
[root@db01 ~]# service mysql start
Starting MySQL... SUCCESS!
[root@db02 ~]# service mysql start
Starting MySQL... SUCCESS!
五.Galera監控
示例腳本/opt/mariadb/support-files/wsrep_notify
查看MySQL版本: SHOW GLOBAL VARIABLES LIKE 'version';
查看wsrep版本: SHOW GLOBAL STATUS LIKE 'wsrep_provider_version';
查看wsrep有關的全部變量: SHOW VARIABLES LIKE 'wsrep%' \G
查看Galera集羣狀態: SHOW STATUS LIKE 'wsrep%';
監控狀態參數說明
集羣完整性檢查:
wsrep_cluster_state_uuid:在集羣全部節點的值應該是相同的,有不一樣值的節點,說明其沒有鏈接入集羣
wsrep_cluster_conf_id:正常狀況下全部節點上該值是同樣的。若是值不一樣,說明該節點被臨時"分區"了。當節點之間網絡鏈接恢復的時候應該會恢復同樣的值
wsrep_cluster_size:若是這個值跟預期的節點數一致,則全部的集羣節點已經鏈接。
wsrep_cluster_status:集羣組成的狀態。若是不爲"Primary",說明出現"分區"或是"split-brain"情況
節點狀態檢查:
wsrep_ready:該值爲ON,則說明能夠接受SQL負載。若是爲Off,則須要檢查wsrep_connected
wsrep_connected: 若是該值爲Off,且wsrep_ready的值也爲Off,則說明該節點沒有鏈接到集
羣。(多是wsrep_cluster_address或wsrep_cluster_name等配置錯形成的。具體錯誤須要
查看錯誤日誌)
wsrep_local_state_comment:若是wsrep_connected爲On,但wsrep_ready爲OFF,則能夠從該項查看緣由
複製健康檢查:
wsrep_flow_control_paused:表示複製中止了多長時間。即代表集羣由於Slave延遲而慢的程度。值爲0~1,越靠近0越好,值爲1表示複製徹底中止。可優化wsrep_slave_threads的值來改善
wsrep_cert_deps_distance:有多少事務能夠並行應用處理。wsrep_slave_threads設置的值不該高出該值太多。
wsrep_flow_control_sent:表示該節點已經中止複製了多少次
wsrep_local_recv_queue_avg:表示slave事務隊列的平均長度。slave瓶頸的預兆。最慢的節點的
wsrep_flow_control_sent和wsrep_local_recv_queue_avg這兩個值最高。這兩個值較低的話,相對更好
檢測慢網絡問題:
wsrep_local_send_queue_avg:網絡瓶頸的預兆。若是這個值比較高的話,可能存在網絡瓶衝突或死鎖的數目
wsrep_last_committed:最後提交的事務數目
wsrep_local_cert_failures和wsrep_local_bf_aborts:回滾,檢測到的衝突數目