LoginSignup
17
16

More than 5 years have passed since last update.

VagrantとAnsibleでMariaDBのGaleraClusterを試す

Last updated at Posted at 2014-10-13

MariaDBのGalera ReplicationはよくあるMySQLのマスタースレーブ構成のレプリケーションとは異なり、すべてのサーバーがマスターとして動作し、同期的にレプリケーションを行うことができます。

通常、マスターがダウンした際のフェイルオーバーに関してはMHAを使うなり、クラスタを組むなりする必要がありますが、Galera Replicationを使えばその辺りを気にする必要はほぼありません。

こういったシェアードナッシングなクラスタの場合、スプリットブレインが問題になりますので、3台以上での構成が推奨されています。
3台のうち1台が他のサーバーと疎通できなくなった場合、孤立した1台はすべてのクエリを受け付けないことで整合性を保ちます。
そのため、今回はサーバー3台構成とします。
すべてを起動するとトータルで3GBほどメモリを使用しますので、ご注意ください。

設定から起動までAnsibleで自動化してますので、Vagrantから起動したら適当なサーバーに対して接続するだけで動作が確認できます。

準備

VirtualBox、Vagrant、Ansibleはあらかじめインストールしておき、ターミナル上でコマンドが実行できるようにしておいてください。

OSはCentOSを使用します。
以下のコマンドでCentOS6.5のボックスを追加します。
ボックスの追加は不要になりました。

$ vagrant box add centos65
http://www.lyricalsoftware.com/downloads/centos65.box

適当なディレクトリで今回使うファイル一式をcloneします。
vagrant upですべてのサーバーが起動し、インストールが行われます。
3台すべてのセットアップが終わるまで待ちます。

$ git clone https://github.com/hakuro/vagrant-mariadb-galera.git
$ cd vagrant-mariadb-galera
$ vagrant up

動作チェック

適当なサーバにログインして動作を確認してみます。

$ vagrant ssh mariadb1
mariadb1:$ mysql -u root
MariaDB> show status like 'wsrep_%';
+------------------------------+----------------------------------------------------------+
| Variable_name                | Value                                                    |
+------------------------------+----------------------------------------------------------+
| wsrep_local_state_uuid       | 45ac8b7d-52bd-11e4-b756-dfc3786e45f5                     |
| wsrep_protocol_version       | 5                                                        |
| wsrep_last_committed         | 0                                                        |
| 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               | 10                                                       |
| wsrep_received_bytes         | 768                                                      |
| wsrep_local_commits          | 0                                                        |
| wsrep_local_cert_failures    | 0                                                        |
| wsrep_local_replays          | 0                                                        |
| wsrep_local_send_queue       | 0                                                        |
| wsrep_local_send_queue_avg   | 0.000000                                                 |
| wsrep_local_recv_queue       | 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.50.11:3306,192.168.50.12:3306,192.168.50.13:3306 |
| wsrep_cluster_conf_id        | 3                                                        |
| wsrep_cluster_size           | 3                                                        |
| wsrep_cluster_state_uuid     | 45ac8b7d-52bd-11e4-b756-dfc3786e45f5                     |
| 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.5 (rXXXX)                                            |
| wsrep_ready                  | ON                                                       |
| wsrep_thread_count           | 2                                                        |
+------------------------------+----------------------------------------------------------+
48 rows in set (0.00 sec)

MariaDB> use test;
Database changed

MariaDB> create table sample (id integer primary key, text varchar(128));
Query OK, 0 rows affected (0.37 sec)

MariaDB> insert into sample values (1, 'aaaaa');
Query OK, 1 row affected (0.01 sec)

他のサーバーにもきちんと値が反映されている。

$ vagrant ssh mariadb2
mariadb2:$ mysql -u root
MariaDB> use test;
Database changed

MariaDB> select * from sample;
+----+-------+
| id | text  |
+----+-------+
|  1 | aaaaa |
+----+-------+
1 row in set (0.00 sec)

17
16
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
16