41から始めました

文字通り41歳から始めたブログです。DB(MySQL)を使ってお仕事してるので、DB周りの話を中心に最近始めたこととかをTwitterのノリで書いています。なお、本サイトにおいて示されている見解は私個人の見解であり、所属団体や組織を代表するものではありません。

EC2上でPercona XtraDB Cluster構築(Galera Arbitratorは後日入れたい)

基本的には公式の手順通り

作業概要

Percona XtraDB Cluster構築を行う。 構成は以下の通り

  • EC2×4台
  • Percona-XtraDB-Cluster-57-5.7.24×3
  • ProxySQL×1(後日)
  • Galera Arbitrator(さらに後日)→ProxySQLと同じサーバとする

事前作業

サーバ IPアドレス
perconadb1 xxx.xxx.1.81
perconadb2 xxx.xxx.0.103
perconadb3 xxx.xxx.2.211
proxysql1 xxx.xxx.1.120

ってな感じで作ってる

  • セキュリティグループで TCP/3306,4444,4567,4568 を開放
  • インバウンドのルールの編集で上記ポートを開放したルールを追加しておく

  • SELinuxが無効であること(disabled)を確認

構築手順(DB)

以下の作業は各DBサーバに対して行うこと。

1.Perconaリポジトリの設定

sudo yum install https://repo.percona.com/yum/percona-release-latest.noarch.rpm

2.Percona XtraDBクラスタパッケージインストール

sudo yum install Percona-XtraDB-Cluster-57

3.root パスワードの変更

rootの初期パスワードが自動で割り当てられているので、変更する

# Percona(MySQL) server起動
sudo service mysql start
# インストール時に割当てられたパスワードを確認
sudo grep 'temporary password' /var/log/mysqld.log
# そのパスワードを使い、rootでログイン
mysql -u root -p

rootパスワードを変更する

ALTER USER 'root'@'localhost' IDENTIFIED BY '<任意のパスワード>';
-- ついでにリモート接続できるrootユーザも作っとく
create user 'root'@'%' identified by '<任意のパスワード>!';
grant all on *.* to 'root'@'%' with grant option;
exit;

この後、my.cnfを変更するので停止する

# Percona(MySQL) Serverの停止
sudo service mysql stop

データディレクトリの作成

sudo mkdir -p /mnt/data
sudo mysql_install_db --datadir=/mnt/data --user=mysql

/etc/my.cnfファイルの作成

一つ目のノードには以下を記入

[mysqld]
datadir=/mnt/data
user=mysql

binlog_format=ROW

wsrep_provider=/usr/lib64/libgalera_smm.so
wsrep_cluster_address=gcomm://<ノード1のip address>,<ノード2のip address>,<ノード3のip address>

wsrep_slave_threads=2
wsrep_cluster_name=trimethylxanthine
wsrep_sst_method=rsync
wsrep_node_name=node1 # ノードごとにここを書き換える

innodb_autoinc_lock_mode=2

2つ目と3つ目のノードはwsrep_node_name の値を変えるだけ

sudo vim /etc/my.cnf

1つ目のノードでPercona XtraDB Cluster起動

sudo systemctl start mysql@bootstrap
(sudo service mysql  start bootstrap-pxcでもたぶん問題ない)

2つ目のノードでもPercona XtraDB Cluster起動

sudo service mysql start

3つ目のノードでもPercona XtraDB Cluster起動

sudo service mysql start

/var/log/mysqld.logに

[Note] WSREP: Synchronized with group, ready for connections

と出力されていれば成功です。 もし疎通のところでエラーが出る人はセキュリティグループを見直したほうがいいです。

接続確認して適当にデータベースでも作る

mysql -u root -p

ところが・・・

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

アレー?なじぇ?? Cluster構成するまでは入れてたのに・・・。 今回は仕方ないのでリモートから入って、ローカルのパスワードを書き換えるという荒業に。

mysql -u root -p -h xxx.xxx.xxx.xxx -Pxxxx
-- 別のノードから入る
UPDATE mysql.user SET authentication_string=password('<任意のパスワード>') WHERE user='root';
 flush privileges;

しかも、事前にリモートroot作ってなかったから、実際はmy.cnfにskip-grant-tables入れて再起動するという、せっかくクラスタ組んだのに大丈夫か?と心配になるような作業をしてる。(でも大丈夫だった)

