Mysql讀寫分離—5.7 gtid 主從 + ProxySql 配置及簡單測試

 
          ProxySQL 配置詳解及讀寫分離(+GTID)等功能說明 (完整篇) https://www.cnblogs.com/kevingrace/p/10329714.html
 
 
實驗目的:用proxysql實現讀寫分離。主庫負責寫入,從庫負責寫入(要設置read_only=1參數)。 因爲只是demo 暫時proxysql安裝在主庫的服務器上。 
實驗前提:已有一主一從 兩臺mysql服務(IP如下),已開啓了GTID,從庫已設置read_only=1參數。
 
環境說明:
proxysql的ip:192.168.56.80
主庫的ip:192.168.56.80       server_id:222
從庫的ip:192.168.56.61       server_id:111
 
 
 
 
一:proxy架構
 
proxysql是比較靈活的中間件,可實現讀寫分離、在線實時調整參數等。
 
 
--1、proxysql的架構圖:
 
 
--2、proxysql三層概念:
runtime: 內存運行,代表的是ProxySQL當前生效的配置,必須從memory層load進來才能生效。
memory:通常情況下只在memory層修改內存配置,但不影響runtime和disk層。 修改後可以load到runtime層 使其現在運行的環境生效,沒問題後可以 save到disk層持久化保存
disk:持久化的存儲配置參數 到  SQLite3數據庫( $(DATADIR)/proxysql.db)。/etc/proxysql.cnf 參數配置文件只在第一次啓動時候加載,之後的啓動都從proxysql.db加載配置數據
 
 
 
--3、安裝proxysql
 
安裝包:proxysql-2.0.10-1-centos7.x86_64.rpm
 
--安裝proxysql
--安裝proxysql , 缺失包 詳見附1
[root@hostmysql80 mysql_setup]# rpm -ivh proxysql-2.0.10-1-centos7.x86_64.rpm

--查看版本
[root@hostmysql80 ~]# proxysql --version
ProxySQL version 2.0.10-27-g5b31997, codename Truls

--啓動proxysql,(首次啓動時 讀取/etc/proxysql.cnf 參數配置文件 默認爲6032和6033。6032端口是ProxySQL的管理端口,6033是ProxySQL對外提供服務的端口)
[root@hostmysql80 mysql]# systemctl start proxysql.service

--查看當前proxysql的端口爲6032和6033
[root@hostmysql80 ~]# netstat -tunlp |grep proxy
tcp        0      0 0.0.0.0:6032            0.0.0.0:*               LISTEN      1019/proxysql       
tcp        0      0 0.0.0.0:6033            0.0.0.0:*               LISTEN      1019/proxysql   

--查看當前proxysql的線程數
[root@hostmysql80 ~]# ss -lntup|grep proxy
tcp    LISTEN     0      128       *:6032                  *:*                   users:(("proxysql",pid=1019,fd=26))
tcp    LISTEN     0      128       *:6033                  *:*                   users:(("proxysql",pid=1019,fd=24))
tcp    LISTEN     0      128       *:6033                  *:*                   users:(("proxysql",pid=1019,fd=23))
tcp    LISTEN     0      128       *:6033                  *:*                   users:(("proxysql",pid=1019,fd=22))
tcp    LISTEN     0      128       *:6033                  *:*                   users:(("proxysql",pid=1019,fd=21))

--disk層會生成保存持久化配置數據的db
[root@hostmysql80 proxysql]# ll /var/lib/proxysql/
total 520
-rw-rw----. 1 proxysql proxysql   1054 Mar 19 14:56 proxysql-ca.pem
-rw-rw----. 1 proxysql proxysql   1062 Mar 19 14:56 proxysql-cert.pem
-rw-------. 1 proxysql proxysql 196608 Mar 19 14:56 proxysql.db
-rw-rw----. 1 proxysql proxysql   1675 Mar 19 14:56 proxysql-key.pem
-rw-------. 1 proxysql proxysql   7942 Mar 20 09:15 proxysql.log
-rw-r--r--. 1 proxysql proxysql      5 Mar 20 09:15 proxysql.pid
-rw-------. 1 proxysql proxysql 311296 Mar 20 16:45 proxysql_stats.db

--查看proxysql狀態
[root@hostmysql80 mysql]# systemctl status proxysql.service
● proxysql.service - High Performance Advanced Proxy for MySQL
   Loaded: loaded (/etc/systemd/system/proxysql.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2020-03-20 09:15:08 CST; 6h ago
  Process: 951 ExecStart=/usr/bin/proxysql -c /etc/proxysql.cnf (code=exited, status=0/SUCCESS)
Main PID: 1017 (proxysql)
    Tasks: 20
   CGroup: /system.slice/proxysql.service
           ├─1017 /usr/bin/proxysql -c /etc/proxysql.cnf
           └─1018 /usr/bin/proxysql -c /etc/proxysql.cnf

Mar 20 09:15:07 hostmysql80 systemd[1]: Starting High Performance Advanced Proxy for MySQL...
Mar 20 09:15:07 hostmysql80 proxysql[951]: 2020-03-20 09:15:07 [INFO] Using config file /etc/proxysql.cnf
Mar 20 09:15:07 hostmysql80 proxysql[951]: 2020-03-20 09:15:07 [INFO] Using OpenSSL version: OpenSSL 1.1.0h  27 Mar 2018
Mar 20 09:15:07 hostmysql80 proxysql[951]: 2020-03-20 09:15:07 [INFO] SSL keys/certificates found in datadir (/var/lib/proxysql): loading them.
Mar 20 09:15:08 hostmysql80 proxysql[951]: Process 1067 died: No such process; trying to remove PID file. (/var/lib/proxysql/proxysql.pid)
Mar 20 09:15:08 hostmysql80 systemd[1]: Started High Performance Advanced Proxy for MySQL.


--進入proxysql ,用6032管理端口進入
[root@hostmysql80 mysql]# mysql -u admin -padmin -h 127.0.0.1 -P6032 --prompt='Admin> '
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 1
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (c) 2000, 2019, 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.
 
 
 
 
--4、proxysql啓動流程
4-1:首次啓動或者initial flag(初始化config file)時, 首先讀取配置文件CONFIG FILE(/etc/proxysql.cnf) ,然後從該配置文件中獲取datadir,datadir中配置的是sqlite的數據目錄,將config file中的參數配置加載到sqliteDB中(disk層 持久化)。
4-2:非首次啓動時,CONFIG FILE文件不讀取,只從disk層 持久化表 讀取參數配置數據。
4-3:非首次啓動 帶有 reload flag(重新加載config file)時,會合並disk層參數和config file文件參數 這兩個參數,但並不能保障能期盼的那樣合併成功。
 
 
 
--5、proxysql庫的基本信息
--查看proxysql的庫
Admin> 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.03 sec)

附 每個庫的含義;
--main 內存參數配置庫,表裏存放後端db實例、用戶驗證、路由規則等信息。表名以 runtime_開頭的表示proxysql當前運行的配置內容runtime層,不能通過dml語句修改,只能修改對應的不以 runtime_ 開頭的(在內存)裏的表(memory層),然後LOAD使其runtime層(runtime_開頭的表)生效, SAVE使其存到disk層(disk庫的表)硬盤以供下次重啓加載。
--disk庫 是持久化保存到硬盤參數配置庫,存儲在sqliteDB數據文件中。
--stats庫 是proxysql運行時抓取的統計信息。
--monitor庫 監控模塊,記錄mysql數據庫的監控信息。


