HOPES

MariaDB Galera Clusterで、MariaDBのクラスタリング環境を構築

MariaDB Galera Clusterで、MariaDBのクラスタリング環境を構築

MariaDBで、マルチマスターなクラスタDB環境を構築することができるMariaDB Galera Clusterの構築についてまとめています。

MariaDB Galera Clusterの概要

MariaDB Galera Clusterとは?

MariaDB Galera Clusterを使うと、Linux複数台にそれぞれMariaDBがインストールされた構成で、MariaDBのクラスター環境を構築することができます。

MariaDB Galera Cluster is a virtually synchronous multi-primary cluster for MariaDB. It is available on Linux only, and only supports the InnoDB storage engine.

What is MariaDB Galera Cluster? - MariaDB Knowledge Base

環境構築の前提条件

環境構築の前提条件としてあるのが、クラスタサーバの台数の条件があり、これについて、3台以上の奇数台が必要(推奨)になります。

In order to avoid a split-brain condition, the minimum recommended number of nodes in a cluster is 3. Blocking state transfer is yet another reason to require a minimum of 3 nodes in order to enjoy service availability in case one of the members fails and needs to be restarted. While two of the members will be engaged in state transfer, the remaining member(s) will be able to keep on serving client requests.

Getting Started with MariaDB Galera Cluster - MariaDB Knowledge Base

あとは、当然ですが、クラスタサーバは、お互いに通信を行なうので、ネットワークでつながっている必要があります。ここではローカルネットワークで繋がっている前提ですすめます。

MariaDB Galera Clusterのインストール

リポジトリの設定とインストール

公式のDownload MariaDB Server - MariaDB.orgから、インストールする環境を設定して、リポジトリを確認します。
ここでは、Rocky Linux8に10.6をインストールするように項目を選択します。

MariaDB

選択するとリポジトリの情報が提供されます。

Here is your custom MariaDB YUM repository entry for CentOS. Copy and paste it into a file under /etc/yum.repos.d (we suggest naming the file MariaDB.repo or something similar).

Download MariaDB Server - MariaDB.org

指定のとおりにファイルを作成します。

# vi /etc/yum.repos.d/MariaDB.repo
# MariaDB 10.6 CentOS repository list - created 2022-01-20 09:17 UTC
# https://mariadb.org/download/
[mariadb]
name = MariaDB
baseurl = https://ftp.harukasan.org/mariadb/yum/10.6/centos8-amd64
module_hotfixes=1
gpgkey=https://ftp.harukasan.org/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck=1

ファイルを設定したらインストールを行います。

# dnf install MariaDB-server
インストール済み:
  MariaDB-client-10.6.3-1.el8.x86_64                                       MariaDB-common-10.6.3-1.el8.x86_64                            
  MariaDB-server-10.6.3-1.el8.x86_64                                       MariaDB-shared-10.6.3-1.el8.x86_64                            
  boost-program-options-1.66.0-10.el8.x86_64                               galera-4-26.4.8-1.el8.x86_64                                  
  libpmem-1.6.1-1.el8.x86_64                                               lsof-4.93.2-1.el8.x86_64                                      
  perl-DBI-1.641-3.module+el8.4.0+509+59a8d9b3.x86_64                      perl-Math-BigInt-1:1.9998.11-7.el8.noarch                     
  perl-Math-Complex-1.59-420.el8.noarch                                    socat-1.7.4.1-1.el8.x86_64                                    

完了しました!

Galera Clusterの設定

設定ファイルを開いて、オプション項目等を確認しながら、クラスタ構成の設定を行います。

A number of options need to be set in order for Galera Cluster to work when using MariaDB.

Configuring MariaDB Galera Cluster - MariaDB Knowledge Base

# vi /etc/my.cnf.d/server.cnf
[galera]
# Mandatory settings
wsrep_on=ON
wsrep_node_address=10.0.0.11
wsrep_provider='/usr/lib64/galera-4/libgalera_smm.so'
wsrep_cluster_address=gcomm://10.0.0.11,10.0.0.12,10.0.0.13
wsrep_cluster_name=LILLI_CLUSTER
binlog_format=row
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
wsrep_sst_method=rsync
innodb_locks_unsafe_for_binlog=1
bind-address=0.0.0.0
wsrep_slave_threads=16
innodb_flush_log_at_trx_commit=1

