検証用にInnoDB Cluster(Group Replication + MySQL Shell + MySQL Router)の環境を作ってみましたので、その際の構築手順のメモ書きです。
InnoDB Cluster
MySQL InnoDB Clusterは、MySQLのための高可用性な環境を構築するためのコンポーネント群です。
具体的には以下の3つから構成されます。
- Group Replication
- MySQL Shell
- MySQL Router
構築手順
環境
OS | Ubuntu 16.04 LTS |
---|---|
MySQL | 5.7.18 (Oracle社オフィシャルパッケージ) |
MySQL Shell | 1.0.9 (Oracle社オフィシャルパッケージ) |
MySQL Router | 2.1.3 (Oracle社オフィシャルパッケージ) |
構成
あらかじめ、こちらの記事の通りに、1つのホストOS内に、複数のMySQLプロセスを異なるポート/データディレクトリで起動して、それらでGroup Replicationを構成し、さらにMySQL ShellとMySQL Routerをインストールします。
MySQL | MySQLポート | Group Replication用ポート | データディレクトリ |
---|---|---|---|
MySQL#01 | 24801 | 24901 | /var/lib/mysql-01/ |
MySQL#02 | 24802 | 24902 | /var/lib/mysql-02/ |
MySQL#03 | 24803 | 24903 | /var/lib/mysql-03/ |