--查看main庫 (disk庫除了沒有runtime_開頭的表之外,其他的都和main庫的表一致。)
--具體的核心配置表(以下紅色標出)的字段說明 可參考如下 附2
Admin> show tables from main;
+----------------------------------------------------+
| tables                                             |
+----------------------------------------------------+
| global_variables                                   |    //the list of global variables which the proxy is configured to use, and which can be tweaked during runtime 全局變量表, 可以在線實時調整
| mysql_aws_aurora_hostgroups                        |
| mysql_collations                                   |    //the list of MySQL collations available for the proxy to work with 字符集和校驗規則
| mysql_firewall_whitelist_rules                     |
| mysql_firewall_whitelist_sqli_fingerprints         |
| mysql_firewall_whitelist_users                     |
| mysql_galera_hostgroups                            |
| mysql_group_replication_hostgroups                 |
| mysql_query_rules                                  |    //the list of query rules which are evaluated when routing traffic to the various backend servers. 定義查詢路由規則
| mysql_query_rules_fast_routing                     |
| mysql_replication_hostgroups                       |    //defines replication hostgroups for use with traditional master / slave ASYNC or SEMI-SYNC replication 讀寫分組信息
| mysql_servers                                      |    //the list of backend servers which ProxySQL connects to 設置proxysql讀寫等配置的表
| mysql_users                                        |    //the list of users and their credentials which connect to ProxySQL 配置連接數據庫的賬號和監控賬號    
| proxysql_servers                                   |
| restapi_routes                                     |
| runtime_checksums_values                           |
| runtime_global_variables                           |
| runtime_mysql_aws_aurora_hostgroups                |
| runtime_mysql_firewall_whitelist_rules             |
| runtime_mysql_firewall_whitelist_sqli_fingerprints |
| runtime_mysql_firewall_whitelist_users             |
| runtime_mysql_galera_hostgroups                    |
| 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_restapi_routes                             |
| runtime_scheduler                                  |
| scheduler                                          |
+----------------------------------------------------+
32 rows in set (0.00 sec)

--查看stats庫
Admin> show tables from stats;
+--------------------------------------+
| tables                               |
+--------------------------------------+
| global_variables                     |
| stats_memory_metrics                 |
| stats_mysql_commands_counters        |
| stats_mysql_connection_pool          |
| stats_mysql_connection_pool_reset    |
| stats_mysql_errors                   |
| stats_mysql_errors_reset             |
| stats_mysql_free_connections         |
| stats_mysql_global                   |
| stats_mysql_gtid_executed            |
| stats_mysql_prepared_statements_info |
| stats_mysql_processlist              |
| stats_mysql_query_digest             |
| stats_mysql_query_digest_reset       |
| stats_mysql_query_rules              |
| stats_mysql_users                    |
| stats_proxysql_servers_checksums     |
| stats_proxysql_servers_metrics       |
| stats_proxysql_servers_status        |
+--------------------------------------+
19 rows in set (0.01 sec)

--查看monitor庫
Admin> show tables from monitor;
+--------------------------------------+
| tables                               |
+--------------------------------------+
| mysql_server_aws_aurora_check_status |
| mysql_server_aws_aurora_failovers    |
| mysql_server_aws_aurora_log          |
| mysql_server_connect_log             |
| mysql_server_galera_log              |
| mysql_server_group_replication_log   |
| mysql_server_ping_log                |
| mysql_server_read_only_log           |
| mysql_server_replication_lag_log     |
+--------------------------------------+
9 rows in set (0.01 sec)
 
 
 
 
--6、proxysql配置生效操作
--proxysql有三層配置,runtime、memory、disk。 每一層都是獨立存在的,如果需要修改proxysql的參數配置,方式如下:
首先在memory層修改配置參數,不會影響現有運行的runtime層環境 以及 持久化保存參數的disk層環境。當memory修改完畢後,可以load到runtime 讓現有運行環境runtime層生效,沒問題後可以save到disk層讓修改後的參數配置持久化。

一般有load from 和load to, save to和save from 兩種方式加載參數配置。但一般習慣使用 load to 和save to 方式(即從memory加載到runtime用load to,memory加載到disk用save to)

--mysql_users表的加載
LOAD MYSQL USERS TO RUNTIME
SAVE MYSQL USERS TO DISK

--mysql_servers表的加載
LOAD MYSQL SERVERS TO RUNTIME
SAVE MYSQL SERVERS TO DISK

--mysql_query_rules表的加載
LOAD MYSQL QUERY RULES TO RUNTIME
SAVE MYSQL QUERY RULES TO DISK

--global_variables表的加載
LOAD MYSQL VARIABLES TO RUNTIME
SAVE MYSQL VARIABLES TO DISK

--global_variables表中 admin- 開頭參數配置 的加載
LOAD ADMIN VARIABLES TO RUNTIME
SAVE ADMIN VARIABLES TO DISK
 
 
 
 
 
 
--7、配置proxysql實現讀寫分離   參考官網:https://github.com/sysown/proxysql/wiki/ProxySQL-Configuration
--流程:1、確認要配置的表是無數據的(參考7.1)
             2、配置mysql_servers表 新增所有mysql服務信息(包括ip 端口等) (參考7.2)
             3、配置monitor監控,後續會把read_only開啓的mysql服務自動調整到讀組(參考7.3)
             4、配置mysql_replication_hostgroups表 新增讀組和寫組的信息(參考7.4)
             5、配置mysql_users表 新增通過proxysql來連接mysql服務的用戶信息。此表可以通過配置用戶分組信息來實現用戶級的讀寫分離(參考7.5)
             6、配置mysql_query_rules表 新增路由規則 比如可新增一些查詢語句的正則表達式來訪問只讀庫。此表可以通過配置sql語句的正則表達式來實現sql語句級的讀寫分離(參考7.6)
             7、查看stats統計信息的表,可以查看sql統計、讀寫分離 等等 一些信息(參考7.7)
--7.1查看要配置的核心配置表,查看應該是無數據的。
[root@hostmysql80 mysql]# mysql -u admin -padmin -h 127.0.0.1 -P6032 --prompt='Admin> '

--切換到main
Admin> use main;

Admin> SELECT * FROM mysql_servers;                   //配置mysql服務的信息
Empty set (0.01 sec)
Admin> SELECT * from mysql_replication_hostgroups;    //配置寫組和讀組的信息
Empty set (0.01 sec)
Admin> SELECT * from mysql_users;                     //配置通過proxysql連接mysql服務的用戶賬號的信息
Empty set (0.01 sec)
Admin> SELECT * from mysql_query_rules;               //配置路由規則
Empty set (0.01 sec)


--7.2在mysql_servers表中新增mysql的主庫和從庫信息。具體的mysql_servers表的字段說明 可參考如下 附2
--192.168.56.80爲主庫ip,192.168.56.61爲從庫ip。hostgroup_id是分組id,這裏設置1爲寫組,2爲讀組(這裏暫定都設置1組,後面ProxySQL需要通過每個節點的read_only值來自動調整它們是屬於讀組還是寫組,比如從庫192.168.56.61 會自動的把hostgroup_id 設置爲2 詳見7.6)。
Admin> INSERT INTO mysql_servers(hostgroup_id,hostname,port) VALUES (1,'192.168.56.80',3306);

