CentOS 7.4部署MariaDB Galera Cluster叢集架構

來源:互聯網
上載者:User

CentOS 7.4部署MariaDB Galera Cluster叢集架構

Mariadb galera Cluster安裝:
作業系統:CentOS 7.4版本
叢集數量:3個節點
主機資訊: 192.168.153.142 node1 selinux=disabled firewalld關閉
192.168.153.143 node2 selinux=disabled firewalld關閉
192.168.153.144 node3 selinux=disabled firewalld關閉

搭建步驟

1.主機之間互相解析:三台節點都要執行
vim /etc/hosts
192.168.153.142 node1
192.168.153.143 node2
192.168.153.144 node3

2.安裝軟體包

第一種方法:(yum install -y MariaDB-server MariaDB-client galera)
配置yum安裝源和配置mariadb galera安裝源
yum源配置掛iso
設定mariadb的yum源並安裝(所有節點都要)
修改yum源檔案

vi /etc/yum.repos.d/mariadb.repo

[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.3.5/centos74-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
enabled=0
安裝galera軟體時需要解決它的依賴包:boost-program-options.x86_64 (直接yum源安裝即可)

第二種方法:(rpm包安裝)三個節點都需要安裝
從網上下載rpm包:  galera-25.3.23-1.rhel7.el7.centos.x86_64.rpm
                    MariaDB-10.3.5-centos74-x86_64-client.rpm
                    MariaDB-10.3.5-centos74-x86_64-compat.rpm
                    MariaDB-10.3.5-centos74-x86_64-common.rpm
                    MariaDB-10.3.5-centos74-x86_64-server.rpm
rpm -ivh MariaDB-10.3.5-centos74-x86_64-compat.rpm --nodeps
rpm -ivh MariaDB-10.3.5-centos74-x86_64-common.rpm
rpm -ivh MariaDB-10.3.5-centos74-x86_64-client.rpm
yum install -y boost-program-options.x86_64 (解決安裝galera的依賴包)
rpm -ivh galera-25.3.23-1.rhel7.el7.centos.x86_64.rpm
rpm -ivh MariaDB-10.3.5-centos74-x86_64-server.rpm

3.mariadb初始化 (三個節點都需要執行)
安裝完成後會提示需要對mariadb進行初始化(設定密碼)
systemctl start mariadb
mysql_secure_installation (按提示設定mysql密碼)
systemctl stop mariadb

4.配置galera
主節點設定檔server.cnf
vim /etc/my.cnf.d/server.cnf
[galera]
wsrep_on=ON
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_address="gcomm://192.168.153.142,192.168.153.143,192.168.153.144"
wsrep_node_name= node1
wsrep_node_address=192.168.153.142
binlog_format=row
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
wsrep_slave_threads=1
innodb_flush_log_at_trx_commit=0
innodb_buffer_pool_size=120M
wsrep_sst_method=rsync
wsrep_causal_reads=ON
將此檔案複製到mariadb-2、mariadb-3,注意要把 wsrep_node_name 和 wsrep_node_address 改成相應節點的 hostname 和 ip。

5.啟動叢集服務:
啟動 MariaDB Galera Cluster 服務:
[root@node1 ~]# /bin/galera_new_cluster
剩餘兩節點啟動方式為:
[root@node1 ~]# systemctl start mariadb
查看叢集狀態:(叢集服務使用了4567和3306連接埠))
[root@node1 ~]# netstat -tulpn | grep -e 4567 -e 3306
tcp 0 0 0.0.0.0:4567 0.0.0.0: LISTEN 3557/mysqld
tcp6 0 0 :::3306 ::: LISTEN 3557/mysqld