MySQL Server / MySQL Client / MySQL Shell / MySQL Router インストール
MySQL公式ソフトウェアリポジトリからMySQL Community Server / Client / MySQL Shell/ MySQL Router を取得してインストールします。
以下から、MySQL公式リポジトリ設定用パッケージを取得してそこからインストールするか、直接ダウンロードしてインストールします。
- MySQL :: Download MySQL APT Repository
- MySQL :: Download MySQL Community Server
- MySQL :: Download MySQL Shell
- MySQL :: Download MySQL Router
MySQL公式APTリポジトリ設定用のパッケージを取得してインストールするには以下のコマンドを実行します。
# curl -OL https://dev.mysql.com/get/mysql-apt-config_0.8.6-1_all.deb
MySQL公式APTリポジトリ設定用のパッケージをインストールします。
# dpkg -i mysql-apt-config*
以下のような設定画面が出ますが、デフォルトのままOKを選択します。(後での変更も可能)
パッケージの設定 lqqqqqqqqqqqqqqqqqqqu mysql-apt-config を設定しています tqqqqqqqqqqqqqqqqqqqk x MySQL APT Repo features MySQL Server along with a variety of MySQL x x components. You may select the appropriate product to choose the version x x that you wish to receive. x x x x Once you are satisfied with the configuration then select last option x x 'Apply' to save the configuration. Advanced users can always change the x x configurations later, depending on their own needs. x x x x Which MySQL product do you wish to configure? x x x x MySQL Server (Currently selected: mysql-5.7) x x MySQL Tools & Connectors (Currently selected: Enabled) x x MySQL Preview Packages (Currently selected: Disabled) x x Ok x x x x x x <Ok> x x x mqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj
パッケージリストを更新します。
# apt update
MySQL、MySQL Shell、MySQL Routerのパッケージをインストールします。
# apt install mysql-server mysql-community-server mysql-shell mysql-router
rootパスワードの入力を求められるので入力して設定します。
パッケージの設定 lqqqqqqqqqqqqqqqu mysql-community-server を設定しています tqqqqqqqqqqqqqqqqk x Please provide a strong password that will be set for the root account x x of your MySQL database. Leave it blank to enable password less login x x using UNIX socket based authentication. x x x x Enter root password: x x x x ________________________________________________________________________ x x x x <了解> x x x mqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj
再度rootパスワードの入力を求められるので入力して設定します。
パッケージの設定 lqqqqqqqqqqqqqu mysql-community-server を設定しています tqqqqqqqqqqqqqqk x Now that you have selected a password for the root account, please x x confirm by typing it again. Do not share the password with anyone. x x x x Re-enter root password: x x x x ____________________________________________________________________ x x x x <了解> x x x mqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj
以下のパッケージがインストールされていることを確認します。
# dpkg -l | grep mysql ii mysql-apt-config 0.8.6-1 all Auto configuration for MySQL APT Repo. ii mysql-client 5.7.18-1ubuntu16.04 amd64 MySQL Client meta package depending on latest version ii mysql-common 5.7.18-1ubuntu16.04 amd64 MySQL Common ii mysql-community-client 5.7.18-1ubuntu16.04 amd64 MySQL Client ii mysql-community-server 5.7.18-1ubuntu16.04 amd64 MySQL Server ii mysql-server 5.7.18-1ubuntu16.04 amd64 MySQL Server meta package depending on latest version
MySQL Group Replicationを使うためのMySQLインスタンス準備
MySQLをインストールしたら、以下のような3つの設定ファイルを準備します。
loose-group_replication_group_name には各サーバで一意な値 uuid を設定します。uuidgenコマンド等で作成したものを記載します。
Single-Primaryモード(1台のサーバのみ書き込み可能で、その他は読み取り専用)で稼働させるため、 loose-group_replication_single_primary_mode = OFF
, loose-group_replication_enforce_update_everywhere_checks = ON
の設定を削除 or コメントアウトします。
MySQLの設定ファイル作成
MySQL#01の設定ファイル(/etc/mysql/mysqld-01.cnf
)を作成します。
[mysqld_safe] socket = /tmp/mysqld-01.sock nice = 0 [mysqld] # # * Basic Settings # server_id = 100001 user = mysql pid-file = /tmp/mysqld-01.pid socket = /tmp/mysqld-01.sock port = 24801 basedir = /usr datadir = /var/lib/mysql-01/ tmpdir = /tmp skip-external-locking slow_query_log = 1 slow_query_log_file = /var/log/mysql/mysql-01-slow.log log-error = /var/log/mysql/mysql-01-error.log log_timestamps = SYSTEM explicit_defaults_for_timestamp = true # # * Binary-log setting # log_bin = /var/log/mysql/mysql-01-bin.log binlog_format = ROW # # * General replication settings # gtid_mode = ON enforce_gtid_consistency = ON master_info_repository = TABLE relay_log_info_repository = TABLE binlog_checksum = NONE log_slave_updates = ON transaction_write_set_extraction = XXHASH64 loose-group_replication_bootstrap_group = OFF loose-group_replication_start_on_boot = OFF loose-group_replication_ssl_mode = DISABLED loose-group_replication_recovery_use_ssl = 0 # # * Replication group settings # loose-group_replication_group_name = "********-****-****-****-************" loose-group_replication_ip_whitelist = "127.0.0.1" loose-group_replication_group_seeds= "127.0.0.1:24901,127.0.0.1:24902,127.0.0.1:24903" loose-group_replication_local_address= "127.0.0.1:24901" # # * Multi-primary mode settings # #loose-group_replication_single_primary_mode = OFF #loose-group_replication_enforce_update_everywhere_checks = ON # # * Host specific replication configuration # report_host = "127.0.0.1" report_port = 24801
MySQL#02の設定ファイル(/etc/mysql/mysqld-02.cnf
)を作成します。
[mysqld_safe] socket = /tmp/mysqld-02.sock nice = 0 [mysqld] # # * Basic Settings # server_id = 100002 user = mysql pid-file = /tmp/mysqld-02.pid socket = /tmp/mysqld-02.sock port = 24802 basedir = /usr datadir = /var/lib/mysql-02/ tmpdir = /tmp skip-external-locking slow_query_log = 1 slow_query_log_file = /var/log/mysql/mysql-02-slow.log log-error = /var/log/mysql/mysql-02-error.log log_timestamps = SYSTEM explicit_defaults_for_timestamp = true # # * Binary-log setting # log_bin = /var/log/mysql/mysql-02-bin.log binlog_format = ROW # # * General replication settings # gtid_mode = ON enforce_gtid_consistency = ON master_info_repository = TABLE relay_log_info_repository = TABLE binlog_checksum = NONE log_slave_updates = ON transaction_write_set_extraction = XXHASH64 loose-group_replication_bootstrap_group = OFF loose-group_replication_start_on_boot = OFF loose-group_replication_ssl_mode = DISABLED loose-group_replication_recovery_use_ssl = 0 # # * Replication group settings # loose-group_replication_group_name = "********-****-****-****-************" loose-group_replication_ip_whitelist = "127.0.0.1" loose-group_replication_group_seeds= "127.0.0.1:24901,127.0.0.1:24902,127.0.0.1:24903" loose-group_replication_local_address= "127.0.0.1:24902" # # * Multi-primary mode settings # #loose-group_replication_single_primary_mode = OFF #loose-group_replication_enforce_update_everywhere_checks = ON # # * Host specific replication configuration # report_host = "127.0.0.1" report_port = 24801
MySQL#03の設定ファイル(/etc/mysql/mysqld-03.cnf
)を作成します。
[mysqld_safe] socket = /tmp/mysqld-03.sock nice = 0 [mysqld] # # * Basic Settings # server_id = 100003 user = mysql pid-file = /tmp/mysqld-03.pid socket = /tmp/mysqld-03.sock port = 24803 basedir = /usr datadir = /var/lib/mysql-01/ tmpdir = /tmp skip-external-locking slow_query_log = 1 slow_query_log_file = /var/log/mysql/mysql-03-slow.log log-error = /var/log/mysql/mysql-03-error.log log_timestamps = SYSTEM explicit_defaults_for_timestamp = true # # * Binary-log setting # log_bin = /var/log/mysql/mysql-03-bin.log binlog_format = ROW # # * General replication settings # gtid_mode = ON enforce_gtid_consistency = ON master_info_repository = TABLE relay_log_info_repository = TABLE binlog_checksum = NONE log_slave_updates = ON transaction_write_set_extraction = XXHASH64 loose-group_replication_bootstrap_group = OFF loose-group_replication_start_on_boot = OFF loose-group_replication_ssl_mode = DISABLED loose-group_replication_recovery_use_ssl = 0 # # * Replication group settings # loose-group_replication_group_name = "********-****-****-****-************" loose-group_replication_ip_whitelist = "127.0.0.1" loose-group_replication_group_seeds= "127.0.0.1:24901,127.0.0.1:24902,127.0.0.1:24903" loose-group_replication_local_address= "127.0.0.1:24903" # # * Multi-primary mode settings # #loose-group_replication_single_primary_mode = OFF #loose-group_replication_enforce_update_everywhere_checks = ON # # * Host specific replication configuration # report_host = "127.0.0.1" report_port = 24801
MySQLデータディレクトリ初期化
MySQLデータディレクトリを初期化します。
単一ホスト内に異なるMySQLを起動する必要があるため、データディレクトリを分けます。
ここでは /var/lib/mysql-{01,02,03}
としていますが、UbuntuではAppArmorによって/var/lib/mysql
以外をデータディレクトリとて初期化しようとするとエラーになります。
まずは /etc/apparmor.d/usr.sbin.mysqld
へ /var/lib/mysql-{01,02,03}
をデータディレクトリとして扱えるように許可設定を行います。
# diff -u /etc/apparmor.d/usr.sbin.mysqld{.org,} --- /etc/apparmor.d/usr.sbin.mysqld.org YYYY-MM-DD hh:mm:ss.000000000 +0900 +++ /etc/apparmor.d/usr.sbin.mysqld YYYY-MM-DD hh:mm:ss.********* +0900 @@ -48,6 +48,10 @@ # Allow data dir access /var/lib/mysql/ r, /var/lib/mysql/** rwk, + /var/lib/mysql-01/ r, + /var/lib/mysql-01/** rwk, + /var/lib/mysql-02/ r, + /var/lib/mysql-02/** rwk, + /var/lib/mysql-03/ r, + /var/lib/mysql-03/** rwk,
設定を反映します。
# apparmor_parser -r /etc/apparmor.d/usr.sbin.mysqld
MySQL#01のデータディレクトリを初期化します。
# install -d -o mysql -g mysql -m 700 /var/lib/mysql-01 # mysqld --defaults-file=/etc/mysql/mysqld-01.cnf --initialize --user=mysql --explicit_defaults_for_timestamp (ログファイルにテンポラリパスワードが記録されているのでメモ)
MySQL#02のデータディレクトリを初期化します。
# install -d -o mysql -g mysql -m 700 /var/lib/mysql-02 # mysqld --defaults-file=/etc/mysql/mysqld-02.cnf --initialize --user=mysql --explicit_defaults_for_timestamp (ログファイルにテンポラリパスワードが記録されているのでメモ)
MySQL#03のデータディレクトリを初期化します。
# install -d -o mysql -g mysql -m 700 /var/lib/mysql-03 # mysqld --defaults-file=/etc/mysql/mysqld-03.cnf --initialize --user=mysql --explicit_defaults_for_timestamp (ログファイルにテンポラリパスワードが記録されているのでメモ)
MySQL起動
MySQL#01を起動します。
# mysqld --defaults-file=/etc/mysql/mysqld-01.cnf --user=mysql &
MySQL#02を起動します。
# mysqld --defaults-file=/etc/mysql/mysqld-02.cnf --user=mysql &
MySQL#03を起動します。
# mysqld --defaults-file=/etc/mysql/mysqld-03.cnf --user=mysql &
MySQL rootパスワード設定
MySQL#01のrootパスワードを再設定します。
# mysql -u root -P 24801 --protocol=tcp -h localhost -p mysql#01> set password for root@localhost=password('********'); Query OK, 0 rows affected, 1 warning (0.01 sec)
MySQL#02のrootパスワードを再設定します。
# mysql -u root -P 24802 --protocol=tcp -h localhost -p mysql#02> set password for root@localhost=password('********'); Query OK, 0 rows affected, 1 warning (0.01 sec)
MySQL#03のrootパスワードを再設定します。
# mysql -u root -P 24803 --protocol=tcp -h localhost -p mysql#03> set password for root@localhost=password('********'); Query OK, 0 rows affected, 1 warning (0.01 sec)
MySQL レプリケーション用ユーザ作成
MySQL#01にレプリケーション用のユーザを作成します。
mysql#01> SET SQL_LOG_BIN=0; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql#01> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%' IDENTIFIED BY '********'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql#01> SET SQL_LOG_BIN=1; Query OK, 0 rows affected, 1 warning (0.00 sec)
MySQL#02にレプリケーション用のユーザを作成します。
mysql#02> SET SQL_LOG_BIN=0; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql#02> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%' IDENTIFIED BY '********'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql#02> SET SQL_LOG_BIN=1; Query OK, 0 rows affected, 1 warning (0.00 sec)
MySQL#03にレプリケーション用のユーザを作成します。
mysql#03> SET SQL_LOG_BIN=0; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql#03> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%' IDENTIFIED BY '********'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql#03> SET SQL_LOG_BIN=1; Query OK, 0 rows affected, 1 warning (0.00 sec)
レプリケーション設定
MySQL#01にレプリケーション接続用の設定を行います。
mysql#01> RESET MASTER; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql#01> CHANGE MASTER TO MASTER_USER='repl', MASTER_PASSWORD='********' FOR CHANNEL 'group_replication_recovery'; Query OK, 0 rows affected, 1 warning (0.00 sec)
MySQL#02にレプリケーション接続用の設定を行います。
mysql#02> RESET MASTER; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql#02> CHANGE MASTER TO MASTER_USER='repl', MASTER_PASSWORD='********' FOR CHANNEL 'group_replication_recovery'; Query OK, 0 rows affected, 1 warning (0.00 sec)
MySQL#03にレプリケーション接続用の設定を行います。
mysql#03> RESET MASTER; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql#03> CHANGE MASTER TO MASTER_USER='repl', MASTER_PASSWORD='********' FOR CHANNEL 'group_replication_recovery'; Query OK, 0 rows affected, 1 warning (0.00 sec)
MySQL Group Replicationプラグイン有効化
MySQL#01のGroup Replicationプラグインを有効化します。
mysql#01> select * from information_schema.plugins where plugin_name='group_replication'\G Empty set (0.00 sec) mysql#01> INSTALL PLUGIN group_replication SONAME 'group_replication.so'; Query OK, 0 rows affected (0.00 sec) mysql#01> select * from information_schema.plugins where plugin_name='group_replication'\G *************************** 1. row *************************** PLUGIN_NAME: group_replication PLUGIN_VERSION: 1.0 PLUGIN_STATUS: ACTIVE PLUGIN_TYPE: GROUP REPLICATION PLUGIN_TYPE_VERSION: 1.1 PLUGIN_LIBRARY: group_replication.so PLUGIN_LIBRARY_VERSION: 1.7 PLUGIN_AUTHOR: ORACLE PLUGIN_DESCRIPTION: Group Replication (1.0.0) PLUGIN_LICENSE: GPL LOAD_OPTION: ON 1 row in set (0.00 sec)
MySQL#02のGroup Replicationプラグインを有効化します。
mysql#02> select * from information_schema.plugins where plugin_name='group_replication'\G Empty set (0.00 sec) mysql#02> INSTALL PLUGIN group_replication SONAME 'group_replication.so'; Query OK, 0 rows affected (0.00 sec) mysql#02> select * from information_schema.plugins where plugin_name='group_replication'\G *************************** 1. row *************************** PLUGIN_NAME: group_replication PLUGIN_VERSION: 1.0 PLUGIN_STATUS: ACTIVE PLUGIN_TYPE: GROUP REPLICATION PLUGIN_TYPE_VERSION: 1.1 PLUGIN_LIBRARY: group_replication.so PLUGIN_LIBRARY_VERSION: 1.7 PLUGIN_AUTHOR: ORACLE PLUGIN_DESCRIPTION: Group Replication (1.0.0) PLUGIN_LICENSE: GPL LOAD_OPTION: ON 1 row in set (0.00 sec)
MySQL#03のGroup Replicationプラグインを有効化します。
mysql#03> select * from information_schema.plugins where plugin_name='group_replication'\G Empty set (0.00 sec) mysql#03> INSTALL PLUGIN group_replication SONAME 'group_replication.so'; Query OK, 0 rows affected (0.00 sec) mysql#03> select * from information_schema.plugins where plugin_name='group_replication'\G *************************** 1. row *************************** PLUGIN_NAME: group_replication PLUGIN_VERSION: 1.0 PLUGIN_STATUS: ACTIVE PLUGIN_TYPE: GROUP REPLICATION PLUGIN_TYPE_VERSION: 1.1 PLUGIN_LIBRARY: group_replication.so PLUGIN_LIBRARY_VERSION: 1.7 PLUGIN_AUTHOR: ORACLE PLUGIN_DESCRIPTION: Group Replication (1.0.0) PLUGIN_LICENSE: GPL LOAD_OPTION: ON 1 row in set (0.00 sec)
InnoDB Cluster管理用ユーザ作成
MySQL InnoDB Cluster管理用のユーザを作成します。
mysql#01> SET SQL_LOG_BIN=0; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql#01> GRANT SUPER, FILE, GRANT OPTION, RELOAD, SHUTDOWN, PROCESS,REPLICATION SLAVE, REPLICATION CLIENT, CREATE USER ON *.* TO icroot@'%' IDENTIFIED BY '********' WITH GRANT OPTION; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql#01> GRANT SELECT, INSERT, UPDATE, DELETE ON `mysql`.* TO 'icroot'@'%' WITH GRANT OPTION; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql#01> GRANT SELECT ON `performance_schema`.* TO 'icroot'@'%' WITH GRANT OPTION; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql#01> GRANT SELECT ON *.* TO 'icroot'@'%' WITH GRANT OPTION; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql#01> GRANT ALL PRIVILEGES ON `mysql_innodb_cluster_metadata`.* TO 'icroot'@'%' WITH GRANT OPTION; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql#01> SET SQL_LOG_BIN=1; Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql#02> SET SQL_LOG_BIN=0; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql#02> GRANT SUPER, FILE, GRANT OPTION, RELOAD, SHUTDOWN, PROCESS,REPLICATION SLAVE, REPLICATION CLIENT, CREATE USER ON *.* TO icroot@'%' IDENTIFIED BY '********'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql#02> GRANT SELECT, INSERT, UPDATE, DELETE ON `mysql`.* TO 'icroot'@'%'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql#02> GRANT SELECT ON `performance_schema`.* TO 'icroot'@'%'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql#02> GRANT SELECT ON *.* TO 'icroot'@'%' WITH GRANT OPTION; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql#02> GRANT ALL PRIVILEGES ON `mysql_innodb_cluster_metadata`.* TO 'icroot'@'%'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql#02> SET SQL_LOG_BIN=1; Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql#03> SET SQL_LOG_BIN=0; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql#03> GRANT SUPER, FILE, GRANT OPTION, RELOAD, SHUTDOWN, PROCESS,REPLICATION SLAVE, REPLICATION CLIENT, CREATE USER ON *.* TO icroot@'%' IDENTIFIED BY '********'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql#03> GRANT SELECT, INSERT, UPDATE, DELETE ON `mysql`.* TO 'icroot'@'%'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql#03> GRANT SELECT ON `performance_schema`.* TO 'icroot'@'%'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql#03> GRANT SELECT ON *.* TO 'icroot'@'%' WITH GRANT OPTION; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql#03> GRANT ALL PRIVILEGES ON `mysql_innodb_cluster_metadata`.* TO 'icroot'@'%'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql#03> SET SQL_LOG_BIN=1; Query OK, 0 rows affected, 1 warning (0.00 sec)
MySQL ShellでのGroup Replication設定
まず、MySQL Shellを使ってGroup Replicationが稼働できる状態にしておくため、こちらの記事にある/etc/mysql/mysqld-01.cnf
, /etc/mysql/mysqld-02.cnf
, /etc/mysql/mysqld-03.cnf
を準備して、3つのMySQLインスタンスを起動します。
Group Replicationが動いている場合はstop group_replication
しておきます。
MySQL Shellを起動するためにmysqlsh
コマンドを実行します。
※root権限化で実行します。
# mysqlsh Welcome to MySQL Shell 1.0.9 Copyright (c) 2016, 2017, 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', '\h' or '\?' for help, type '\quit' or '\q' to exit. Currently in JavaScript mode. Use \sql to switch to SQL mode and execute queries. mysql-js>
Group Replication用のインスタンスを設定します。
設定には MySQL Shell の AdminAPI というInnoDBクラスタを設定および管理するためのAPIを使います。
MySQL Shell上の グローバル変数 ‘dba’ を使ってAdminAPI機能にアクセスしてInnoDBクラスタを操作・管理します。
configureLocalInstanceというクラスを使います。これはインスタンスの構成をレビューして、グループ複製およびクラスタでの使用に有効かどうかを識別します。 操作の結果を含むJSONオブジェクトが返されます。
mysql-js> dba.configureLocalInstance('root@localhost:24801') Please provide the password for 'root@localhost:24801': Detecting the configuration file... Found configuration file at standard location: /etc/mysql/mysql.conf.d/mysqld.cnf Do you want to modify this file? [Y|n]: n # <-デフォルトのコンフィグファイルを使わないので n を入力します。 Default file not found at the standard locations. Please specify the path to the MySQL configuration file: /etc/mysql/mysqld-01.cnf # <-MySQL#01の設定ファイルを指定 MySQL user 'root' cannot be verified to have access to other hosts in the network. 1) Create root@% with necessary grants 2) Create account with different name 3) Continue without creating account 4) Cancel Please select an option [1]: 3 # <- 上記でユーザを作成していない場合はここで 2 を選択して作成 Validating instance... The instance 'localhost:24801' is valid for Cluster usage You can now use it in an InnoDB Cluster. { "status": "ok" }
mysql-js> dba.configureLocalInstance('root@localhost:24802') Please provide the password for 'root@localhost:24802': Detecting the configuration file... Found configuration file at standard location: /etc/mysql/mysql.conf.d/mysqld.cnf Do you want to modify this file? [Y|n]: n # <-デフォルトのコンフィグファイルを使わないので n を入力します。 Default file not found at the standard locations. Please specify the path to the MySQL configuration file: /etc/mysql/mysqld-02.cnf # <-MySQL#01の設定ファイルを指定 MySQL user 'root' cannot be verified to have access to other hosts in the network. 1) Create root@% with necessary grants 2) Create account with different name 3) Continue without creating account 4) Cancel Please select an option [1]: 3 # <- 上記でユーザを作成していない場合はここで 2 を選択して作成 Validating instance... The instance 'localhost:24802' is valid for Cluster usage You can now use it in an InnoDB Cluster. { "status": "ok" }
mysql-js> dba.configureLocalInstance('root@localhost:24803') Please provide the password for 'root@localhost:24803': Detecting the configuration file... Found configuration file at standard location: /etc/mysql/mysql.conf.d/mysqld.cnf Do you want to modify this file? [Y|n]: n # <-デフォルトのコンフィグファイルを使わないので n を入力します。 Default file not found at the standard locations. Please specify the path to the MySQL configuration file: /etc/mysql/mysqld-03.cnf # <-MySQL#01の設定ファイルを指定 MySQL user 'root' cannot be verified to have access to other hosts in the network. 1) Create root@% with necessary grants 2) Create account with different name 3) Continue without creating account 4) Cancel Please select an option [1]: 3 # <- 上記でユーザを作成していない場合はここで 2 を選択して作成 Validating instance... The instance 'localhost:24803' is valid for Cluster usage You can now use it in an InnoDB Cluster. { "status": "ok" }
3つのMySQLインスタンスへの接続情報を変数 i1, i2, i3 へ格納します。
mysql-js> var i1 = 'icroot@192.168.10.1:24801' mysql-js> var i2 = 'icroot@192.168.10.1:24802' mysql-js> var i3 = 'icroot@192.168.10.1:24803'
shell関数のconnectクラスを使って、いずれかのMySQLインスタンスとのコネクションを確立します。
mysql-js> shell.connect(i1) Please provide the password for 'icroot@192.168.10.1:24801': Creating a Session to 'icroot@192.168.10.1:24801' Classic Session successfully established. No default schema selected.
dba関数のcreateClusterクラスを使って、新規にInnoDBクラスターを作成します。
引数にはクラスタの名称を入力します。ここではClusterDevとしています。
mysql-js> var cluster = dba.createCluster('ClusterDev'); A new InnoDB cluster will be created on instance 'icroot@192.168.10.1:24801'. Creating InnoDB cluster 'ClusterDev' on 'icroot@192.168.10.1:24801'... Adding Seed Instance... Cluster successfully created. Use Cluster.addInstance() to add MySQL instances. At least 3 instances are needed for the cluster to be able to withstand up to one server failure.
cluster変数にaddInstanceクラスを使って、残りのMySQLインスタンスをクラスタに追加します。
mysql-js> cluster.addInstance(i2); A new instance will be added to the InnoDB cluster. Depending on the amount of data on the cluster this might take from a few seconds to several hours. Please provide the password for 'icroot@192.168.10.1:24802': Adding instance to the cluster ... The instance 'icroot@192.168.10.1:24802' was successfully added to the cluster.
mysql-js> cluster.addInstance(i3); A new instance will be added to the InnoDB cluster. Depending on the amount of data on the cluster this might take from a few seconds to several hours. Please provide the password for 'icroot@192.168.10.1:24803': Adding instance to the cluster ... The instance 'icroot@192.168.10.1:24803' was successfully added to the cluster.
クラスタのステータスを確認します。
mysql-js> cluster.status(); { "clusterName": "ClusterDev", "defaultReplicaSet": { "name": "default", "primary": "192.168.10.1:24801", "status": "OK", "statusText": "Cluster is ONLINE and can tolerate up to ONE failure.", "topology": { "192.168.10.1:24801": { "address": "192.168.10.1:24801", "mode": "R/W", "readReplicas": {}, "role": "HA", "status": "ONLINE" }, "192.168.10.1:24802": { "address": "192.168.10.1:24802", "mode": "R/O", "readReplicas": {}, "role": "HA", "status": "ONLINE" }, "192.168.10.1:24803": { "address": "192.168.10.1:24803", "mode": "R/O", "readReplicas": {}, "role": "HA", "status": "ONLINE" } } } }
MySQL Router の設定
MySQL Routerの設定ファイルを生成します。
# mysqlrouter --bootstrap icroot@192.168.10.1:24801 --user mysqlrouter Please enter MySQL password for icroot: # <-パスワードを入力 Bootstrapping system MySQL Router instance... MySQL Router has now been configured for the InnoDB cluster 'ClusterDev'. The following connection information can be used to connect to the cluster. Classic MySQL protocol connections to cluster 'ClusterDev': - Read/Write Connections: localhost:6446 - Read/Only Connections: localhost:6447 X protocol connections to cluster 'ClusterDev': - Read/Write Connections: localhost:64460 - Read/Only Connections: localhost:64470
MySQL Routerを起動します。
# sudo -u mysqlrouter /usr/bin/mysqlrouter -c /etc/mysqlrouter/mysqlrouter.conf &
確認
マスターにはMySQL Routerの6446ポートを使って接続可能になっており、セカンダリにはMySQL Routerの6447ポートを使って接続可能になっています。
セカンダリは2台あるので、いずれかにロードバランスされます。
マスターへの接続(MySQL#01:24801)
# mysql -u icroot --protocol=tcp -P 6446 -h 192.168.10.1 -p -e "select @@port;" Enter password: +--------+ | @@port | +--------+ | 24801 | +--------+
セカンダリへの接続(MySQL#02:24802 or MySQL#03:24803へのバランシング)
# mysql -u icroot --protocol=tcp -P 6447 -h 192.168.10.1 -p -e "select @@port;" Enter password: +--------+ | @@port | +--------+ | 24802 | +--------+
# mysql -u icroot --protocol=tcp -P 6447 -h 192.168.10.1 -p -e "select @@port;" Enter password: +--------+ | @@port | +--------+ | 24803 | +--------+
まとめ
これで MySQL InnoDB Cluster環境を構築できました。
次は動作検証について書こうと思います。
参考
- MySQL :: MySQL 5.7 Reference Manual :: 20 InnoDB Cluster
- MySQL :: MySQL 5.7 Reference Manual :: 3.8 MySQL Shell User Guide
- MySQL :: MySQL Router
- MySQL InnoDB Cluster GA is Available Now! | MySQL Server Blog
- MySQL Bugs: #85567: cluster.rejoinInstance() doesn’t use the user in the connection string parameter
- MySQL Bugs: #86050: mysqlrouter does not start with default mysqlrouter –bootstrap
- MySQL InnoDB Cluster – A Hands on Tutorial | MySQL Server Blog