Admin> INSERT INTO mysql_servers(hostgroup_id,hostname,port) VALUES (1,'192.168.56.61',3306);

--查看mysql_servers
Admin> select * from mysql_servers;
+--------------+---------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| hostgroup_id | hostname      | port | gtid_port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+---------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 1            | 192.168.56.80 | 3306 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 1            | 192.168.56.61 | 3306 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
+--------------+---------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
2 rows in set (0.00 sec)

--加載到runtime和disk
Admin> load mysql servers to runtime;

Admin> save mysql servers to disk;


--7.3後端添加monitor監控。會監控主從的信息,寫入到監控表中。還會把read_only爲1的庫自動設置爲讀組
--登錄主庫192.168.56.80添加監控的用戶monitor,設置密碼、權限等。
[root@hostmysql80 ~]# mysql -uroot -p

mysql> create user monitor@'192.168.56.%' identified by 'Monitor123$';

mysql> grant replication client on *.* to monitor@'192.168.56.%';

mysql>  flush privileges;



--返回到proxysql 設置連接mysql數據庫的監控monitor用戶和密碼
Admin> set mysql-monitor_username='monitor';

Admin> set mysql-monitor_password='Monitor123$';

Admin> select * from global_variables where variable_name like '%monitor_username%' or variable_name like '%monitor_password%';
+------------------------+----------------+
| variable_name          | variable_value |
+------------------------+----------------+
| mysql-monitor_username | monitor        |
| mysql-monitor_password | Monitor123$    |
+------------------------+----------------+
2 rows in set (0.00 sec)

--加載到runtime和disk
Admin> load mysql variables to runtime;

Admin> save mysql variables to disk;


--查看monitor監控表的信息
Admin> SHOW TABLES FROM monitor;
+--------------------------------------+
| tables                               |
+--------------------------------------+
| mysql_server_aws_aurora_check_status |
| mysql_server_aws_aurora_failovers    |
| mysql_server_aws_aurora_log          |
| mysql_server_connect_log             |
| mysql_server_galera_log              |
| mysql_server_group_replication_log   |
| mysql_server_ping_log                |
| mysql_server_read_only_log           |
| mysql_server_replication_lag_log     |
+--------------------------------------+
9 rows in set (0.01 sec)

--判斷連接是否正常的監控表
Admin> SELECT * FROM monitor.mysql_server_connect_log ORDER BY time_start_us DESC LIMIT 10;
+---------------+------+------------------+-------------------------+---------------+
| hostname      | port | time_start_us    | connect_success_time_us | connect_error |
+---------------+------+------------------+-------------------------+---------------+
| 192.168.56.80 | 3306 | 1584948975209848 | 2310                    | NULL          |
| 192.168.56.61 | 3306 | 1584948974072648 | 4395                    | NULL          |
| 192.168.56.80 | 3306 | 1584948914769017 | 1650                    | NULL          |
| 192.168.56.61 | 3306 | 1584948914071812 | 2711                    | NULL          |
| 192.168.56.80 | 3306 | 1584948854877383 | 1566                    | NULL          |
| 192.168.56.61 | 3306 | 1584948854070703 | 1196                    | NULL          |
| 192.168.56.61 | 3306 | 1584948795047377 | 1380                    | NULL          |
| 192.168.56.80 | 3306 | 1584948794070877 | 1757                    | NULL          |
| 192.168.56.61 | 3306 | 1584948734867748 | 4011                    | NULL          |
| 192.168.56.80 | 3306 | 1584948734070699 | 1633                    | NULL          |
+---------------+------+------------------+-------------------------+---------------+
10 rows in set (0.00 sec)

--ping心跳信息的監控表
Admin> SELECT * FROM monitor.mysql_server_ping_log ORDER BY time_start_us DESC LIMIT 10;
+---------------+------+------------------+----------------------+------------+
| hostname      | port | time_start_us    | ping_success_time_us | ping_error |
+---------------+------+------------------+----------------------+------------+
| 192.168.56.80 | 3306 | 1584949004468120 | 133                  | NULL       |
| 192.168.56.61 | 3306 | 1584949004365508 | 1479                 | NULL       |
| 192.168.56.61 | 3306 | 1584948994535963 | 789                  | NULL       |
| 192.168.56.80 | 3306 | 1584948994364743 | 123                  | NULL       |
| 192.168.56.80 | 3306 | 1584948984501357 | 256                  | NULL       |
| 192.168.56.61 | 3306 | 1584948984364424 | 1403                 | NULL       |
| 192.168.56.80 | 3306 | 1584948974506591 | 554                  | NULL       |
| 192.168.56.61 | 3306 | 1584948974364011 | 1329                 | NULL       |
| 192.168.56.80 | 3306 | 1584948964493164 | 225                  | NULL       |
| 192.168.56.61 | 3306 | 1584948964363715 | 1497                 | NULL       |
+---------------+------+------------------+----------------------+------------+
10 rows in set (0.00 sec)



--7.4 設置複製組信息的表,設定寫組爲1,讀組爲2
Admin> insert into mysql_replication_hostgroups (writer_hostgroup ,reader_hostgroup ,comment ) values (1,2,'proxysql_demo');
Query OK, 1 row affected (0.00 sec)

* If they have read_only=0 , they will be moved to hostgroup 1
* If they have read_only=1 , they will be moved to hostgroup 2


--加載到runtime和disk
Admin> load mysql servers to runtime;

Admin> save mysql servers to disk;

--再次查看mysql_servers表,因爲192.168.56.61的從庫read_only設置爲1,所以被monitor設置爲了讀組2。這樣主庫192.168.56.80就爲只寫的組,192.168.56.61從庫只讀,實現了庫的讀寫分離配置。
Admin> select * from mysql_servers;
+--------------+---------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| hostgroup_id | hostname      | port | gtid_port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+---------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 1            | 192.168.56.80 | 3306 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 2            | 192.168.56.61 | 3306 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
+--------------+---------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
2 rows in set (0.00 sec)

--查看mysql庫設置了read_only參數的狀態表
Admin>  SELECT * FROM monitor.mysql_server_read_only_log ORDER BY time_start_us DESC LIMIT 10;
+---------------+------+------------------+-----------------+-----------+-------+
| hostname      | port | time_start_us    | success_time_us | read_only | error |
+---------------+------+------------------+-----------------+-----------+-------+
| 192.168.56.61 | 3306 | 1584949238400396 | 1294            | 1         | NULL  |
| 192.168.56.80 | 3306 | 1584949238378363 | 555             | 0         | NULL  |
| 192.168.56.61 | 3306 | 1584949236907160 | 1734            | 1         | NULL  |
| 192.168.56.80 | 3306 | 1584949236877600 | 320             | 0         | NULL  |
| 192.168.56.80 | 3306 | 1584949235401126 | 183             | 0         | NULL  |
| 192.168.56.61 | 3306 | 1584949235377586 | 820             | 1         | NULL  |
| 192.168.56.80 | 3306 | 1584949233899518 | 401             | 0         | NULL  |
| 192.168.56.61 | 3306 | 1584949233877797 | 1786            | 1         | NULL  |
| 192.168.56.80 | 3306 | 1584949232407828 | 739             | 0         | NULL  |
| 192.168.56.61 | 3306 | 1584949232377432 | 1716            | 1         | NULL  |
+---------------+------+------------------+-----------------+-----------+-------+
10 rows in set (0.00 sec)


