MySQLミドルウェアproxysql読み書き分離を実現
13501 ワード
環境の説明:
IP
ロール#ロール#
適用
プラットフォーム
192.168.161.96
読み書き分離解析ホスト
proxysql
rhel7.4
192.168.161.164
master
mysql
rhel7.4
192.168.161.167
slave
mysql
rhel7.4
前提条件:ファイアウォール を閉じる selinux を閉じる MySQLをインストールし、マスター から構成
192.168.161.96で読み書き分離解析を行うホストインストールProxySQL mysqlメインライブラリproxysql追加検索できるアカウント
ログインproxysql管理側mariadbインストール
データベースの説明: mainメモリ構成データベースは、バックエンドdbインスタンス、ユーザー検証、ルーティングルールなどの情報をテーブルに格納します.テーブル名はruntimeで始まるproxysqlが現在実行している構成内容を表し、dml文では修正できません.runtimeで始まる(メモリにある)テーブルのみを修正し、LOADを有効にします.SAVEはハードディスクに保存して次のロードを再起動するために diskはハードディスクに永続化する構成であり、sqliteデータファイル statsはproxysql実行キャプチャの統計情報であり、バックエンドへの各コマンドの実行回数、トラフィック、processlist、クエリー種別の要約/実行時間など を含む. monitorライブラリはmonitorモジュールが収集した情報を格納し、主にバックエンドdbの健康/遅延検査 である. stats_history統計履歴ライブラリ Proxysql管理側バックエンド接続mysqlプライマリ・スレーブ・データベースの構成を追加
runtime_ 最初は実行時の構成で、これらは変更できません.ProxySQLの構成を変更するには、runtime以外の設定を変更する必要があります.表、変更後にRUNTIMEにロードするにはLOAD...TO RUNTIMEを実行し、save...to diskを実行して構成をディスクに永続化する必要があります.
次の文ではmainライブラリに切り替えずに実行できました.ProxySQL内部で使用されているSQLite 3データベースエンジンは、MySQLの解析方法とは異なります.USE main文を実行しても効果はありませんが、エラーは発生しません
insert文を使用してmysqlホストをmysql_に追加serversテーブル:hostgroup_id 1は書き込みグループ,2は読み出しグループを表す
修正後、RUNTIMEにロードし、diskに保存する必要があります.
proxysqlホストのmysql_usersテーブルにmasterで作成したアカウントproxysqlを追加します.proxysqlクライアントはこのアカウントを使用してデータベースdefault_にアクセスする必要があります.hostgroupのデフォルトグループは書き込みグループ、すなわち1に設定されています.読み書き分離のルーティングルールが一致しない場合、デフォルトグループのデータベースにアクセスします.
健康診断のアカウントを追加mysqlのmaster端にproxysqlに属する読み取り専用アカウントを追加
proxysqlホスト側で変数を変更して健康診断のアカウントを設定します
リード/ライト分離のルーティング・ルール要件を追加するには、次の手順に従います. selectクエリ文をhostgroup_にすべてルーティングid=2のグループ(つまりリードグループ) ただしselect*from tb for updateのような文はデータを変更するので、hostgroup_にルーティングするために個別に定義する必要があります.id=1のグループ(つまり書き込みグループ) 他の規則的に一致しないグループは、ユーザのデフォルトのグループ(mysql_usersテーブルのdefault_hostgroup) にルーティングされる.
認証読み書き分離ログインproxysqlクライアントログインユーザー先mysql_userテーブルに作成されたユーザー、ポート6033
データベースとクエリーを変更して2つのデータベースを作成し、テーブルをクエリーしてみます.
読み書き分離に成功したかどうかを確認するproxysqlには監査のような機能があり、proxysql管理側で実行する必要があるSQLの実行状況を表示できます.
上のhostgroupとdigestからtext値を見ると、すべての書き込み操作は1グループにルーティングされ、読み取り操作は2グループにルーティングされ、そのうち1グループは書き込みグループ、2グループは読み取りグループである!
このことから,読み書き分離に成功した.
IP
ロール#ロール#
適用
プラットフォーム
192.168.161.96
読み書き分離解析ホスト
proxysql
rhel7.4
192.168.161.164
master
mysql
rhel7.4
192.168.161.167
slave
mysql
rhel7.4
前提条件:
192.168.161.96で読み書き分離解析を行うホストインストールProxySQL
// proxysql yum
[root@localhost ~]# cat < [proxysql_repo]
> name=ProxySQL
> baseurl=http://repo.proxysql.com/ProxySQL/proxysql-1.4.x/centos/7
> gpgcheck=1
> gpgkey=http://repo.proxysql.com/ProxySQL/repo_pub_key
> EOF
[proxysql_repo]
name=ProxySQL
baseurl=http://repo.proxysql.com/ProxySQL/proxysql-1.4.x/centos/7
gpgcheck=1
gpgkey=http://repo.proxysql.com/ProxySQL/repo_pub_key
// proxysql
[root@localhost ~]# yum -y install proxysql
// proxysql
//proxysql sysv , chkconfig
[root@localhost ~]# systemctl start proxysql
[root@localhost ~]# chkconfig proxysql on
mysql> grant all on *.* to 'proxysql'@'192.168.161.96' identified by 'pwproxysql'';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
ログインproxysql管理側mariadbインストール
[root@localhost ~]# yum -y install mariadb
[root@localhost ~]# export MYSQL_PS1="(\u@\h:\p) [\d]> "
[root@localhost ~]# mysql -uadmin -padmin -h127.0.0.1 -P6032
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.30 (ProxySQL Admin Module)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
([email protected]:6032) [(none)]>
([email protected]:6032) [(none)]> 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)
データベースの説明:
([email protected]:6032) [(none)]> show tables from main;
+--------------------------------------------+
| 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)
runtime_ 最初は実行時の構成で、これらは変更できません.ProxySQLの構成を変更するには、runtime以外の設定を変更する必要があります.表、変更後にRUNTIMEにロードするにはLOAD...TO RUNTIMEを実行し、save...to diskを実行して構成をディスクに永続化する必要があります.
次の文ではmainライブラリに切り替えずに実行できました.ProxySQL内部で使用されているSQLite 3データベースエンジンは、MySQLの解析方法とは異なります.USE main文を実行しても効果はありませんが、エラーは発生しません
insert文を使用してmysqlホストをmysql_に追加serversテーブル:hostgroup_id 1は書き込みグループ,2は読み出しグループを表す
([email protected]:6032) [(none)]> insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(1,'192.168.161.164',3306,1,'Write Group');
Query OK, 1 row affected (0.00 sec)
([email protected]:6032) [(none)]> insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(2,'192.168.161.167',3306,1,'Read Group');
Query OK, 1 row affected (0.00 sec)
([email protected]:6032) [(none)]> select * from mysql_servers;
+--------------+-----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+-------------+
| hostgroup_id | hostname | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+-----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+-------------+
| 1 | 192.168.161.164 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | Write Group |
| 2 | 192.168.161.167 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | Read Group |
+--------------+-----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+-------------+
2 rows in set (0.00 sec)
修正後、RUNTIMEにロードし、diskに保存する必要があります.
([email protected]:6032) [(none)]> load mysql servers to runtime;
Query OK, 0 rows affected (0.00 sec)
([email protected]:6032) [(none)]> save mysql servers to disk;
Query OK, 0 rows affected (0.05 sec)
proxysqlホストのmysql_usersテーブルにmasterで作成したアカウントproxysqlを追加します.proxysqlクライアントはこのアカウントを使用してデータベースdefault_にアクセスする必要があります.hostgroupのデフォルトグループは書き込みグループ、すなわち1に設定されています.読み書き分離のルーティングルールが一致しない場合、デフォルトグループのデータベースにアクセスします.
([email protected]:6032) [(none)]> insert into mysql_users(username,password,default_hostgroup,transaction_persistent)values('proxysql','pwproxysql',1,1);
Query OK, 1 row affected (0.00 sec)
([email protected]:6032) [(none)]> select * from mysql_users \G
*************************** 1. row ***************************
username: proxysql
password: pwproxysql
active: 1
use_ssl: 0
default_hostgroup: 1
default_schema: NULL
schema_locked: 0
transaction_persistent: 1
fast_forward: 0
backend: 1
frontend: 1
max_connections: 10000
1 row in set (0.00 sec)
([email protected]:6032) [(none)]> load mysql users to runtime;
Query OK, 0 rows affected (0.00 sec)
([email protected]:6032) [(none)]> save mysql users to disk;
Query OK, 0 rows affected (0.01 sec)
健康診断のアカウントを追加mysqlのmaster端にproxysqlに属する読み取り専用アカウントを追加
mysql> GRANT SELECT ON *.* TO 'monitor'@'192.168.161.%' IDENTIFIED BY 'monitor';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.03 sec)
proxysqlホスト側で変数を変更して健康診断のアカウントを設定します
([email protected]:6032) [(none)]> set mysql-monitor_username='monitor';
Query OK, 1 row affected (0.01 sec)
([email protected]:6032) [(none)]> set mysql-monitor_password='monitor';
Query OK, 1 row affected (0.00 sec)
([email protected]:6032) [(none)]> load mysql variables to runtime;
Query OK, 0 rows affected (0.00 sec)
([email protected]:6032) [(none)]> save mysql variables to disk;
Query OK, 97 rows affected (0.01 sec)
リード/ライト分離のルーティング・ルール要件を追加するには、次の手順に従います.
([email protected]:6032) [(none)]> insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply)values(1,1,'^SELECT.*FOR UPDATE$',1,1);
Query OK, 1 row affected (0.00 sec)
([email protected]:6032) [(none)]> insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply)values(2,1,'^SELECT',2,1);
Query OK, 1 row affected (0.00 sec)
([email protected]:6032) [(none)]> select rule_id,active,match_digest,destination_hostgroup,apply from mysql_query_rules;
+---------+--------+----------------------+-----------------------+-------+
| rule_id | active | match_digest | destination_hostgroup | apply |
+---------+--------+----------------------+-----------------------+-------+
| 1 | 1 | ^SELECT.*FOR UPDATE$ | 1 | 1 |
| 2 | 1 | ^SELECT | 2 | 1 |
+---------+--------+----------------------+-----------------------+-------+
2 rows in set (0.00 sec)
([email protected]:6032) [(none)]> load mysql query rules to runtime;
Query OK, 0 rows affected (0.00 sec)
([email protected]:6032) [(none)]> load admin variables to runtime;
Query OK, 0 rows affected (0.00 sec)
([email protected]:6032) [(none)]> save mysql query rules to disk;
Query OK, 0 rows affected (0.01 sec)
([email protected]:6032) [(none)]> save admin variables to disk;
Query OK, 31 rows affected (0.03 sec)
認証読み書き分離ログインproxysqlクライアントログインユーザー先mysql_userテーブルに作成されたユーザー、ポート6033
[root@localhost ~]# mysql -uproxysql -ppwproxysql -h127.0.0.1 -P6033
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.30 (ProxySQL)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
([email protected]:6033) [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.05 sec)
データベースとクエリーを変更して2つのデータベースを作成し、テーブルをクエリーしてみます.
([email protected]:6033) [(none)]> create database xixi;
Query OK, 1 row affected (0.00 sec)
([email protected]:6033) [(none)]> create database haha;
Query OK, 1 row affected (0.00 sec)
([email protected]:6033) [(none)]> select user,host from mysql.user;
+---------------+----------------+
| user | host |
+---------------+----------------+
| monitor | 192.168.161.% |
| proxysql | 192.168.161.96 |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+---------------+----------------+
5 rows in set (0.00 sec)
読み書き分離に成功したかどうかを確認するproxysqlには監査のような機能があり、proxysql管理側で実行する必要があるSQLの実行状況を表示できます.
[root@localhost ~]# mysql -uadmin -padmin -h127.0.0.1 -P6032
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.30 (ProxySQL Admin Module)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
([email protected]:6032) [(none)]> select * from stats_mysql_query_digest;
+-----------+--------------------+----------+--------------------+----------------------------------+------------+------------+------------+----------+----------+----------+
| hostgroup | schemaname | username | digest | digest_text | count_star | first_seen | last_seen | sum_time | min_time | max_time |
+-----------+--------------------+----------+--------------------+----------------------------------+------------+------------+------------+----------+----------+----------+
| 1 | information_schema | proxysql | 0xEB4549A4A010C504 | create database haha | 1 | 1543034576 | 1543034576 | 2522 | 2522 | 2522 |
| 1 | information_schema | proxysql | 0x2C9014AFB3100E16 | create database xixi | 1 | 1543034570 | 1543034570 | 2132 | 2132 | 2132 |
| 2 | information_schema | proxysql | 0x0F02B330C823D739 | select user,host from mysql.user | 1 | 1543034594 | 1543034594 | 3562 | 3562 | 3562 |
+-----------+--------------------+----------+--------------------+----------------------------------+------------+------------+------------+----------+----------+----------+
6 rows in set (0.00 sec)
上のhostgroupとdigestからtext値を見ると、すべての書き込み操作は1グループにルーティングされ、読み取り操作は2グループにルーティングされ、そのうち1グループは書き込みグループ、2グループは読み取りグループである!
このことから,読み書き分離に成功した.