構築手順(ProxySQL)

1.Perconaリポジトリの設定(DBと同じ)

sudo yum install https://repo.percona.com/yum/percona-release-latest.noarch.rpm

2.ProxySQLのインストール

sudo yum install Percona-XtraDB-Cluster-57

3.MySQL Clientのインストール

下記はPerconaのクライアントだが、別にMySQL Clientでも問題ない

sudo yum install -y Percona-Server-client-57

4.ProxySQLの設定

Percona XtraDBクラスタノードを設定するため、「proxysql-admin」というツールがインストールされているのでそれを使う。

設定ファイル修正

sudo vim /etc/proxysql-admin.cnf

デフォルトでは以下のようになっている

# PXC admin credentials for connecting to pxc-cluster-node.
export CLUSTER_USERNAME='admin'
export CLUSTER_PASSWORD='admin'
export CLUSTER_HOSTNAME='localhost'
export CLUSTER_PORT='3306'

ので、適当なノードを選んでそこにつなぐようにする。

# PXC admin credentials for connecting to pxc-cluster-node.
export CLUSTER_USERNAME='root'
export CLUSTER_PASSWORD='xxxxxxxx'
export CLUSTER_HOSTNAME='xxx.xxx.xxx.xxx'
export CLUSTER_PORT='3306'

設定

sudo proxysql-admin --config-file=/etc/proxysql-admin.cnf --enable

以下のようなメッセージが表示されれば成功

[ec2-user@proxysql1 ~]$sudo proxysql-admin --config-file=/etc/proxysql-admin.cnf --enable

This script will assist with configuring ProxySQL for use with
Percona XtraDB Cluster (currently only PXC in combination
with ProxySQL is supported)

ProxySQL read/write configuration mode is singlewrite

Configuring the ProxySQL monitoring user.
ProxySQL monitor user name as per command line/config-file is monitor

User 'monitor'@'10.%' has been added with USAGE privileges

Configuring the Percona XtraDB Cluster application user to connect through ProxySQL
Percona XtraDB Cluster application user name as per command line/config-file is proxysql_user

Percona XtraDB Cluster application user 'proxysql_user'@'10.%' has been added with ALL privileges, this user is created for testing purposes

Adding the Percona XtraDB Cluster server nodes to ProxySQL

Write node info
+-----------+--------------+------+---------+---------+
| hostname  | hostgroup_id | port | weight  | comment |
+-----------+--------------+------+---------+---------+
| xxx.xxx.1.81 | 10           | 3306 | 1000000 | WRITE   |
+-----------+--------------+------+---------+---------+

ProxySQL configuration completed!

ProxySQL has been successfully configured to use with Percona XtraDB Cluster

You can use the following login credentials to connect your application through ProxySQL

mysql --user=proxysql_user -p --host=localhost --port=6033 --protocol=tcp

※※※もしも設定を削除したい場合※※※

--disableオプションをつけて同じように実行すればいい

$ proxysql-admin --config-file=/etc/proxysql-admin.cnf --disable
ProxySQL configuration removed!

設定確認