--7.5 添加通過proxysql來連接mysql的用戶   mysql_users 表
Admin> select * from mysql_users;
Empty set (0.00 sec)

--登錄主庫192.168.56.80添加通過proxysql來連接mysql的用戶,設置密碼、權限等。  proxy_user用戶可以讀寫,read_user只讀
[root@hostmysql80 ~]# mysql -uroot -p

mysql> create user proxy_user@'192.168.56.%' identified by 'proxy_user123$';

mysql> grant all on *.* to proxy_user@'192.168.56.%';

mysql> create user read_user@'192.168.56.%' identified by 'read_user123$';

mysql> grant select on *.* to read_user@'192.168.56.%';

mysql>  flush privileges;

--返回到proxysql 設置連接mysql數據庫的讀寫proxy_user和只讀read_user用戶和密碼。default_hostgroup爲默認的組,proxy_user設置1爲寫組 read_user設置2爲讀組。具體的mysql_users表的字段說明 可參考如下 附2
Admin> insert into mysql_users(username,password,default_hostgroup) values('proxy_user','proxy_user123$',1);

Admin> insert into mysql_users(username,password,default_hostgroup) values('read_user','read_user123$',2);

Admin> select * from mysql_users;
+------------+----------------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+---------+
| username   | password       | active | use_ssl | default_hostgroup | default_schema | schema_locked | transaction_persistent | fast_forward | backend | frontend | max_connections | comment |
+------------+----------------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+---------+
| proxy_user | proxy_user123$ | 1      | 0       | 1                 | NULL           | 0             | 1                      | 0            | 1       | 1        | 10000           |         |
| read_user  | read_user123$  | 1      | 0       | 2                 | NULL           | 0             | 1                      | 0            | 1       | 1        | 10000           |         |
+------------+----------------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+---------+
2 rows in set (0.00 sec)

--加載到runtime和disk
Admin> load mysql users to runtime;

Admin> save mysql users to disk;


--主庫server_id:222,從庫server_id:111
--通過uproxy_user用戶登錄mysql服務器,因爲該用戶是寫組1,所以會自動連接主庫(server_id:222)。
[root@hostmysql80 ~]# mysql -uproxy_user -pproxy_user123$ -h192.168.56.80 -P6033

mysql> show variables like 'server_id';
+----------------+-------+
| Variable_name  | Value |
+----------------+-------+
| server_id      | 222   |
+----------------+-------+
1 rows in set (0.14 sec)


--通過read_user用戶登錄mysql服務器,因爲該用戶是讀組2,所以會自動連接從庫(server_id:111)。
[root@hostmysql61 ~]# mysql -uread_user -pread_user123$ -h192.168.56.80 -P6033

mysql> show variables like 'server_id';
+----------------+-------+
| Variable_name  | Value |
+----------------+-------+
| server_id      | 111   |
+----------------+-------+
1 rows in set (0.02 sec)



--7.6配置路由規則 
--新增一個簡單的路由規則,把select開頭的sql語句 放到從庫(server_id:111)上面運行(也就是讀組2)。
Admin> insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply) VALUES  (1,1,'^SELECT',2,1);
Query OK, 1 row affected (0.00 sec)

--加載到runtime和disk
Admin> load mysql query rules to runtime;

Admin> save mysql query rules to disk;

--通過uproxy_user用戶登錄mysql服務器,因爲該用戶是寫組1,所以會自動連接主庫(server_id:222)。
[root@hostmysql80 ~]# mysql -uproxy_user -pproxy_user123$ -h192.168.56.80 -P6033

mysql> show variables like 'server_id';
+----------------+-------+
| Variable_name  | Value |
+----------------+-------+
| server_id      | 222   |
+----------------+-------+
1 rows in set (0.14 sec)

--執行建表操作
mysql> create table flydb.t_proxysql_rule_test (a int);
Query OK, 0 rows affected (0.05 sec)

--執行插入數據操作
mysql> insert into flydb.t_proxysql_rule_test values(1);
Query OK, 1 row affected (0.01 sec)

--執行select查詢操作,因爲select開頭 所以應該走的從庫(server_id:111)上面運行(也就是讀組2)
mysql> select * from flydb.t_proxysql_rule_test;
+------+
| a    |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

--可以通過stats庫下的stats_mysql_query_digest表查看具體sql運行的信息
--可以查看到紅色標記的三個sql都是通過proxy_user用戶連接到mysql服務的,執行建表和插入操作都是走的寫組1(主庫操作),select開頭的查詢sql走的讀組2(從庫操作)
Admin> SELECT hostgroup,schemaname,username,client_address,digest_text,count_star  FROM stats.stats_mysql_query_digest ORDER BY sum_time DESC limit 10;
+-----------+--------------------+------------+----------------+--------------------------------------------------+------------+
| hostgroup | schemaname         | username   | client_address | digest_text                                      | count_star |
+-----------+--------------------+------------+----------------+--------------------------------------------------+------------+
| 1         | information_schema | proxy_user |                | show variables like ?                            | 2          |
| 2         | information_schema | read_user  |                | show variables like ?                            | 1          |
| 1         | information_schema | proxy_user |                | create table flydb.t_proxysql_rule_test (a int)  | 1          |
| 1         | information_schema | proxy_user |                | insert into flydb.t_proxysql_rule_test values(?) | 1          |
| 2         | information_schema | proxy_user |                | select * from flydb.t_proxysql_rule_test         | 1          |
| 2         | information_schema | read_user  |                | select @@version_comment limit ?                 | 1          |
| 1         | information_schema | proxy_user |                | select @@version_comment limit ?                 | 2          |
+-----------+--------------------+------------+----------------+--------------------------------------------------+------------+
8 rows in set (0.00 sec)



--7.7查看stats統計信息的表
Admin> SHOW TABLES FROM stats;
+--------------------------------------+
| tables                               |
+--------------------------------------+
| global_variables                     |
| stats_memory_metrics                 |
| stats_mysql_commands_counters        |
| stats_mysql_connection_pool          |
| stats_mysql_connection_pool_reset    |
| stats_mysql_errors                   |
| stats_mysql_errors_reset             |
| stats_mysql_free_connections         |
| stats_mysql_global                   |
| stats_mysql_gtid_executed            |
| stats_mysql_prepared_statements_info |
| stats_mysql_processlist              |
| stats_mysql_query_digest             |
| stats_mysql_query_digest_reset       |
| stats_mysql_query_rules              |
| stats_mysql_users                    |
| stats_proxysql_servers_checksums     |
| stats_proxysql_servers_metrics       |
| stats_proxysql_servers_status        |
+--------------------------------------+
19 rows in set (0.03 sec)


