MariaDB Galera Cluster部署實踐

官方文檔:

http://galeracluster.com/documentation-webpages/index.html

一、 Galera Cluster的工作原理

主要關注點是數據一致性。 事務既可以應用於每個節點,也可以不全部應用。 所以,只要它們配置正確,數據庫保持同步。

Galera複製插件不同於傳統的MySQL複製,可以解決多個問題,包括多主寫入衝突,複製滯後和主從不同步。

MariaDB Galera Cluster部署實踐

在典型的Galera集羣實例中,應用程序可以寫入集羣中的任何節點,然後通過基於認證的複製將事務提交(RBR事件)應用於所有服務器。

使用組通信和事務排序技術,基於認證的複製是同步數據庫複製的另一種方法

二、Galera集羣安裝

說明:Galera集羣至少需要三個節點的服務器硬件。

node-12:10.71.11.12
node-13:10.71.11.13
node-14:10.71.11.14

操作系統:

centos7

內核版本:

 3.10.0-693.21.1.el7.x86_64

開始安裝

說明:爲集羣中的每個節點執行以下步驟。文檔以node-12配置爲例
1.編輯/etc/hosts文件,配置節點互相解析

[root@node-12 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.71.11.12  node-12
10.71.11.13  node-13
10.71.11.14  node-14

2.關閉節點防火牆

[root@node-12 ~]# cat /etc/sysconfig/selinux |grep di
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
#     minimum - Modification of targeted policy. Only selected processes are protected.

3.配置Galera集羣yum源

[root@node-12 ~]# cat /etc/yum.repos.d/galera.repo 
[galera]
name = Galera
baseurl = http://releases.galeracluster.com/galera-3/centos/7/x86_64
gpgkey = http://releases.galeracluster.com/GPG-KEY-galeracluster.com
gpgcheck = 1
[mysql-wsrep]
name = MySQL-wsrep
baseurl =  http://releases.galeracluster.com/mysql-wsrep-5.6/centos/7/x86_64
gpgkey = http://releases.galeracluster.com/GPG-KEY-galeracluster.com
gpgcheck = 1

4.更新yum源緩存

yum makecache

5.安裝服務

yum install mysql-wsrep-shared-5.6

說明:如果節點上之前安裝過mysql,執行上面命令可能會安裝包衝突報錯
6.以node-12爲例,編輯/etc/my.cnf,添加如下配置

[root@node-12 ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
binlog_format=row
bind-address=10.71.11.12
default_storage_engine=innodb
innodb_autoinc_lock_mode=2
innodb_flush_log_at_trx_commit=0
innodb_buffer_pool_size=122M
wsrep_provider=/usr/lib64/galera-3/libgalera_smm.so
wsrep_provider_options="gcache.size=300M; gcache.page_size=300M"
wsrep_sst_method=rsync
wsrep_cluster_name=MyCluster
wsrep_cluster_address="gcomm://10.71.11.12,10.71.11.13,10.71.11.14"
wsrep_node_name=node-12
wsrep_node_address="10.71.11.12"
[mysql_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

說明:其他另外node-13和node-14依據1-6步配置,需要修改/etc/my.cnf與每個節點對應

系統內存配置說明

Galera Cluster的內存要求很難準確預測。 它使用的特定內存量可能會有很大差異,具體取決於給定節點接收的負載。 如果Galera Cluster嘗試使用比節點更多的內存,則mysqld實例會崩潰。
保護節點系統崩潰的方法是確保服務有足夠的swap 空間分區或者交換文件

檢查系統可用交換空間
MariaDB Galera Cluster部署實踐
MariaDB Galera Cluster部署實踐
MariaDB Galera Cluster部署實踐

如果系統swap分區不滿足環境需求,可以進行如下操作配置swap分區
說明:集羣的中每個節點都要進行相關swap分區設置

1.根據需求在根目錄下創建一個2G大小swap空文件

[root@node-11 ~]#fallocate -l 2048M /swapfile

或者執行

[root@node-11 ~]#dd if=/dev/zero of=/swapfile bs=1M count=2048

設置swap交換分區大小
2.設置swap文件權限

[root@node-11 ~]#chown 600 /swapfile
ll /swapfile 
-rw-r--r-- 1 600 root 2147483648 Apr 17 09:13 /swapfile

3.格式化swapfile

[root@node-11 ~]# mkswap /swapfile
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=6341a320-1804-4fe1-8c96-7c22fe270eeb

4 .激活swapfile

[root@node-11 ~]# swapon /swapfile
swapon: /swapfile: insecure permissions 0644, 0600 suggested.
swapon: /swapfile: insecure file owner 600, 0 (root) suggested.
  1. 編輯/etc/fstab,在文件底部加入如下配置
    /swapfile none swap defaults 0 0

    MariaDB Galera Cluster部署實踐
    6.驗證swap交換分區配置文件是否成功

    [root@node-11 ~]# swapon --summary
    Filename                Type        Size    Used    Priority
    /swapfile                               file    2097148 0   -1

    說明:由於現在部署Galera集羣的節點環境部署過其他環境,這裏說下刪除swap分區的步驟

[root@node-11 ~]# swapon --summary
Filename                Type        Size    Used    Priority
/openstack/swap.img                     file    4194300 0   0
/swapfile                               file    2097148 0   -1
[root@node-11 ~]# swapoff /openstack/swap.img   ##首先停止swap分區
[root@node-11 ~]# rm -rf /openstack/swap.img  ##刪除swap分區文件

[root@node-11 ~]# swapon --summary
Filename                Type        Size    Used    Priority
/swapfile                               file    2097148 0   -1

MariaDB Galera Cluster部署實踐

三、啓動Galera集羣

說明:完成安裝和配置Galera Cluster後,需要使用--wsrep-new-cluster選擇一個節點上啓動mysqld。 這將會初始化集羣的核心組件。 在此之後啓動的每個節點都將連接到該組件並開始複製
1.在node-12上使用參數--wsrep-new-cluster啓動數據庫服務

[root@node-12 mysql]# service mysql start --wsrep-new-cluster

MariaDB Galera Cluster部署實踐
數據庫初始化報錯解決辦法,刪除/var/lib/mysq/目錄下的logfile
MariaDB Galera Cluster部署實踐
注意警告:只有在初始化主要組件時才使用--wsrep-new-cluster參數。 如果希望節點連接到現有羣集時,不要執行這條命令。
2.在node-12節點啓動數據庫服務後,登錄mysql數據庫庫,通過wsrep_cluster_size參數來判斷啓動是否成功。

SHOW STATUS LIKE 'wsrep_cluster_size';

MariaDB Galera Cluster部署實踐
說明:參數wsrep_cluster_size數值代表目前galera集羣當前節點在線數

3.向galera集羣添加節點,分別在node-12和node-13上執行下面命令啓動數據庫服務

[root@node-13 mysql]# service mysql start
Starting MySQL.Logging to '/var/lib/mysql/node-13.err'.
....... SUCCESS!
[root@node-14 yum.repos.d]# service mysql start
Starting MySQL.Logging to '/var/lib/mysql/node-14.err'.
...... SUCCESS!

然後node-13或者node-14上登錄mysql數據庫,查看wsrep_cluster_size的數值

[root@node-14 yum.repos.d]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.39 MySQL Community Server (GPL), wsrep_25.22

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> SHOW STATUS LIKE 'wsrep_cluster_size';

MariaDB Galera Cluster部署實踐
到此,galera集羣搭建成功

四、Galera集羣複製測試

說明:要測試Galera羣集是否正常工作
1.在數據庫客戶端上,驗證所有節點是否已相互連接

[root@node-12 ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.39 MySQL Community Server (GPL), wsrep_25.22

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show status like 'wsrep_%';

MariaDB Galera Cluster部署實踐

MariaDB Galera Cluster部署實踐

參數說明:
wsrep_local_state_comment參數中Synced表示該節點已經連接到集羣並能正常運行
wsrep_cluster_size的數值3表示galera集羣中有3個節點都在線
wsrep_ready中on表示此節點連接到集羣並能夠處理事務。

2.在node-12上創建一個表並插入數據

[root@node-12 mysql]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.39 MySQL Community Server (GPL), wsrep_25.22

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> CREATE DATABASE galeratest;
Query OK, 1 row affected (0.01 sec)

mysql> USE galeratest;
Database changed
mysql> CREATE TABLE test_table (id INT PRIMARY KEY AUTO_INCREMENT,msg TEXT ) ENGINE=InnoDB;
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO test_table (msg) VALUES ("Hello my dear cluster.");
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO test_table (msg) VALUES ("Hello, again, cluster dear."); 
Query OK, 1 row affected (0.00 sec)
  1. 在node-13或者ndoe-14查看三個節點能夠同步node-12上的數據操作
    
    [root@node-13 mysql]# mysql
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 4
    Server version: 5.6.39 MySQL Community Server (GPL), wsrep_25.22

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| galeratest |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
mysql> use galeratest;
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
mysql> SELECT * FROM test_table;
+----+-----------------------------+
| id | msg |
+----+-----------------------------+
| 1 | Hello my dear cluster. |
| 4 | Hello, again, cluster dear. |
+----+-----------------------------+
2 rows in set (0.00 sec)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章