您好,登錄后才能下訂單哦!
本篇內容主要講解“MySQL多主復制和MySQL Galera的安裝部署方法”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“MySQL多主復制和MySQL Galera的安裝部署方法”吧!
特性簡介
MySQL/Galera是MySQL/InnoDB的多主集群,有以下特性:
- 同步復制
- Active-active的多主拓撲結構
- 集群任意節點可以讀和寫
- 自動身份控制,失敗節點自動脫離集群
- 自動節點接入
- 真正的基于”行”級別和ID檢查的并行復制
- 無單點故障,易擴展
安裝前準備
機器準備
G221 : 192.168.1.221 (Centos 6.4)
G222 : 192.168.1.222 (Centos 6.4)
G223 : 192.168.1.223 (Centos 6.4)
安裝依賴
- 確認安裝有gcc和gcc-c++的版本為至少4.4
# yum install gcc gcc-c++
- 確認安裝有boost-devel的版本至少為1.4.1
# yum install boost-devel
- 安裝scons check-devel openssl-devel
# yum install scons check-devel openssl-devel
MySQL Galera安裝
安裝含wsrep Patch的MySQL 5.5.29
# yum install libaio # wget https://launchpad.net/codership-mysql/5.5/5.5.29-23.7.3/+download/mysql-5.5.29_wsrep_23.7.3-linux-x86_64.tar.gz # tar zxvf mysql-5.5.29_wsrep_23.7.3-linux-x86_64.tar.gz # # mv mysql-5.5.29_wsrep_23.7.3-linux-x86_64 /usr/local/mysql # cd /usr/local/mysql/ # groupadd mysql # useradd -r -g mysql mysql # chown -R mysql:mysql . # ./scripts/mysql_install_db --no-defaults --datadir=/usr/local/mysql/data --user=mysql # chown -R root . # chown -R mysql data
安裝Galera復制插件
# wget https://launchpad.net/galera/2.x/23.2.4/+download/galera-23.2.4-src.tar.gz # tar zxvf galera-23.2.4-src.tar.gz # cd galera-23.2.4-src # scons # cp garb/garbd /usr/local/mysql/bin/ # cp libgalera_smm.so /usr/local/mysql/lib/plugin/
MySQL Galera配置
MySQL Galera配置例子:
# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql # mkdir -p /var/lib/mysql # chown mysql:mysql /var/lib/mysql # vi /etc/my.cnf # cat /etc/my.cnf
[client] port = 3306 socket = /var/lib/mysql/mysql.sock [mysqld_safe] log-error = /var/lib/mysql/mysql.log pid-file = /var/lib/mysql/mysql.pid [mysqld] wsrep_node_name = node1 wsrep_provider = /usr/local/mysql/lib/plugin/libgalera_smm.so #wsrep_provider_options ='gcache.size=1G;socket.ssl_key=my_key;socket.ssl_cert=my_cert' #wsrep_slave_threads=16 wsrep_sst_method = rsync #wsrep_sst_auth=root: port = 3306 socket = /var/lib/mysql/mysql.sock user = mysql basedir = /usr/local/mysql datadir = /usr/local/mysql/data default_storage_engine=InnoDB #innodb_buffer_pool_size=1G #innodb_log_file_size=256M innodb_autoinc_lock_mode=2 innodb_locks_unsafe_for_binlog=1 innodb_flush_log_at_trx_commit=0 innodb_doublewrite=0 innodb_file_per_table=1 binlog_format=ROW log-bin=mysql-bin server-id=101 relay-log=mysql-relay-bin #read_only=1 log-slave-updates=1
注: 可參考MySQL wsrep參數
MySQL Galera啟動與關閉
初次啟動:
[root@G221 ~]# /usr/local/mysql/bin/mysqld_safe --wsrep_cluster_address=gcomm:// >/dev/null &
或
[root@G221 ~]# service mysql start --wsrep_cluster_address=gcomm://
查看mysqld監聽的端口:
[root@G221 ~]# netstat -plantu | grep mysqld tcp 0 0 0.0.0.0:4567 0.0.0.0:* LISTEN 3656/mysqld tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 3656/mysqld
注:
1)”gcomm://”是特殊的地址,僅僅是galera cluster初始化啟動時候使用,再次啟動的時候需要使用具體的IP地址.
2) 端口4567是wsrep使用的默認端口.該端口的防火墻設置規則應該和3306的一樣.
關閉:
[root@G221 ~]# /usr/local/mysql/bin/mysqladmin -uroot -p shutdown
添加新節點
節點接入
添加新節點的時候,新接入的節點叫Joiner,給joiner提供復制的節點叫Donor.新的節點接入需要:
1) 安裝帶wsrep patch的MySQL版本
2) 安裝Galera復制插件
3) 配置好新節點的MySQL(參考Donnor的my.cnf)
4) 配置或啟動的gcomm://的地址是需要使用donnor的IP.
接入節點G222:
[root@G222 data]# /usr/local/mysql/bin/mysqld_safe --wsrep_cluster_address="gcomm://192.168.1.221:4567,192.168.1.223:4567" >/dev/null &
接入節點G223:
[root@G223 data]# service mysql start --wsrep_cluster_address="gcomm://192.168.1.221:4567,192.168.1.222:4567"
修改節點的wsrep_cluster_address修改wsrep_cluster_address有兩種方式:1)使用新的wsrep_cluster_address重啟節點:
[root@G221 data]# service mysql restart --wsrep_cluster_address="gcomm://192.168.1.222:4567,192.168.1.223:4567" Shutting down MySQL.... SUCCESS! Starting MySQL....... SUCCESS!
2)直接修改MySQL全局變量
mysql> SHOW VARIABLES LIKE 'wsrep_cluster_address'; +-----------------------+----------------------------+ | Variable_name | Value | +-----------------------+----------------------------+ | wsrep_cluster_address | gcomm://192.168.1.222:4567 | +-----------------------+----------------------------+ 1 row in set (0.00 sec) mysql> set global wsrep_cluster_address="gcomm://192.168.1.222:4567,192.168.1.223:4567"; Query OK, 0 rows affected (2.20 sec) mysql> SHOW VARIABLES LIKE 'wsrep_cluster_address'; +-----------------------+-------------------------------------------------------+ | Variable_name | Value | +-----------------------+-------------------------------------------------------+ | wsrep_cluster_address | gcomm://192.168.1.222:4567,192.168.1.223:4567 | +-----------------------+-------------------------------------------------------+ 1 row in set (0.00 sec)
查看相關變量
查看MySQL版本:
mysql> SHOW GLOBAL VARIABLES LIKE 'version'; +---------------+------------+ | Variable_name | Value | +---------------+------------+ | version | 5.5.29-log | +---------------+------------+ 1 row in set (0.00 sec)
查看wsrep版本:
mysql> SHOW GLOBAL STATUS LIKE 'wsrep_provider_version'; +------------------------+------------+ | Variable_name | Value | +------------------------+------------+ | wsrep_provider_version | 2.4(rXXXX) | +------------------------+------------+ 1 row in set (0.00 sec)
查看wsrep有關的所有變量:
mysql> SHOW VARIABLES LIKE 'wsrep%' \G *************************** 1. row *************************** Variable_name: wsrep_OSU_method Value: TOI *************************** 2. row *************************** Variable_name: wsrep_auto_increment_control Value: ON *************************** 3. row *************************** Variable_name: wsrep_causal_reads Value: OFF *************************** 4. row *************************** Variable_name: wsrep_certify_nonPK Value: ON *************************** 5. row *************************** Variable_name: wsrep_cluster_address Value: gcomm://192.168.1.222:4567,192.168.1.223:4567 *************************** 6. row *************************** Variable_name: wsrep_cluster_name Value: my_wsrep_cluster *************************** 7. row *************************** Variable_name: wsrep_convert_LOCK_to_trx Value: OFF *************************** 8. row *************************** Variable_name: wsrep_data_home_dir Value: /usr/local/mysql/data/ *************************** 9. row *************************** Variable_name: wsrep_dbug_option Value: *************************** 10. row *************************** Variable_name: wsrep_debug Value: OFF *************************** 11. row *************************** Variable_name: wsrep_drupal_282555_workaround Value: OFF *************************** 12. row *************************** Variable_name: wsrep_forced_binlog_format Value: NONE *************************** 13. row *************************** Variable_name: wsrep_log_conflicts Value: OFF *************************** 14. row *************************** Variable_name: wsrep_max_ws_rows Value: 131072 *************************** 15. row *************************** Variable_name: wsrep_max_ws_size Value: 1073741824 *************************** 16. row *************************** Variable_name: wsrep_mysql_replication_bundle Value: 0 *************************** 17. row *************************** Variable_name: wsrep_node_address Value: *************************** 18. row *************************** Variable_name: wsrep_node_incoming_address Value: AUTO *************************** 19. row *************************** Variable_name: wsrep_node_name Value: node1 *************************** 20. row *************************** Variable_name: wsrep_notify_cmd Value: *************************** 21. row *************************** Variable_name: wsrep_on Value: ON *************************** 22. row *************************** Variable_name: wsrep_provider Value: /usr/local/mysql/lib/plugin/libgalera_smm.so *************************** 23. row *************************** Variable_name: wsrep_provider_options Value: base_host = 192.168.1.221; base_port = 4567; cert.log_conflicts = no; evs.causal_keepalive_period = PT1S; evs.debug_log_mask = 0x1; evs.inactive_check_period = PT0.5S; evs.inactive_timeout = PT15S; evs.info_log_mask = 0; evs.install_timeout = PT15S; evs.join_retrans_period = PT1S; evs.keepalive_period = PT1S; evs.max_install_timeouts = 1; evs.send_window = 4; evs.stats_report_period = PT1M; evs.suspect_timeout = PT5S; evs.use_aggregate = true; evs.user_send_window = 2; evs.version = 0; evs.view_forget_timeout = PT5M; gcache.dir = /usr/local/mysql/data/; gcache.keep_pages_size = 0; gcache.mem_size = 0; gcache.name = /usr/local/mysql/data//galera.cache; gcache.page_size = 128M; gcache.size = 128M; gcs.fc_debug = 0; gcs.fc_factor = 1; gcs.fc_limit = 16; gcs.fc_master_slave = NO; gcs.max_packet_size = 64500; gcs.max_throttle = 0.25; gcs.recv_q_hard_limit = 9223372036854775807; gcs.recv_q_soft_limit = 0.25; gcs.sync_donor = NO; gmcast.listen_addr = tcp://0.0.0.0:4567; gmcast.mcast_addr = ; gmcast.mcast_ttl = 1; gmcast.peer_timeout = PT3S; gmcast.time_wait = PT5S; gmcast.version = 0; ist.recv_addr = 192.168.1.221; pc.checksum = true; pc.ignore_quorum = false; pc.ignore_sb = false; pc.linger = PT20S; pc.npvo = false; pc.version = 0; pc.weight = 1; protonet.backend = asio; protonet.version = 0; replicator.causal_read_timeout = PT30S; replicator.commit_order = 3 *************************** 24. row *************************** Variable_name: wsrep_recover Value: OFF *************************** 25. row *************************** Variable_name: wsrep_replicate_myisam Value: OFF *************************** 26. row *************************** Variable_name: wsrep_retry_autocommit Value: 1 *************************** 27. row *************************** Variable_name: wsrep_slave_threads Value: 2 *************************** 28. row *************************** Variable_name: wsrep_sst_auth Value: *************************** 29. row *************************** Variable_name: wsrep_sst_donor Value: *************************** 30. row *************************** Variable_name: wsrep_sst_donor_rejects_queries Value: OFF *************************** 31. row *************************** Variable_name: wsrep_sst_method Value: rsync *************************** 32. row *************************** Variable_name: wsrep_sst_receive_address Value: AUTO *************************** 33. row *************************** Variable_name: wsrep_start_position Value: 80cdd13d-8cf2-11e2-0800-e0817023b754:0 33 rows in set (0.00 sec)
狀態監控
查看Galera集群狀態:
mysql> show status like 'wsrep%'; +----------------------------+----------------------------------------------------------+ | Variable_name | Value | +----------------------------+----------------------------------------------------------+ | wsrep_local_state_uuid | 80cdd13d-8cf2-11e2-0800-e0817023b754 | | wsrep_protocol_version | 4 | | wsrep_last_committed | 3 | | wsrep_replicated | 3 | | wsrep_replicated_bytes | 522 | | wsrep_received | 6 | | wsrep_received_bytes | 1134 | | wsrep_local_commits | 1 | | wsrep_local_cert_failures | 0 | | wsrep_local_bf_aborts | 0 | | wsrep_local_replays | 0 | | wsrep_local_send_queue | 0 | | wsrep_local_send_queue_avg | 0.000000 | | wsrep_local_recv_queue | 0 | | wsrep_local_recv_queue_avg | 0.000000 | | wsrep_flow_control_paused | 0.000000 | | wsrep_flow_control_sent | 0 | | wsrep_flow_control_recv | 0 | | wsrep_cert_deps_distance | 1.000000 | | wsrep_apply_oooe | 0.000000 | | wsrep_apply_oool | 0.000000 | | wsrep_apply_window | 1.000000 | | wsrep_commit_oooe | 0.000000 | | wsrep_commit_oool | 0.000000 | | wsrep_commit_window | 1.000000 | | wsrep_local_state | 4 | | wsrep_local_state_comment | Synced | | wsrep_cert_index_size | 5 | | wsrep_causal_reads | 0 | | wsrep_incoming_addresses | 192.168.1.221:3306,192.168.1.222:3306,192.168.1.223:3306 | | wsrep_cluster_conf_id | 13 | | wsrep_cluster_size | 3 | | wsrep_cluster_state_uuid | 80cdd13d-8cf2-11e2-0800-e0817023b754 | | wsrep_cluster_status | Primary | | wsrep_connected | ON | | wsrep_local_index | 0 | | wsrep_provider_name | Galera | | wsrep_provider_vendor | Codership Oy <info@codership.com> | | wsrep_provider_version | 2.4(rXXXX) | | wsrep_ready | ON | +----------------------------+----------------------------------------------------------+ 40 rows in set (0.00 sec)
監控狀態說明
wsrep_last_committed:最后提交的事務數目
wsrep_local_cert_failures和wsrep_local_bf_aborts:回滾,檢測到的沖突數目(www.51itstudy.com)
wsrep_local_send_queue_avg:網絡瓶頸的預兆.如果這個值比較高的話,可能存在網絡瓶
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_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_cluster_state_uuid:在集群所有節點的值應該是相同的,有不同值的節點,說明其沒有連接入集群.
wsrep_cluster_conf_id:正常情況下所有節點上該值是一樣的.如果值不同,說明該節點被臨時”分區”了.當節點之間網絡連接恢復的時候應該會恢復一樣的值.
wsrep_cluster_size:如果這個值跟預期的節點數一致,則所有的集群節點已經連接.
wsrep_cluster_status:集群組成的狀態.如果不為”Primary”,說明出現”分區”或是”split-brain”狀況.
集群完整性檢查:
節點狀態檢查:
復制健康檢查:
最慢的節點的wsrep_flow_control_sent和wsrep_local_recv_queue_avg這兩個值最高.這兩個值較低的話,相對更好.
檢測慢網絡問題:
沖突或死鎖的數目:
Galera arbitrator
garbd可以做為一個無數據的Galera節點運行,該節點可以用一種優化的方式幫助檢測和處理網絡分裂(network splits).
一個好的garbd節點可以防止”split-brain”狀況發生.
SSL支持
Galera支持SSL通訊,這對增加數據安全性很重要(特別是云部署).可以通過參數socket.ssl_cert和socket.ssl_key來定義.
SST方式選擇
SST允許新接入的節點使用定制的方法來獲取最初的數據.SST方式有mysqldump(默認,比較慢),rsync,和xtrabackup(很快).
可通過wsrep_sst_method來定義.Production環境中建議使用rsync或是xtrabackup方式.
到此,相信大家對“MySQL多主復制和MySQL Galera的安裝部署方法”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。