--查看統計mysql數據庫連接池信息的表
Admin> SELECT * FROM stats.stats_mysql_connection_pool;
+-----------+---------------+----------+--------+----------+----------+--------+---------+-------------+---------+-------------------+-----------------+-----------------+------------+
| hostgroup | srv_host      | srv_port | status | ConnUsed | ConnFree | ConnOK | ConnERR | MaxConnUsed | Queries | Queries_GTID_sync | Bytes_data_sent | Bytes_data_recv | Latency_us |
+-----------+---------------+----------+--------+----------+----------+--------+---------+-------------+---------+-------------------+-----------------+-----------------+------------+
| 1         | 192.168.56.80 | 3306     | ONLINE | 0        | 1        | 1      | 0       | 1           | 29      | 0                 | 951             | 543             | 398        |
| 2         | 192.168.56.61 | 3306     | ONLINE | 0        | 1        | 1      | 0       | 1           | 2       | 0                 | 66              | 80              | 1490       |
+-----------+---------------+----------+--------+----------+----------+--------+---------+-------------+---------+-------------------+-----------------+-----------------+------------+
2 rows in set (0.06 sec)


--查看統計運行sql次數的表
Admin> SELECT * FROM stats_mysql_commands_counters WHERE Total_cnt;
+--------------+---------------+-----------+-----------+-----------+---------+---------+----------+----------+-----------+-----------+--------+--------+---------+----------+
| Command      | Total_Time_us | Total_cnt | cnt_100us | cnt_500us | cnt_1ms | cnt_5ms | cnt_10ms | cnt_50ms | cnt_100ms | cnt_500ms | cnt_1s | cnt_5s | cnt_10s | cnt_INFs |
+--------------+---------------+-----------+-----------+-----------+---------+---------+----------+----------+-----------+-----------+--------+--------+---------+----------+
| CREATE_TABLE | 62530         | 2         | 0         | 0         | 0       | 0       | 0        | 1        | 1         | 0         | 0      | 0      | 0       | 0        |
| INSERT       | 9794          | 1         | 0         | 0         | 0       | 0       | 1        | 0        | 0         | 0         | 0      | 0      | 0       | 0        |
| SELECT       | 4229          | 4         | 3         | 0         | 0       | 1       | 0        | 0        | 0         | 0         | 0      | 0      | 0       | 0        |
| SHOW         | 192809        | 3         | 0         | 0         | 0       | 0       | 0        | 2        | 0         | 1         | 0      | 0      | 0       | 0        |
+--------------+---------------+-----------+-----------+-----------+---------+---------+----------+----------+-----------+-----------+--------+--------+---------+----------+
4 rows in set (0.01 sec)


--查看統計運行具體sql的表
Admin> SELECT * FROM stats_mysql_query_digest ORDER BY sum_time DESC limit 5;
+-----------+--------------------+------------+----------------+--------------------+--------------------------------------------------+------------+------------+------------+----------+----------+----------+-------------------+---------------+
| hostgroup | schemaname         | username   | client_address | digest             | digest_text                                      | count_star | first_seen | last_seen  | sum_time | min_time | max_time | sum_rows_affected | sum_rows_sent |
+-----------+--------------------+------------+----------------+--------------------+--------------------------------------------------+------------+------------+------------+----------+----------+----------+-------------------+---------------+
| 1         | information_schema | proxy_user |                | 0xB06310E983BD5E0B | show variables like ?                            | 2          | 1585017073 | 1585017535 | 161593   | 36212    | 125381   | 0                 | 2             |
| 1         | information_schema | proxy_user |                | 0xC8D7E725163C0840 | create table flydb.t_proxysql_rule_test (a int)  | 1          | 1585017569 | 1585017569 | 50755    | 50755    | 50755    | 0                 | 0             |
| 2         | information_schema | read_user  |                | 0xB06310E983BD5E0B | show variables like ?                            | 1          | 1585017083 | 1585017083 | 31216    | 31216    | 31216    | 0                 | 1             |
| 1         | information_schema | proxy_user |                | 0x71C66C96E0C12B1D | create table t_proxysql_rule_test (a int)        | 1          | 1585017562 | 1585017562 | 11775    | 11775    | 11775    | 0                 | 0             |
| 1         | information_schema | proxy_user |                | 0x0DEF4CDD8F99F1E6 | insert into flydb.t_proxysql_rule_test values(?) | 1          | 1585017580 | 1585017580 | 9794     | 9794     | 9794     | 1                 | 0             |
+-----------+--------------------+------------+----------------+--------------------+--------------------------------------------------+------------+------------+------------+----------+----------+----------+-------------------+---------------+
5 rows in set (0.01 sec)


--看top sql的信息。
Admin> SELECT hostgroup hg, sum_time, count_star, digest_text FROM stats_mysql_query_digest ORDER BY sum_time DESC limit 10;
+----+----------+------------+------------------------------------------+
| hg | sum_time | count_star | digest_text                              |
+----+----------+------------+------------------------------------------+
| 1  | 134873   | 1          | show variables like ?                    |
| 1  | 61296    | 1          | create table t_proxysql_test(a int )     |
| 1  | 24725    | 1          | SELECT * FROM gtid_executed WHERE ?=?    |
| 1  | 18057    | 1          | show databases                           |
| 1  | 17840    | 1          | SELECT DATABASE()                        |
| 1  | 17791    | 1          | insert into t_proxysql_test values (?)   |
| 2  | 13127    | 2          | show variables like ?                    |
| 1  | 10006    | 1          | SELECT * FROM t3 WHERE ?=?               |
| 1  | 9314     | 1          | SELECT * FROM t_auto WHERE ?=?           |
| 1  | 5310     | 1          | SELECT * FROM test_limit_table WHERE ?=? |
+----+----------+------------+------------------------------------------+
10 rows in set (0.00 sec)
 
 
 
 
 
 
 
 
 
--8、通過進入sqlite庫,查看disk層的數據。
--拷貝一份disk層的數據  做測試
[root@hostmysql80 proxysql]# cp /var/lib/proxysql/proxysql.db    /mysql_setup/proxysql_test.db
[root@hostmysql80 proxysql]# cd /mysql_setup/

--進入sqlite庫 
[root@hostmysql80 mysql_setup]# sqlite3
SQLite version 3.7.17 2013-05-20 00:56:22
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .databases
seq  name             file                                                      
---  ---------------  ----------------------------------------------------------
0    main                         

--加載測試庫proxysql_test.db 到sqlite庫中                                             
sqlite>  ATTACH DATABASE 'proxysql_test.db' as 'test';
sqlite> .databases
seq  name             file                                                      
---  ---------------  ----------------------------------------------------------
0    main                                                                       
2    test             /mysql_setup/proxysql_test.db  

--設置顯示字段
sqlite> .header on
--設置列對其
sqlite> .mode column
--設置列的寬度(這裏只設置了前三列)
sqlite> .width 20, 20, 10
--select可以查詢到disk層的數據
sqlite> select hostgroup_id,hostname,port  from mysql_servers;
hostgroup_id          hostname              port      
--------------------  --------------------  ----------
1                     192.168.56.80         3306      
2                     192.168.56.61         3306  
 
 
附1:安裝時缺失包,用yum安裝一下。
[root@hostmysql80 mysql_setup]# rpm -ivh proxysql-2.0.10-1-centos7.x86_64.rpm
error: Failed dependencies:
        perl(DBD::mysql) is needed by proxysql-2.0.10-1.x86_64
        perl(DBI) is needed by proxysql-2.0.10-1.x86_64
[root@hostmysql80 mysql_setup]# yum install -y perl-DBI.x86_64
[root@hostmysql80 mysql_setup]# yum install -y perl-DBD-mysql.el7.x86_64
 
 
 
 
 
附2:核心表的字段說明:

mysql_servers