ProxySQLのローカルDBにつなぐ(SQLite

proxysql_user (ポート=6033)

パスワードは/etc/proxysql-admin.cnfのCLUSTER_APP_PASSWORDを見ること

mysql --user=proxysql_user -p --host=localhost --port=6033 --protocol=tcp
exit;
admin (ポート=6032)

パスワードは/etc/proxysql-admin.cnfのPROXYSQL_PASSWORDを見ること

mysql -u admin -p -h 127.0.0.1 -P 6032
mysql> show databases;
+-----+---------------+-------------------------------------+
| seq | name          | file                                |
+-----+---------------+-------------------------------------+
| 0   | main          |                                     |
| 2   | disk          | /var/lib/proxysql/proxysql.db       |
| 3   | stats         |                                     |
| 4   | monitor       |                                     |
| 5   | stats_history | /var/lib/proxysql/proxysql_stats.db |
+-----+---------------+-------------------------------------+
5 rows in set (0.00 sec)

mysql> SHOW TABLES;
+--------------------------------------------+
| tables                                     |
+--------------------------------------------+
| global_variables                           |
| mysql_collations                           |
| mysql_group_replication_hostgroups         |
| mysql_query_rules                          |
| mysql_query_rules_fast_routing             |
| mysql_replication_hostgroups               |
| mysql_servers                              |
| mysql_users                                |
| proxysql_servers                           |
| runtime_checksums_values                   |
| runtime_global_variables                   |
| runtime_mysql_group_replication_hostgroups |
| runtime_mysql_query_rules                  |
| runtime_mysql_query_rules_fast_routing     |
| runtime_mysql_replication_hostgroups       |
| runtime_mysql_servers                      |
| runtime_mysql_users                        |
| runtime_proxysql_servers                   |
| runtime_scheduler                          |
| scheduler                                  |
+--------------------------------------------+
20 rows in set (0.00 sec)

ホストグループ(RW/RO)

mysql> SELECT hostgroup_id, hostname, status FROM mysql_servers;
+--------------+------------+--------------+
| hostgroup_id | hostname   | status       |
+--------------+------------+--------------+
| 11           | xxx.xxx.0.103 | ONLINE       |
| 10           | xxx.xxx.1.81  | ONLINE       |
| 11           | xxx.xxx.2.211 | ONLINE       |
| 11           | xxx.xxx.1.81  | OFFLINE_SOFT |
+--------------+------------+--------------+
4 rows in set (0.00 sec)

スケジュール

mysql> SELECT * FROM scheduler\G
*************************** 1. row ***************************
         id: 1
     active: 1
interval_ms: 3000
   filename: /bin/proxysql_galera_checker
       arg1: --config-file=/etc/proxysql-admin.cnf --writer-is-reader=ondemand --write-hg=10 --read-hg=11 --writer-count=1 --mode=singlewrite  --use-slave-as-writer=no  --log=/var/lib/proxysql/trimethylxanthine_proxysql_galera_check.log
       arg2: NULL
       arg3: NULL
       arg4: NULL
       arg5: NULL
    comment: trimethylxanthine
1 row in set (0.00 sec)

ユーザ一覧(ProxySQLサーバじゃなく、DBサーバで確認)

適当なCluster DBノードでDBにログインし、monitorとproxysql_userユーザが作られていることを確認

[ec2-user@perconadb1 ~]$mysql -uroot -pxxxxxxxx
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 19405
Server version: 5.7.24-26-57-log Percona XtraDB Cluster (GPL), Release rel26, Revision 65db531, WSREP version 31.33, wsrep_31.33

Copyright (c) 2009-2018 Percona LLC and/or its affiliates
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> select user from mysql.user;
+---------------+
| user          |
+---------------+
| root          |
| monitor       |
| proxysql_user |
| mysql.session |
| mysql.sys     |
| root          |
+---------------+
6 rows in set (0.00 sec)

mysql>

とりあえずできたっぽい?

  • proxysql-adminツール便利だわ。
  • 公式があっちこっち飛ばすのでそこは面倒。
  • 後日検証+Galera Arbitrator入れて遊ぶ。

ちょっと追記

MyISAMのデータはデフォルトでは作れない

engine がMyISAMのデータについては

Error Code: 1105. Percona-XtraDB-Cluster prohibits use of DML command on a table (hogehoge) that resides in non-transactional storage engine with pxc_strict_mode = ENFORCING or MASTER

みたいなこと言われる。 要するに、pxc_strict_mode ってパラメータがENFORCING or MASTERだとMyISAMでは上みたいなエラーメッセージ吐かれるらしい。

PXC Strict Mode からは PERMISSIVE(ワーニング扱いにする) または DISABLED(エラー無視して強制実行) にすればいい。

あと、MyISAMでのレプリケーションはデフォルトだと wsrep_replicate_myisam がOFFなんでONにしろってあるんだけど、Percona XtraDB Cluster Limitations 手元のPercona-XtraDB-ClusterではMyISAMだとデータが他のノードに行かなかったな…。 他に何かいるのかなー?

参考

https://www.percona.com/doc/percona-repo-config/percona-release.html https://www.percona.com/doc/percona-xtradb-cluster/LATEST/install/yum.html#yum https://www.percona.com/doc/percona-xtradb-cluster/LATEST/howtos/3nodesec2.html https://www.percona.com/doc/percona-xtradb-cluster/5.7/howtos/proxysql.html https://qiita.com/ksugawara61/items/d8d69b7d57a3afcef980