CentOS 7 - MariaDB Galera Cluster をインストールしてみました。
MariaDBを使った、データベースの冗長化に「MariaDB Galera Cluster」というがあります。
データベースの冗長化(クラスタ)は、構築が面倒なものばかりでしたが MariaDB Galera Cluster は非常に簡単です。
また、Galera Cluster に参加するノードは全てマスターとして動作するため、どのノードでもデータベースの更新作業を行うことができます。
本投稿では、MariaDB Galera Cluster の構築方法を紹介します。
作業環境
- OS: CentOS Linux release 7.1.1503 (Core) - minimal(最小構成)インストール - 仮想サーバ(KVM) x2 - db1 192.168.5.5 - db2 192.168.5.6
目次
1.DBサーバの準備
2.Galera Server のインストール
3.Galera Server の起動
4.Galera Cluster にノードを追加
5.Galera Cluster の動作確認
1.準備
Firewalldの無効化(db1,db2)
Firewalldに詳しい方は、無効化する必要はありません。3306、4444、4567の3つのポートを開放してください。
# systemctl stop firewalld # systemctl disable firewalld
Selinuxを無効化(db1,db2)
Selinuxを無効します。以下のコマンドだけでいいですが、再起動することをオススメします。
# setenforce 0 # sed -i "s/^SELINUX\=enforcing/SELINUX\=disabled/g" /etc/selinux/config
MariaDBをアンインストール(db1,db2)
MariaDBが既にインストールされている場合は、アンインストールします。MariaDBとGalera serverは、共存できません。
# yum remove mariadb-server mariadb
2.MariaDB Galera Cluster のインストール
リポジトリの追加(db1,db2)
Galera Cluster 用のリポジトリを追加します。
# vim /etc/yum.repos.d/MariaDB.repo
# MariaDB 10.0 CentOS repository list - created 2015-08-03 03:28 UTC # http://mariadb.org/mariadb/repositories/ [mariadb] name = MariaDB baseurl = http://yum.mariadb.org/10.0/centos7-amd64 gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB enabled=1 gpgcheck=1
Galera Server のインストール(db1,db2)
Galera Server をインストールします。
# yum install MariaDB-Galera-server MariaDB-client galera
MariaDB Galera Clusterの設定(db1,db2)
「server.cnf」に MariaDB Galera Cluster の設定を追記します。設定は[mysqld]セッションに記述してください。
# vim /etc/my.cnf.d/server.cnf
〜 [mysqld] wsrep_cluster_name=DBCLUSTER wsrep_cluster_address=gcomm://192.168.5.6 wsrep_node_address=192.168.5.5 wsrep_provider='/usr/lib64/galera/libgalera_smm.so' wsrep_sst_method=rsync wsrep_slave_threads=4 default_storage_engine=InnoDB binlog_format=ROW innodb_autoinc_lock_mode=2 innodb_locks_unsafe_for_binlog=1 〜
※ 設定項目の「wsrep_node_address」は、db1とdb2でサーバ自身のIPアドレスを設定してください。
3.Galera Server の起動
ノードの起動(db1)
初期ノード(db1)で Galera Server を起動します。オプションに「--wsrep_cluster_address=gcomm://」をつけます。オプションをつけることで server.cnf に記述したクラスタに接続せずに起動できます。
# service mysql start --wsrep_cluster_address=gcomm:// Starting MySQL.. SUCCESS! ※ Galera Server は、Systemdに対応していません。
併せて、自動起動設定を有効にします。
# chkconfig mysql on
ユーザの登録(db1)
Galera Server に新規ユーザを作成し、パスワードを設定します。
「新規ユーザ:root」、「パスワード:hoge123」 にします。
# mysql -e "grant all privileges on *.* to root@'%' identified by 'hoge123' with grant option;" # mysql -e "grant all privileges on *.* to root@localhost identified by 'hoge123' with grant option;"
Galera Server の設定確認
Galera Server にログインを行い、「wsrep_cluster_status」が Primary になっていることを確認します。
# mysql -u root -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 12 Server version: 10.0.21-MariaDB-wsrep MariaDB Server, wsrep_25.10.r4144 Copyright (c) 2000, 2015, 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_cluster_status'; +----------------------+---------+ | Variable_name | Value | +----------------------+---------+ | wsrep_cluster_status | Primary | +----------------------+---------+ 1 row in set (0.00 sec) MariaDB [(none)]> \q Bye
4.Galera Cluster にノードを追加
追加ノードを起動(db2)
初期ノードと同様、オプションに「--wsrep_cluster_address=gcomm://」をつけて Galera Server を起動します。
# service mysql start --wsrep_cluster_address=gcomm:// Starting MySQL.. SUCCESS!
併せて、自動起動設定を有効にします。
# chkconfig mysql on
ユーザの登録(db2)
初期ノードと同じ、ユーザ名とパスワードを設定します。
「新規ユーザ:root」、「パスワード:hoge123」 です。
# mysql -e "grant all privileges on *.* to root@'%' identified by 'hoge123' with grant option;" # mysql -e "grant all privileges on *.* to root@localhost identified by 'hoge123' with grant option;"
Galera Clusterに追加(db2)
ユーザ作成が完了し終えたら、Galera Server を再起動します。クラスタに参加させるため「--wsrep_cluster_address=gcomm://」オプションはつけません。
# service mysql restart Shutting down MySQL.... SUCCESS! Starting MySQL...SST in progress, setting sleep higher. SUCCESS!
「Starting MySQL...SST in progress, setting sleep higher. SUCCESS!」と表示されれば成功です。
Galera 動作確認(db1,db2)
Galera Server にログインして、ステータスを確認します。
# mysql -u root -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 12 Server version: 10.0.21-MariaDB-wsrep MariaDB Server, wsrep_25.10.r4144 Copyright (c) 2000, 2015, 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 | c265c656-407f-11e5-91d1-2788d8316ba9 | | wsrep_protocol_version | 7 | | wsrep_last_committed | 2 | | 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 | 3 | | wsrep_received_bytes | 212 | | 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.000000 | | wsrep_local_recv_queue | 0 | | wsrep_local_recv_queue_max | 1 | | wsrep_local_recv_queue_min | 0 | | wsrep_local_recv_queue_avg | 0.000000 | | wsrep_local_cached_downto | 18446744073709551615 | | wsrep_flow_control_paused_ns | 0 | | wsrep_flow_control_paused | 0.000000 | | wsrep_flow_control_sent | 0 | | wsrep_flow_control_recv | 0 | | wsrep_cert_deps_distance | 0.000000 | | wsrep_apply_oooe | 0.000000 | | wsrep_apply_oool | 0.000000 | | wsrep_apply_window | 0.000000 | | wsrep_commit_oooe | 0.000000 | | wsrep_commit_oool | 0.000000 | | wsrep_commit_window | 0.000000 | | wsrep_local_state | 4 | | wsrep_local_state_comment | Synced | | wsrep_cert_index_size | 0 | | wsrep_causal_reads | 0 | | wsrep_cert_interval | 0.000000 | | wsrep_incoming_addresses | 192.168.5.6:3306,192.168.5.5:3306 | | wsrep_evs_delayed | | | wsrep_evs_evict_list | | | wsrep_evs_repl_latency | 0/0/0/0/0 | | wsrep_evs_state | OPERATIONAL | | wsrep_gcomm_uuid | bec6eaaa-4082-11e5-a954-c2bce2061581 | | wsrep_cluster_conf_id | 6 | | wsrep_cluster_size | 2 | | wsrep_cluster_state_uuid | c265c656-407f-11e5-91d1-2788d8316ba9 | | wsrep_cluster_status | Primary | | wsrep_connected | ON | | wsrep_local_bf_aborts | 0 | | wsrep_local_index | 0 | | wsrep_provider_name | Galera | | wsrep_provider_vendor | Codership Oy <info@codership.com> | | wsrep_provider_version | 25.3.9(r3387) | | wsrep_ready | ON | | wsrep_thread_count | 5 | +------------------------------+--------------------------------------+ 57 rows in set (0.01 sec) MariaDB [(none)]> \q Bye
「wsrep_incoming_addresses」にIPアドレスが2つ表示されていれば、クラスタとして機能しています。
5.Galera Cluster の動作確認
動作確認
まず、Galera Clusterにログインしてデータベースを確認します。
[root@db1 ~]# mysql -u root -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 12 Server version: 10.0.21-MariaDB-wsrep MariaDB Server, wsrep_25.10.r4144 Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ 3 rows in set (0.00 sec)
つぎにデータベース(test)を新規作成し、確認します。
MariaDB [(none)]> create database test; Query OK, 1 row affected (0.01 sec) MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.00 sec)
クラスタが正常に機能していれば、追加ノード(db2)でもデータベース(test)が作成できているはずです。
[root@db2 ~]# mysql -u root -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 12 Server version: 10.0.21-MariaDB-wsrep MariaDB Server, wsrep_25.10.r4144 Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.00 sec)
追加ノードでもデータベースが作成されていることが確認できました。