Table mysql_servers defines all the MySQL servers:
Admin> SHOW CREATE TABLE mysql_servers\G
*************************** 1. row ***************************
       table: mysql_servers
Create Table: CREATE TABLE mysql_servers (
    hostgroup_id INT CHECK (hostgroup_id>=0) NOT NULL DEFAULT 0,
    hostname VARCHAR NOT NULL,
    port INT NOT NULL DEFAULT 3306,
    gtid_port INT CHECK (gtid_port <> port) NOT NULL DEFAULT 0,
    status VARCHAR CHECK (UPPER(status) IN ('ONLINE','SHUNNED','OFFLINE_SOFT', 'OFFLINE_HARD')) NOT NULL DEFAULT 'ONLINE',
    weight INT CHECK (weight >= 0 AND weight <=10000000) NOT NULL DEFAULT 1,
    compression INT CHECK (compression >=0 AND compression <= 102400) NOT NULL DEFAULT 0,
    max_connections INT CHECK (max_connections >=0) NOT NULL DEFAULT 1000,
    max_replication_lag INT CHECK (max_replication_lag >= 0 AND max_replication_lag <= 126144000) NOT NULL DEFAULT 0,
    use_ssl INT CHECK (use_ssl IN(0,1)) NOT NULL DEFAULT 0,
    max_latency_ms INT UNSIGNED CHECK (max_latency_ms>=0) NOT NULL DEFAULT 0,
    comment VARCHAR NOT NULL DEFAULT '',
    PRIMARY KEY (hostgroup_id, hostname, port) )
1 row in set (0.00 sec)
 
 
The fields have the following semantics:
  • hostgroup_id: the hostgroup in which this mysqld instance is included. Notice that the same instance can be part of more than one hostgroup
  • hostname, port: the TCP endpoint at which the mysqld instance can be contacted
  • gtid_port: the backend server port where ProxySQL Binlog Reader listens on for GTID tracking
  • status:
    • ONLINE - backend server is fully operational
    • SHUNNED - backend sever is temporarily taken out of use because of either too many connection errors in a time that was too short, or replication lag exceeded the allowed threshold
    • OFFLINE_SOFT - when a server is put into OFFLINE_SOFT mode, new incoming connections aren't accepted anymore, while the existing connections are kept until they became inactive. In other words, connections are kept in use until the current transaction is completed. This allows to gracefully detach a backend
    • OFFLINE_HARD - when a server is put into OFFLINE_HARD mode, the existing connections are dropped, while new incoming connections aren't accepted either. This is equivalent to deleting the server from a hostgroup, or temporarily taking it out of the hostgroup for maintenance work
  • weight - the bigger the weight of a server relative to other weights, the higher the probability of the server to be chosen from a hostgroup
  • compression - if the value is greater than 0, new connections to that server will use compression
  • max_connections - the maximum number of SHOW CREATE TABLE mysql_replication_hostgroups\Gconnections ProxySQL will open to this backend server. Even though this server will have the highest weight, no new connections will be opened to it once this limit is hit. Please ensure that the backend is configured with a correct value of max_connections to avoid that ProxySQL will try to go beyond that limit
  • max_replication_lag - if greater than 0, ProxySQL will regularly monitor replication lag and if it goes beyond such threshold it will temporary shun the host until replication catch ups
  • use_ssl - if set to 1, connections to the backend will use SSL
  • max_latency_ms - ping time is regularly monitored. If a host has a ping time greater than max_latency_ms it is excluded from the connection pool (although the server stays ONLINE)
  • comment - text field that can be used for any purposed defined by the user. Could be a description of what the host stores, a reminder of when the host was added or disabled, or a JSON processed by some checker script.

 

 

mysql_replication_hostgroups

Table mysql_replication_hostgroups defines replication hostgroups for use with traditional master / slave ASYNC or SEMI-SYNC replication. In case Group Replication / InnoDB Cluster or Galera / Percona XtraDB Cluster is used for replication the mysql_group_replication_hostgroups or mysql_galera_hostgroups (available in version 2.x) should be used instead.
Admin> 
*************************** 1. row ***************************
       table: mysql_replication_hostgroups
Create Table: CREATE TABLE mysql_replication_hostgroups (
    writer_hostgroup INT CHECK (writer_hostgroup>=0) NOT NULL PRIMARY KEY,
    reader_hostgroup INT NOT NULL CHECK (reader_hostgroup<>writer_hostgroup AND reader_hostgroup>0),
    check_type VARCHAR CHECK (LOWER(check_type) IN ('read_only','innodb_read_only','super_read_only')) NOT NULL DEFAULT 'read_only',
    comment VARCHAR,
    UNIQUE (reader_hostgroup))
1 row in set (0.00 sec)
 
 
Each row in mysql_replication_hostgroups represent a pair of writer_hostgroup and reader_hostgroup . ProxySQL will monitor the value of read_only for all the servers in specified hostgroups, and based on the value of read_only will assign the server to the writer or reader hostgroups. The field comment can be used to store any arbitrary data.
The fields have the following semantics:
  • writer_hostgroup - the hostgroup that all traffic will be sent to by default, nodes that have read_only=0 in MySQL will be assigned to this hostgroup.
  • reader_hostgroup - the hostgroup that read traffic should be sent to, query rules or a separate read only user should be defined to route traffic to this hostgroup, nodes that have read_only=1 will be assigned to this hostgroup.
  • check_type - the MySQL variable checked when executing a Read Only check, read_only by default (alternatively super_read_only can be used as well). For AWS Aurora innodb_read_only should be used.
  • comment - text field that can be used for any purposed defined by the user. Could be a description of what the cluster stores, a reminder of when the hostgroup was added or disabled, or a JSON processed by some checker script.

 

 

mysql_group_replication_hostgroups

Table mysql_group_replication_hostgroups defines hostgroups for use with Oracle Group Replication / InnoDB Cluster
Admin> show create table mysql_group_replication_hostgroups\G
*************************** 1. row ***************************
       table: mysql_group_replication_hostgroups
Create Table: CREATE TABLE mysql_group_replication_hostgroups (
    writer_hostgroup INT CHECK (writer_hostgroup>=0) NOT NULL PRIMARY KEY,
    backup_writer_hostgroup INT CHECK (backup_writer_hostgroup>=0 AND backup_writer_hostgroup<>writer_hostgroup) NOT NULL,
    reader_hostgroup INT NOT NULL CHECK (reader_hostgroup<>writer_hostgroup AND backup_writer_hostgroup<>reader_hostgroup AND reader_hostgroup>0),
    offline_hostgroup INT NOT NULL CHECK (offline_hostgroup<>writer_hostgroup AND offline_hostgroup<>reader_hostgroup AND backup_writer_hostgroup<>offline_hostgroup AND offline_hostgroup>=0),
    active INT CHECK (active IN (0,1)) NOT NULL DEFAULT 1,
    max_writers INT NOT NULL CHECK (max_writers >= 0) DEFAULT 1,
    writer_is_also_reader INT CHECK (writer_is_also_reader IN (0,1)) NOT NULL DEFAULT 0,
    max_transactions_behind INT CHECK (max_transactions_behind>=0) NOT NULL DEFAULT 0,
    comment VARCHAR,
    UNIQUE (reader_hostgroup),
    UNIQUE (offline_hostgroup),
    UNIQUE (backup_writer_hostgroup))