クラスタ以外のその他の設定も必要に応じて設定します。

[mysqld]
character-set-server=utf8mb4
...

サーバ台数分のインストール

クラスタ構成の対象の全てのサーバでインストールを行い、wsrep_node_addressの項目等、それぞれで違う項目に注意しつつ設定を行います。

クラスターの稼働

サーバを起動して、クラスタを稼働させます。

最初のノードの起動

新しいクラスターの最初のノードの起動は、以下のコマンドを利用して起動します。

# galera_new_cluster

On operating systems that use systemd, a node can be bootstrapped in the following way:

Getting Started with MariaDB Galera Cluster - MariaDB Knowledge Base

ファイアウォールの設定

クラスターのサーバ同士で通信を行えるようにファイアウォールを設定します。MariaDB Galera Clusterの通信で必要なポートは以下のようになっています。

3306
MariaDB標準ポート
4567
Galera Clusterレプリケーション用ポート
4568
Incremental State Transsfers(IST)用ポート
4444
State Snapshot Transfers(SST)用ポート

Galera Cluster needs access to the following ports:

Configuring MariaDB Galera Cluster - MariaDB Knowledge Base

ここでは、ローカルネットワークでつながるよう設定します。

# firewall-cmd --permanent --new-zone=local
# firewall-cmd --permanent --zone=local --set-target=ACCEPT
# firewall-cmd --permanent --zone=local --add-source=10.0.0.0/24
# firewall-cmd --permanent --zone=local --add-port=3306/tcp
# firewall-cmd --permanent --zone=local --add-port=4444/tcp
# firewall-cmd --permanent --zone=local --add-port=4567/tcp
# firewall-cmd --permanent --zone=local --add-port=4568/tcp
# firewall-cmd --reload

2台目以降のノードの起動

2台目以降は、通常どおり、MariaDBを立ち上げます。

# systemctl start mariadb

クラスターの稼働の確認

クラスターの該当のサーバを全て起動したら、稼働の確認を行います。

ひとつのサーバのMariaDBにログインします。

# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 24
Server version: 10.6.3-MariaDB-log MariaDB Server