6.驗證叢集狀態:
在node1上執行:
[root@node1 ~]# mysql -uroot -p ##進入資料庫
查看是否啟用galera外掛程式
串連mariadb,查看是否啟用galera外掛程式
MariaDB [(none)]> show status like "wsrep_ready";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wsrep_ready | ON |
+---------------+-------+
1 row in set (0.004 sec)
目前叢集機器數
MariaDB [(none)]> show status like "wsrep_cluster_size";
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| wsrep_cluster_size | 3 |
+--------------------+-------+
1 row in set (0.001 sec)
查看叢集狀態
MariaDB [(none)]> show status like "wsrep%";
+------------------------------+----------------------------------------------------------------+
| Variable_name | Value |
+------------------------------+----------------------------------------------------------------+
| wsrep_apply_oooe | 0.000000 |
| wsrep_apply_oool | 0.000000 |
| wsrep_apply_window | 1.000000 |
| wsrep_causal_reads | 14 |
| wsrep_cert_deps_distance | 1.200000 |
| wsrep_cert_index_size | 3 |
| wsrep_cert_interval | 0.000000 |
| wsrep_cluster_conf_id | 22 |
| wsrep_cluster_size | 3 | ##叢集成員
| wsrep_cluster_state_uuid | b8ecf355-233a-11e8-825e-bb38179b0eb4 | ##UUID 叢集唯一標記
| 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 | 0eba3aff-2341-11e8-b45a-f277db2349d5 |
| wsrep_incoming_addresses | 192.168.153.142:3306,192.168.153.143:3306,192.168.153.144:3306 | ##串連中的資料庫
| wsrep_last_committed | 9 | ##sql 提交記錄
| wsrep_local_bf_aborts | 0 | ##從執行事務過程被本地中斷
| wsrep_local_cached_downto | 5 |
| wsrep_local_cert_failures | 0 | ##本地失敗事務
| wsrep_local_commits | 4 | ##本地執行的sql
| wsrep_local_index | 0 |
| wsrep_local_recv_queue | 0 |
| wsrep_local_recv_queue_avg | 0.057143 |
| wsrep_local_recv_queue_max | 2 |
| 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 | b8ecf355-233a-11e8-825e-bb38179b0eb4 | ##叢集ID
| wsrep_protocol_version | 8 |
| wsrep_provider_name | Galera |
| wsrep_provider_vendor | Codership Oy <info@codership.com> |
| wsrep_provider_version | 25.3.23(r3789) |
| wsrep_ready | ON | ##外掛程式是否應用中
| wsrep_received | 35 | ##資料複製接收次數
| wsrep_received_bytes | 5050 |
| wsrep_repl_data_bytes | 1022 |
| wsrep_repl_keys | 14 |
| wsrep_repl_keys_bytes | 232 |
| wsrep_repl_other_bytes | 0 |
| wsrep_replicated | 5 | ##隨著複製發出的次數
| wsrep_replicated_bytes | 1600 | ##資料複製發出的位元組數
| wsrep_thread_count | 2 |
+------------------------------+----------------------------------------------------------------+
58 rows in set (0.003 sec)
查看串連的主機
MariaDB [(none)]> show status like "wsrep_incoming_addresses";
+--------------------------+----------------------------------------------------------------+
| Variable_name | Value |
+--------------------------+----------------------------------------------------------------+
| wsrep_incoming_addresses | 192.168.153.142:3306,192.168.153.143:3306,192.168.153.144:3306 |
+--------------------------+----------------------------------------------------------------+
1 row in set (0.002 sec)