1 row in set (0.00 sec)
 
 
The fields have the following semantics:
  • writer_hostgroup - the hostgroup that all traffic will be sent to by default, nodes that have read_only=0 in MySQL will be assigned to this hostgroup.
  • backup_writer_hostgroup - if the cluster has multiple nodes with read_only=0 and max_writers, ProxySQL will put the additional nodes (in excess of max_writes) in the backup_writer_hostgroup.
  • reader_hostgroup - the hostgroup that read traffic should be sent to, query rules or a separate read only user should be defined to route traffic to this hostgroup, nodes that have read_only=1 will be assigned to this hostgroup.
  • offline_hostgroup - when ProxySQL's monitoring determines a node is OFFLINE it will be put into the offline_hostgroup.
  • active - when enabled, ProxySQL monitors the hostgroups and moves nodes between the appropriate hostgroups.
  • max_writers' - this value determines the maximum number of nodes that should be allowed in the writer_hostgroup, nodes in excess of this value will be put into the backup_writer_hostgroup`
  • writer_is_also_reader - determines if a node should be added to the reader_hostgroup as well as the writer_hostgroup after it is promoted.
  • max_transactions_behind - determines the maximum number of transactions behind the writers that ProxySQL should allow before shunning the node to prevent stale reads (this is determined by querying the transactions_behind field of the sys.gr_member_routing_candidate_status table in MySQL).
  • comment - text field that can be used for any purposed defined by the user. Could be a description of what the cluster stores, a reminder of when the hostgroup was added or disabled, or a JSON processed by some checker script.

 

 

mysql_galera_hostgroups

Table mysql_galera_hostgroups (available in ProxySQL 2.x and higher) defines hostgroups for use with Galera Cluster / Percona XtraDB Cluster.
Admin> show create table mysql_galera_hostgroups\G
*************************** 1. row ***************************
       table: mysql_galera_hostgroups
Create Table: CREATE TABLE mysql_galera_hostgroups (
    writer_hostgroup INT CHECK (writer_hostgroup>=0) NOT NULL PRIMARY KEY,
    backup_writer_hostgroup INT CHECK (backup_writer_hostgroup>=0 AND backup_writer_hostgroup<>writer_hostgroup) NOT NULL,
    reader_hostgroup INT NOT NULL CHECK (reader_hostgroup<>writer_hostgroup AND backup_writer_hostgroup<>reader_hostgroup AND reader_hostgroup>0),
    offline_hostgroup INT NOT NULL CHECK (offline_hostgroup<>writer_hostgroup AND offline_hostgroup<>reader_hostgroup AND backup_writer_hostgroup<>offline_hostgroup AND offline_hostgroup>=0),
    active INT CHECK (active IN (0,1)) NOT NULL DEFAULT 1,
    max_writers INT NOT NULL CHECK (max_writers >= 0) DEFAULT 1,
    writer_is_also_reader INT CHECK (writer_is_also_reader IN (0,1)) NOT NULL DEFAULT 0,
    max_transactions_behind INT CHECK (max_transactions_behind>=0) NOT NULL DEFAULT 0,
    comment VARCHAR,
    UNIQUE (reader_hostgroup),
    UNIQUE (offline_hostgroup),
    UNIQUE (backup_writer_hostgroup))
1 row in set (0.00 sec)
 
 
The fields have the following semantics:
  • writer_hostgroup - the hostgroup that all traffic will be sent to by default, nodes that have read_only=0 in MySQL will be assigned to this hostgroup.
  • backup_writer_hostgroup - if the cluster has multiple nodes with read_only=0 and max_writers, ProxySQL will put the additional nodes (in excess of max_writes) in the backup_writer_hostgroup.
  • reader_hostgroup - the hostgroup that read traffic should be sent to, query rules or a separate read only user should be defined to route traffic to this hostgroup, nodes that have read_only=1 will be assigned to this hostgroup.
  • offline_hostgroup - when ProxySQL's monitoring determines a host is OFFLINE it will be put into the offline_hostgroup
  • active - when enabled, ProxySQL monitors the hostgroups and moves servers between the appropriate hostgroups.
  • max_writers' - this value determines the maximum number of nodes that should be allowed in the writer_hostgroup, nodes in excess of this value will be put into the backup_writer_hostgroup`
  • writer_is_also_reader - determines if a node should be added to the reader_hostgroup as well as the writer_hostgroup after it is promoted.
  • max_transactions_behind - determines the maximum number of writesets behind the cluster that ProxySQL should allow before shunning the node to prevent stale reads (this is determined by querying the wsrep_local_recv_queue Galera variable).
  • comment - text field that can be used for any purposed defined by the user. Could be a description of what the cluster stores, a reminder of when the hostgroup was added or disabled, or a JSON processed by some checker script.

 

 

mysql_users

Table mysql_users defines MySQL users, used to connect to backends.
Admin> SHOW CREATE TABLE mysql_users\G
*************************** 1. row ***************************
       table: mysql_users
Create Table: CREATE TABLE mysql_users (
    username VARCHAR NOT NULL,
    password VARCHAR,
    active INT CHECK (active IN (0,1)) NOT NULL DEFAULT 1,
    use_ssl INT CHECK (use_ssl IN (0,1)) NOT NULL DEFAULT 0,
    default_hostgroup INT NOT NULL DEFAULT 0,
    default_schema VARCHAR,
    schema_locked INT CHECK (schema_locked IN (0,1)) NOT NULL DEFAULT 0,
    transaction_persistent INT CHECK (transaction_persistent IN (0,1)) NOT NULL DEFAULT 0,
    fast_forward INT CHECK (fast_forward IN (0,1)) NOT NULL DEFAULT 0,
    backend INT CHECK (backend IN (0,1)) NOT NULL DEFAULT 1,
    frontend INT CHECK (frontend IN (0,1)) NOT NULL DEFAULT 1,
    max_connections INT CHECK (max_connections >=0) NOT NULL DEFAULT 10000,
    comment VARCHAR NOT NULL DEFAULT '',
    PRIMARY KEY (username, backend),
    UNIQUE (username, frontend))
1 row in set (0.00 sec)
 
 
The fields have the following semantics:
  • username, password - credentials for connecting to the mysqld or ProxySQL instance. See also Password management
  • active - the users with active = 0 will be tracked in the database, but will be never loaded in the in-memory data structures
  • default_hostgroup - if there is no matching rule for the queries send by this users, the traffic is generates is sent to the specified hostgroup
  • default_schema - the schema to which the connection should change by default
  • schema_locked - not supported yet (TODO: check)
  • transaction_persistent - if this is set for the user with which the MySQL client is connecting to ProxySQL (thus a "frontend" user - see below), transactions started within a hostgroup will remain within that hostgroup regardless of any other rules
  • fast_forward - if set it bypass the query processing layer (rewriting, caching) and pass through the query directly as is to the backend server.
  • frontend - if set to 1, this (username, password) pair is used for authenticating to the ProxySQL instance
  • backend - if set to 1, this (username, password) pair is used for authenticating to the mysqld servers against any hostgroup
  • max_connections - defines the maximum number of allowable frontend connections for a specific user.
  • comment - text field that can be used for any purposed defined by the user. Could be a description of what the cluster stores, a reminder of when the hostgroup was added or disabled, or a JSON processed by some checker script.