Copyright (c) 2000, 2018, 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                 | Value                                                                                                                                          |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
| wsrep_local_state_uuid        | d872c412-79e5-11ec-b68b-7af00a5c2179                                                                                                           |
| wsrep_protocol_version        | 10                                                                                                                                             |
| wsrep_last_committed          | 3                                                                                                                                              |
| wsrep_replicated              | 0                                                                                                                                              |
| wsrep_replicated_bytes        | 0                                                                                                                                              |
| wsrep_repl_keys               | 0                                                                                                                                              |
| wsrep_repl_keys_bytes         | 0                                                                                                                                              |
| wsrep_repl_data_bytes         | 0                                                                                                                                              |
| wsrep_repl_other_bytes        | 0                                                                                                                                              |
| wsrep_received                | 7                                                                                                                                              |
| wsrep_received_bytes          | 748                                                                                                                                            |
| wsrep_local_commits           | 0                                                                                                                                              |
| wsrep_local_cert_failures     | 0                                                                                                                                              |
| wsrep_local_replays           | 0                                                                                                                                              |
| wsrep_local_send_queue        | 0                                                                                                                                              |
| wsrep_local_send_queue_max    | 1                                                                                                                                              |
| wsrep_local_send_queue_min    | 0                                                                                                                                              |
| wsrep_local_send_queue_avg    | 0                                                                                                                                              |
| wsrep_local_recv_queue        | 0                                                                                                                                              |
| wsrep_local_recv_queue_max    | 2                                                                                                                                              |
| wsrep_local_recv_queue_min    | 0                                                                                                                                              |
| wsrep_local_recv_queue_avg    | 0.142857                                                                                                                                       |
| wsrep_local_cached_downto     | 1                                                                                                                                              |
| wsrep_flow_control_paused_ns  | 0                                                                                                                                              |
| wsrep_flow_control_paused     | 0                                                                                                                                              |
| wsrep_flow_control_sent       | 0                                                                                                                                              |
| wsrep_flow_control_recv       | 0                                                                                                                                              |
| wsrep_flow_control_active     | false                                                                                                                                          |
| wsrep_flow_control_requested  | false                                                                                                                                          |
| wsrep_cert_deps_distance      | 0                                                                                                                                              |
| wsrep_apply_oooe              | 0                                                                                                                                              |
| wsrep_apply_oool              | 0                                                                                                                                              |
| wsrep_apply_window            | 0                                                                                                                                              |
| wsrep_commit_oooe             | 0                                                                                                                                              |
| wsrep_commit_oool             | 0                                                                                                                                              |
| wsrep_commit_window           | 0                                                                                                                                              |
| wsrep_local_state             | 4                                                                                                                                              |
| wsrep_local_state_comment     | Synced                                                                                                                                         |
| wsrep_cert_index_size         | 0                                                                                                                                              |
| wsrep_causal_reads            | 0                                                                                                                                              |
| wsrep_cert_interval           | 0                                                                                                                                              |
| wsrep_open_transactions       | 0                                                                                                                                              |
| wsrep_open_connections        | 0                                                                                                                                              |
| wsrep_incoming_addresses      | AUTO,AUTO,AUTO                                                                                                                                 |
| wsrep_cluster_weight          | 3                                                                                                                                              |
| 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_gcomm_uuid              | d87289e3-79e5-11ec-b1b1-57f549342118                                                                                                           |
| wsrep_gmcast_segment          | 0                                                                                                                                              |
| wsrep_applier_thread_count    | 16                                                                                                                                             |
| wsrep_cluster_capabilities    |                                                                                                                                                |
| wsrep_cluster_conf_id         | 3                                                                                                                                              |
| wsrep_cluster_size            | 3                                                                                                                                              |
| wsrep_cluster_state_uuid      | d872c412-79e5-11ec-b68b-7af00a5c2179                                                                                                           |
| wsrep_cluster_status          | Primary                                                                                                                                        |
| wsrep_connected               | ON                                                                                                                                             |
| wsrep_local_bf_aborts         | 0                                                                                                                                              |
| wsrep_local_index             | 1                                                                                                                                              |
| wsrep_provider_capabilities   | :MULTI_MASTER:CERTIFICATION:PARALLEL_APPLYING:TRX_REPLAY:ISOLATION:PAUSE:CAUSAL_READS:INCREMENTAL_WRITESET:UNORDERED:PREORDERED:STREAMING:NBO: |
| wsrep_provider_name           | Galera                                                                                                                                         |
| wsrep_provider_vendor         | Codership Oy                                                                                                               |
| wsrep_provider_version        | 26.4.8(r902dd268)                                                                                                                              |
| wsrep_ready                   | ON                                                                                                                                             |
| wsrep_rollbacker_thread_count | 1                                                                                                                                              |
| wsrep_thread_count            | 17                                                                                                                                             |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
68 rows in set (0.000 sec)
@codership.com>

「wsrep_local_state_comment」が「Synced」になっていること、また、「wsrep_incoming_addresses」が、「AUTO,AUTO,AUTO」になっていること等が確認できます。

その他の設定

必要に応じて、その他の設定も行っておきます。

管理ユーザーの設定

確認ができたら、管理ユーザーの設定を行います。

# mysqladmin -u root password ********
# mysqladmin -p -u root -h localhost password ********
Enter password: 

これは、ひとつで設定すれば、クラスタ内で共有されますので、別なサーバでログイン等試せばクラスター動作の確認にもなります。

自動起動の設定

今後、MariaDBが自動起動するように設定しておきます。

# systemctl enable mariadb
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.

これで、もし、サーバダウン等が起こっても、サーバが再度起動すれば、自動的にクラスタに復帰します。

2022年01月22日に投稿されました。

2022年07月26日に更新されました。