7.測試叢集mariad資料是否同步
MariaDB [(none)]> create database lizk;
Query OK, 1 row affected (0.010 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database          |
+--------------------+
| china              |
| hello              |
| hi                |
| information_schema |
| lizk              |
| mysql              |
| performance_schema |
| test              |
+--------------------+
8 rows in set (0.001 sec)
在其他兩個節點上可以查看lizk庫已經同步。

8.類比腦裂後的處理
下面類比在網路抖動發生丟包的情況下,兩個節點失聯導致腦裂。在192.168.153.143和192.168.153.144兩個節點上分別執行:
iptables -A INPUT -p tcp --sport 4567 -j DROP
iptables -A INPUT -p tcp --dport 4567 -j DROP
以上命令用來禁止wsrep全同步複製4567連接埠通訊
在192.168.153.142節點上查看:
MariaDB [(none)]> show status like "ws%";
+------------------------------+--------------------------------------------+
| Variable_name | Value |
+------------------------------+--------------------------------------------+
| wsrep_apply_oooe | 0.000000 |
| wsrep_apply_oool | 0.000000 |
| wsrep_apply_window | 1.000000 |
| wsrep_causal_reads | 16 |
| wsrep_cert_deps_distance | 1.125000 |
| wsrep_cert_index_size | 3 |
| wsrep_cert_interval | 0.000000 |
| wsrep_cluster_conf_id | 18446744073709551615 |
| wsrep_cluster_size | 1 |
| wsrep_cluster_state_uuid | b8ecf355-233a-11e8-825e-bb38179b0eb4 |
| wsrep_cluster_status | non-Primary |
現在已經出現腦裂的情況,並且叢集無法執行任何命令。
為瞭解決這個問題,可以執行
set global wsrep_provider_options="pc.bootstrap=true";
通過這個命令來強制恢複出現腦裂的節點。
驗證:
MariaDB [(none)]> set global wsrep_provider_options="pc.bootstrap=true";
Query OK, 0 rows affected (0.015 sec)

MariaDB [(none)]> select @@wsrep_node_name;
+-------------------+
| @@wsrep_node_name |
+-------------------+
| node1            |
+-------------------+
1 row in set (0.478 sec)
最後我們將節點192.168.153.143和192.168.153.144恢複一下,只要清理一下iptables表即可(因為我的是測試環境,生產環境需要刪除上面的規則即可):
[root@node3 mysql]# iptables -F
恢複後驗證一下:
MariaDB [(none)]> show status like "wsrep_cluster_size";
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| wsrep_cluster_size | 3    |
+--------------------+-------+
1 row in set (0.001 sec)

9.因故障需要對叢集的兩個節點進行停機檢查,重啟服務後是否能同步資料;
對192.168.153.143和192.168.153.144執行停止mariadb的操作:
[root@node2 mysql]# systemctl stop mariadb
在192.168.153.142節點上插入資料:
MariaDB [test]> select * from test1;
+------+
| id |
+------+
| 2 |
| 2 |
| 1 |
| 3 |
+------+
4 rows in set (0.007 sec)
現在把叢集中另外兩個節點重新啟動,查看資料一致性情況,跟主節點的資料一樣。

10.異常處理:當機房突然停電,所有galera主機都非順利關機,來電後開機,會導致galera叢集服務無法正常啟動。如何處理?
第1步:開啟galera叢集的群主主機的mariadb服務。
第2步:開啟galera叢集的成員主機的mariadb服務。
異常處理:galera叢集的群主主機和成員主機的mysql服務無法啟動,如何處理?
解決方案一:第1步、刪除garlera群主主機的/var/lib/mysql/grastate.dat狀態檔案
/bin/galera_new_cluster啟動服務。啟動正常。登入並查看wsrep狀態。
第2步:刪除galera成員主機中的/var/lib/mysql/grastate.dat狀態檔案
systemctl restart mariadb重啟服務。啟動正常。登入並查看wsrep狀態。
解決方案二:第1步、修改garlera群主主機的/var/lib/mysql/grastate.dat狀態檔案中的0為1
/bin/galera_new_cluster啟動服務。啟動正常。登入並查看wsrep狀態。
第2步:修改galera成員主機中的/var/lib/mysql/grastate.dat狀態檔案中的0為1
systemctl restart mariadb重啟服務。啟動正常。登入並查看wsrep狀態。

本文永久更新連結地址:https://www.bkjia.com/Linux/2018-03/151302.htm

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.