Note, currently all users need both "frontend" and "backend" set to 1 . Future versions of ProxySQL will separate the crendentials between frontend and backend. In this way frontend will never know the credential to connect directly to the backend, forcing all the connection through ProxySQL and increasing the security of the system.
Fast forward notes:
  • it doesn't require a different port : full features proxy logic and "fast forward" logic is implemented in the same code/module
  • fast forward is implemented on a per-user basis : depending from the user that connects to ProxySQL , fast forward is enabled or disabled
  • fast forward algorithm is enabled after authentication : the client still authenticates to ProxySQL, and ProxySQL will create a connection when the client will start sending traffic. This means that connections' errors are still handled during connect phase.
  • fast forward does NOT support SSL
  • if using compression, it must be enabled in both ends
Note: users in mysql_users shouldn't be used also for admin-admin_credentials and admin-stats_credentials

 

 

mysql_query_rules

Table mysql_query_rules defines routing policies and attributes.
Admin> SHOW CREATE TABLE mysql_query_rules\G
*************************** 1. row ***************************
       table: mysql_query_rules
Create Table: CREATE TABLE mysql_query_rules (
    rule_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    active INT CHECK (active IN (0,1)) NOT NULL DEFAULT 0,
    username VARCHAR,
    schemaname VARCHAR,
    flagIN INT NOT NULL DEFAULT 0,
    client_addr VARCHAR,
    proxy_addr VARCHAR,
    proxy_port INT,
    digest VARCHAR,
    match_digest VARCHAR,
    match_pattern VARCHAR,
    negate_match_pattern INT CHECK (negate_match_pattern IN (0,1)) NOT NULL DEFAULT 0,
    re_modifiers VARCHAR DEFAULT 'CASELESS',
    flagOUT INT,
    replace_pattern VARCHAR,
    destination_hostgroup INT DEFAULT NULL,
    cache_ttl INT CHECK(cache_ttl > 0),
    cache_empty_result INT CHECK (cache_empty_result IN (0,1)) DEFAULT NULL,
    reconnect INT CHECK (reconnect IN (0,1)) DEFAULT NULL,
    timeout INT UNSIGNED,
    retries INT CHECK (retries>=0 AND retries <=1000),
    delay INT UNSIGNED,
    next_query_flagIN INT UNSIGNED,
    mirror_flagOUT INT UNSIGNED,
    mirror_hostgroup INT UNSIGNED,
    error_msg VARCHAR,
    OK_msg VARCHAR,
    sticky_conn INT CHECK (sticky_conn IN (0,1)),
    multiplex INT CHECK (multiplex IN (0,1)),
    gtid_from_hostgroup INT UNSIGNED,
    log INT CHECK (log IN (0,1)),
    apply INT CHECK(apply IN (0,1)) NOT NULL DEFAULT 0,
    comment VARCHAR)
1 row in set (0.00 sec)
 
 
The fields have the following semantics:
  • rule_id - the unique id of the rule. Rules are processed in rule_id order
  • active - only rules with active=1 will be considered by the query processing module and only active rules are loaded into runtime.
  • username - filtering criteria matching username. If is non-NULL, a query will match only if the connection is made with the correct username
  • schemaname - filtering criteria matching schemaname. If is non-NULL, a query will match only if the connection uses schemaname as default schema (in mariadb/mysql schemaname is equivalent to databasename)
  • flagIN, flagOUT, apply - these allow us to create "chains of rules" that get applied one after the other. An input flag value is set to 0, and only rules with flagIN=0 are considered at the beginning. When a matching rule is found for a specific query, flagOUT is evaluated and if NOT NULL the query will be flagged with the specified flag in flagOUT. If flagOUT differs from flagIN , the query will exit the current chain and enters a new chain of rules having flagIN as the new input flag. If flagOUT matches flagIN, the query will be re-evaluate again against the first rule with said flagIN. This happens until there are no more matching rules, or apply is set to 1 (which means this is the last rule to be applied)
  • client_addr - match traffic from a specific source
  • proxy_addr - match incoming traffic on a specific local IP
  • proxy_port - match incoming traffic on a specific local port
  • digest - match queries with a specific digest, as returned by stats_mysql_query_digest.digest
  • match_digest - regular expression that matches the query digest. See also mysql-query_processor_regex
  • match_pattern - regular expression that matches the query text. See also mysql-query_processor_regex
  • negate_match_pattern - if this is set to 1, only queries not matching the query text will be considered as a match. This acts as a NOT operator in front of the regular expression matching against match_pattern or match_digest
  • re_modifiers - comma separated list of options to modify the behavior of the RE engine. With CASELESS the match is case insensitive. With GLOBAL the replace is global (replaces all matches and not just the first). For backward compatibility, only CASELESS is the enabled by default. See also mysql-query_processor_regex for more details.
  • replace_pattern - this is the pattern with which to replace the matched pattern. It's done using RE2::Replace, so it's worth taking a look at the online documentation for that: https://github.com/google/re2/blob/master/re2/re2.h#L378. Note that this is optional, and when this is missing, the query processor will only cache, route, or set other parameters without rewriting.
  • destination_hostgroup - route matched queries to this hostgroup. This happens unless there is a started transaction and the logged in user has the transaction_persistent flag set to 1 (see mysql_users table).
  • cache_ttl - the number of milliseconds for which to cache the result of the query. Note: in ProxySQL 1.1 cache_ttl was in seconds
  • cache_empty_result - controls if resultset without rows will be cached or not
  • reconnect - feature not used
  • timeout - the maximum timeout in milliseconds with which the matched or rewritten query should be executed. If a query run for longer than the specific threshold, the query is automatically killed. If timeout is not specified, global variable mysql-default_query_timeout applies
  • retries - the maximum number of times a query needs to be re-executed in case of detected failure during the execution of the query. If retries is not specified, global variable mysql-query_retries_on_failure applies
  • delay - number of milliseconds to delay the execution of the query. This is essentially a throttling mechanism and QoS, allowing to give priority to some queries instead of others. This value is added to the mysql-default_query_delay global variable that applies to all queries. Future version of ProxySQL will provide a more advanced throttling mechanism.
  • mirror_flagOUT and mirror_hostgroup - setting related to mirroring .
  • error_msg - query will be blocked, and the specified error_msg will be returned to the client
  • OK_msg - the specified message will be returned for a query that uses the defined rule
  • sticky_conn - not implemented yet
  • multiplex - If 0, multiplex will be disabled. If 1, multiplex could be re-enabled if there are is not any other conditions preventing this (like user variables or transactions). If 2, multiplexing is not disabled for just the current query. See wiki Default is NULL, thus not modifying multiplexing policies
  • gtid_from_hostgroup - defines which hostgroup should be used as the leader for GTID consistent reads (typically the defined WRITER hostgroup in a replication hostgroup pair)
  • log - this column can have three values: 1 - matched query will be recorded into the events log; 0 - matched query will not be recorded into the events log; NULL - matched query log attribute will remain value from the previous match(es). Executed query will be recorded to the events log if its log attribute is set to 1 when rule is applied (apply=1) or after processing all query rules and its log attribute is set to 1
  • apply - when set to 1 no further queries will be evaluated after this rule is matched and processed (note: mysql_query_rules_fast_routing rules will not be evaluated afterwards)
  • comment - free form text field, usable for a descriptive comment of the query rule
 
 
 
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章