您好,登錄后才能下訂單哦!
[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軟件時(shí)需要解決它的依賴包:boost-program-options.x86_64 (直接yum源安裝即可)
第二種方法:(rpm包安裝)三個(gè)節(jié)點(diǎn)都需要安裝
從網(wǎng)上下載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初始化 (三個(gè)節(jié)點(diǎn)都需要執(zhí)行)
安裝完成后會(huì)提示需要對mariadb進(jìn)行初始化(設(shè)置密碼)
systemctl start mariadb
mysql_secure_installation (按提示設(shè)置mysql密碼)
systemctl stop mariadb
4.配置galera
主節(jié)點(diǎn)配置文件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
將此文件復(fù)制到mariadb-2、mariadb-3,注意要把 wsrep_node_name 和 wsrep_node_address 改成相應(yīng)節(jié)點(diǎn)的 hostname 和 ip。
5.啟動(dòng)集群服務(wù):
啟動(dòng) MariaDB Galera Cluster 服務(wù):
[root@node1 ~]# /bin/galera_new_cluster
剩余兩節(jié)點(diǎn)啟動(dòng)方式為:
[root@node1 ~]# systemctl start mariadb
查看集群狀態(tài):(集群服務(wù)使用了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.驗(yàn)證集群狀態(tài):
在node1上執(zhí)行:
[root@node1 ~]# mysql -uroot -p ##進(jìn)入數(shù)據(jù)庫
查看是否啟用galera插件
連接mariadb,查看是否啟用galera插件
MariaDB [(none)]> show status like "wsrep_ready";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wsrep_ready | ON |
+---------------+-------+
1 row in set (0.004 sec)
目前集群機(jī)器數(shù)
MariaDB [(none)]> show status like "wsrep_cluster_size";
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| wsrep_cluster_size | 3 |
+--------------------+-------+
1 row in set (0.001 sec)
查看集群狀態(tài)
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 集群唯一標(biāo)記
| wsrep_cluster_status | Primary | ##主服務(wù)器
| wsrep_commit_oooe | 0.000000 |
| wsrep_commit_oool | 0.000000 |
| wsrep_commit_window | 1.000000 |
| wsrep_connected | ON | ##當(dāng)前是否連接中
| 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 | ##連接中的數(shù)據(jù)庫
| wsrep_last_committed | 9 | ##sql 提交記錄
| wsrep_local_bf_aborts | 0 | ##從執(zhí)行事務(wù)過程被本地中斷
| wsrep_local_cached_downto | 5 |
| wsrep_local_cert_failures | 0 | ##本地失敗事務(wù)
| wsrep_local_commits | 4 | ##本地執(zhí)行的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 | ##本地發(fā)出的隊(duì)列
| wsrep_local_send_queue_avg | 0.000000 | ##隊(duì)列平均時(shí)間間隔
| 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 | ##插件是否應(yīng)用中
| wsrep_received | 35 | ##數(shù)據(jù)復(fù)制接收次數(shù)
| 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 | ##隨著復(fù)制發(fā)出的次數(shù)
| wsrep_replicated_bytes | 1600 | ##數(shù)據(jù)復(fù)制發(fā)出的字節(jié)數(shù)
| wsrep_thread_count | 2 |
+------------------------------+----------------------------------------------------------------+
58 rows in set (0.003 sec)
查看連接的主機(jī)
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數(shù)據(jù)是否同步
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)
在其他兩個(gè)節(jié)點(diǎn)上可以查看lizk庫已經(jīng)同步。
8.模擬腦裂后的處理
下面模擬在網(wǎng)絡(luò)抖動(dòng)發(fā)生丟包的情況下,兩個(gè)節(jié)點(diǎn)失聯(lián)導(dǎo)致腦裂。在192.168.153.143和192.168.153.144兩個(gè)節(jié)點(diǎn)上分別執(zhí)行:
iptables -A INPUT -p tcp --sport 4567 -j DROP
iptables -A INPUT -p tcp --dport 4567 -j DROP
以上命令用來禁止wsrep全同步復(fù)制4567端口通信
在192.168.153.142節(jié)點(diǎn)上查看:
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 |
現(xiàn)在已經(jīng)出現(xiàn)腦裂的情況,并且集群無法執(zhí)行任何命令。
為了解決這個(gè)問題,可以執(zhí)行
set global wsrep_provider_options="pc.bootstrap=true";
通過這個(gè)命令來強(qiáng)制恢復(fù)出現(xiàn)腦裂的節(jié)點(diǎn)。
驗(yàn)證:
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)
最后我們將節(jié)點(diǎn)192.168.153.143和192.168.153.144恢復(fù)一下,只要清理一下iptables表即可(因?yàn)槲业氖菧y試環(huán)境,生產(chǎn)環(huán)境需要?jiǎng)h除上面的規(guī)則即可):
[root@node3 mysql]# iptables -F
恢復(fù)后驗(yàn)證一下:
MariaDB [(none)]> show status like "wsrep_cluster_size";
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| wsrep_cluster_size | 3 |
+--------------------+-------+
1 row in set (0.001 sec)
9.因故障需要對集群的兩個(gè)節(jié)點(diǎn)進(jìn)行停機(jī)檢查,重啟服務(wù)后是否能同步數(shù)據(jù);
對192.168.153.143和192.168.153.144執(zhí)行停止mariadb的操作:
[root@node2 mysql]# systemctl stop mariadb
在192.168.153.142節(jié)點(diǎn)上插入數(shù)據(jù):
MariaDB [test]> select * from test1;
+------+
| id |
+------+
| 2 |
| 2 |
| 1 |
| 3 |
+------+
4 rows in set (0.007 sec)
現(xiàn)在把集群中另外兩個(gè)節(jié)點(diǎn)重新啟動(dòng),查看數(shù)據(jù)一致性情況,跟主節(jié)點(diǎn)的數(shù)據(jù)一樣。
10.異常處理:當(dāng)機(jī)房突然停電,所有g(shù)alera主機(jī)都非正常關(guān)機(jī),來電后開機(jī),會(huì)導(dǎo)致galera集群服務(wù)無法正常啟動(dòng)。如何處理?
第1步:開啟galera集群的群主主機(jī)的mariadb服務(wù)。
第2步:開啟galera集群的成員主機(jī)的mariadb服務(wù)。
異常處理:galera集群的群主主機(jī)和成員主機(jī)的mysql服務(wù)無法啟動(dòng),如何處理?
解決方法一:第1步、刪除garlera群主主機(jī)的/var/lib/mysql/grastate.dat狀態(tài)文件
/bin/galera_new_cluster啟動(dòng)服務(wù)。啟動(dòng)正常。登錄并查看wsrep狀態(tài)。
第2步:刪除galera成員主機(jī)中的/var/lib/mysql/grastate.dat狀態(tài)文件
systemctl restart mariadb重啟服務(wù)。啟動(dòng)正常。登錄并查看wsrep狀態(tài)。
解決方法二:第1步、修改garlera群主主機(jī)的/var/lib/mysql/grastate.dat狀態(tài)文件中的0為1
/bin/galera_new_cluster啟動(dòng)服務(wù)。啟動(dòng)正常。登錄并查看wsrep狀態(tài)。
第2步:修改galera成員主機(jī)中的/var/lib/mysql/grastate.dat狀態(tài)文件中的0為1
systemctl restart mariadb重啟服務(wù)。啟動(dòng)正常。登錄并查看wsrep狀態(tài)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。