mysql中间件ProxySQL

mysql实现读写分离的方式

mysql 实现读写分离的方式有以下几种:

1.程序修改mysql操作,直接和数据库通信,简单快捷的读写分离和随机的方式实现的负载均衡,权限独立分配,需要开发人员协助。
2.amoeba,直接实现读写分离和负载均衡,不用修改代码,有很灵活的数据解决方案,自己分配账户,和后端数据库权限管理独立,权限处理不够灵活。
3.mysql-proxy,直接实现读写分离和负载均衡,不用修改代码,master和slave用一样的帐号,效率低
4.mycat中间件
5.proxysql中间件(推荐使用)

ProxySQL简介

ProxySQL 是基于 MySQL 的一款开源的中间件的产品,是一个灵活的 MySQL 代理层,可以实现读写分离,支持 Query 路由功能,支持动态指定某个 SQL 进行缓存,支持动态加载(无需重启 ProxySQL 服务),故障切换和一些 SQL 的过滤功能。
ProxySQL 是用 C++ 语言开发的,虽然也是一个轻量级产品,但性能很好(据测试,能处理千亿级的数据),功能也足够,能满足中间件所需的绝大多数功能,包括:

1.最基本的读/写分离,且方式有多种
2.可定制基于用户、基于schema、基于语句的规则对SQL语句进行路由。换句话说,规则很灵活。基于schema和与语句级的规则,可以实现简单的sharding(分库分表)
3.可缓存查询结果。虽然ProxySQL的缓存策略比较简陋,但实现了基本的缓存功能,绝大多数时候也够用了。此外,作者已经打算实现更丰富的缓存策略
4.监控后端节点。ProxySQL可以监控后端节点的多个指标,包括:ProxySQL和后端的心跳信息,后端节点的read-only/read-write,slave和master的数据同步延迟性(replication lag)

ProxySQL安装

环境说明

主机名IP
localhost192.168.175.100
导入proxysql压缩包
[root@localhost ~]# ls
!  anaconda-ks.cfg  proxysql-2.0.13-1-centos7.x86_64.rpm

直接本地安装
[root@localhost ~]# yum -y localinstall proxysql-2.0.13-1-centos7.x86_64.rpm 

ProxySQL的Admin管理接口

当 ProxySQL 启动后,将监听两个端口:

1、admin管理接口,默认端口为6032。该端口用于查看、配置ProxySQL
2、接收SQL语句的接口,默认端口为6033,这个接口类似于MySQL的3306端口
在这里插入图片描述
ProxySQL的admin管理接口是一个使用MySQL协议的接口,所以,可以直接使用mysql客户端、navicat等工具去连接这个管理接口。
例如,使用mysql客户端去连接ProxySQL的管理接口。下面使用的是管理接口的默认用户名和密码admin:admin

此时需要安装mariadb
[root@localhost ~]# yum -y install mariadb
启动proxysql
[root@localhost ~]# systemctl restart proxysql
[root@localhost ~]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128             *:6032                        *:*                  
LISTEN     0      128             *:6033                        *:*                  
LISTEN     0      128             *:6033                        *:*                  
LISTEN     0      128             *:6033                        *:*                  
LISTEN     0      128             *:6033                        *:*                  
LISTEN     0      128             *:22                          *:*                  
LISTEN     0      100     127.0.0.1:25                          *:*                  
LISTEN     0      128            :::22                         :::*                  
LISTEN     0      100           ::1:25                         :::*            

登录mysql
[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.

MySQL [(none)]> 

由于ProxySQL的配置全部保存在几个自带的库中,所以通过管理接口,可以非常方便地通过发送一些SQL命令去修改ProxySQL的配置。ProxySQL会解析通过该接口发送的某些对ProxySQL有效的特定命令,并将其合理转换后发送给内嵌的SQLite3数据库引擎去运行。
查看库和库中的表

MySQL [(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)

MySQL [(none)]> 


MySQL [(none)]> show tables from disk;
+--------------------------------------------+
| tables                                     |
+--------------------------------------------+
| global_variables                           |
| mysql_aws_aurora_hostgroups                |
| mysql_collations                           |
| mysql_firewall_whitelist_rules             |
| mysql_firewall_whitelist_sqli_fingerprints |
| mysql_firewall_whitelist_users             |
| mysql_galera_hostgroups                    |
| mysql_group_replication_hostgroups         |
| mysql_query_rules                          |
| mysql_query_rules_fast_routing             |
| mysql_replication_hostgroups               |
| mysql_servers                              |
| mysql_users                                |
| proxysql_servers                           |
| restapi_routes                             |
| scheduler                                  |
+--------------------------------------------+
16 rows in set (0.00 sec)

MySQL [(none)]> 

ProxySQL的配置几乎都是通过管理接口来操作的,通过Admin管理接口,可以在线修改几乎所有的配置并使其生效。只有两个变量的配置是必须重启ProxySQL才能生效的,它们是:mysql-threads和mysql-stacksize
环境说明

主机IP
localhost192.168.175.100
yangcan1192.168.175.150
yangcan2安装mariadb
[root@yangcan1 ~]# yum -y install mariadb

另一个虚拟机去登录发现登录失败,因为admin只能本机登录
[root@yangcan1 ~]# mysql -uadmin -padmin -h127.0.0.1 -P6032
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)

上面登录失败,因为admin只能在本地登录,想添加用户需要用到admin-admin_credentials

一、admin-admin_credentials 变量控制的是admin管理接口的管理员账户。默认的管理员账户和密码为admin:admin,但是这个默认的用户只能在本地使用。如果想要远程连接到ProxySQL,例如用windows上的navicat连接Linux上的ProxySQL管理接口,必须自定义一个管理员账户。
添加管理员账户

MySQL [(none)]> select @@admin-admin_credentials;   查看管理员用户和密码
+---------------------------+
| @@admin-admin_credentials |
+---------------------------+
| admin:admin               |
+---------------------------+
1 row in set (0.00 sec)

添加管理员账号和密码
MySQL [(none)]> set admin-admin_credentials='admin:admin;yangcan:123456';
Query OK, 1 row affected (0.00 sec)
MySQL [(none)]>  select @@admin-admin_credentials;
+----------------------------+
| @@admin-admin_credentials  |
+----------------------------+
| admin:admin;yangcan:123456 |
+----------------------------+
1 row in set (0.00 sec)

此时光添加还不够,添加下面两行命令
MySQL [(none)]> load admin variables to runtime;     //使修改立即生效
Query OK, 0 rows affected (0.00 sec)

MySQL [(none)]> save admin variables to disk;      //使修改永久保存到磁盘
Query OK, 35 rows affected (0.01 sec)

修改完后去另一台虚拟机验证
[root@yangcan1 ~]# mysql -uyangcan -p123456 -h192.168.175.100 -P6032
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
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.

MySQL [(none)]> 

所有的配置操作都是在修改main库中对应的表
MySQL [(none)]> select * from global_variables where variable_name='admin-admin_credentials';
+-------------------------+----------------------------+
| variable_name           | variable_value             |
+-------------------------+----------------------------+
| admin-admin_credentials | admin:admin;yangcan:123456 |
+-------------------------+----------------------------+
1 row in set (0.01 sec)

admin管理接口的用户必须不能存在于mysql_users中,这是出于安全的考虑,防止通过admin管理接口用户猜出mysql_users中的用户

二、admin-stats_credentials该变量控制admin管理接口的普通用户,这个变量中的用户没有超级管理员权限,只能查看monitor库和main库中关于统计的数据,其它库都是不可见的,且没有任何写权限。默认的普通用户名和密码为stats:stats。

查看库
MySQL [(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)

MySQL [(none)]> use main;  使用表
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MySQL [main]> select @@admin-stats_credentials;    查看临时用户
+---------------------------+
| @@admin-stats_credentials |
+---------------------------+
| stats:stats               |
+---------------------------+
1 row in set (0.00 sec)

用临时用户登录
[root@localhost ~]# mysql -ustats -pstats -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.

MySQL [(none)]> show databases;    查看数据库发现跟admin的数据不一样
+-----+---------------+-------------------------------------+
| seq | name          | file                                |
+-----+---------------+-------------------------------------+
| 0   | main          |                                     |
| 2   | monitor       |                                     |
| 3   | stats_history | /var/lib/proxysql/proxysql_stats.db |
+-----+---------------+-------------------------------------+
3 rows in set (0.00 sec)
MySQL [(none)]> use main;    使用库
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MySQL [main]> show tables;    查看表发现这个用户只能看状态不能写
+--------------------------------------+
| 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.00 sec)

添加临时用户
MySQL [(none)]> set admin-stats_credentials='stats:stats;tom:123';
Query OK, 1 row affected (0.00 sec)

MySQL [(none)]> select @@admin-stats_credentials;   查看
+---------------------------+
| @@admin-stats_credentials |
+---------------------------+
| stats:stats;tom:123       |
+---------------------------+
1 row in set (0.00 sec)
此时还不能登录,还需两条命令
MySQL [(none)]> load admin variables to runtime;
Query OK, 0 rows affected (0.00 sec)      //使修改立即生效

MySQL [(none)]> save admin variables to disk;
Query OK, 35 rows affected (0.01 sec)     //使修改保存到磁盘中

另一个虚拟机登录验证,登录成功
[root@yangcan1 ~]# mysql -utom -p123 -h192.168.175.100 -P6032
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
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.

MySQL [(none)]> 
MySQL [(none)]> show tables from main;    查看main表中的内容
+--------------------------------------+
| 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.00 sec)

MySQL [(none)]> 

三、admin-mysql_ifaces 变量指定admin接口的监听地址,格式为冒号分隔的hostname:port列表。默认监听在 0.0.0.0:6032

MySQL [main]> select @@admin-mysql_ifaces;         查看端口号
+----------------------+
| @@admin-mysql_ifaces |
+----------------------+
| 0.0.0.0:6032         |
+----------------------+
1 row in set (0.00 sec)

修改端口号,把真实端口号隐藏起来
MySQL [main]> set admin-mysql_ifaces = '0.0.0.0:8888';
Query OK, 1 row affected (0.00 sec)

MySQL [main]> select @@admin-mysql_ifaces;    查看端口号
+----------------------+
| @@admin-mysql_ifaces |
+----------------------+
| 0.0.0.0:8888         |
+----------------------+
1 row in set (0.00 sec)
输入下面两条命令使之生效
MySQL [main]> load admin variables to runtime;     
Query OK, 0 rows affected (0.00 sec)

MySQL [main]> save admin variables to disk;
Query OK, 35 rows affected (0.00 sec)

查看端口号,已变成8888
[root@localhost ~]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128             *:6033                        *:*                  
LISTEN     0      128             *:6033                        *:*                  
LISTEN     0      128             *:6033                        *:*                  
LISTEN     0      128             *:6033                        *:*                  
LISTEN     0      128             *:22                          *:*                  
LISTEN     0      128             *:8888                        *:*                  
LISTEN     0      100     127.0.0.1:25                          *:*                  
LISTEN     0      128            :::22                         :::*                  
LISTEN     0      100           ::1:25                         :::*         

修改端口号后重新用原来的端口登录发现失败
[root@localhost ~]# mysql -uadmin -padmin -h127.0.0.1 -P6032
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)

此时需要用新端口登录
[root@localhost ~]# mysql -uadmin -padmin -h127.0.0.1 -P8888
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
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.

MySQL [(none)]> 

四、多层配置系统
使用ProxySQL的Admin管理接口连上ProxySQL,可查看ProxySQL拥有的库
查看数据库

MySQL [(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)
main库是ProxySQL最主要的库,是需要修改配置时使用的库,它其实是一个内存数据库系统。所以,修改main库中的配置后,必须将其持久化到disk上才能永久保存
disk库是磁盘数据库,该数据库结构和内存数据库完全一致。当持久化内存数据库中的配置时,其实就是写入到disk库中。磁盘数据库的默认路径为 $DATADIR/proxysql.db
查看main库
MySQL [(none)]> use main;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MySQL [main]> show tables;
+----------------------------------------------------+
| tables                                             |
+----------------------------------------------------+
| global_variables                                   |
| mysql_aws_aurora_hostgroups                        |
| mysql_collations                                   |
| mysql_firewall_whitelist_rules                     |
| mysql_firewall_whitelist_sqli_fingerprints         |
| mysql_firewall_whitelist_users                     |
| mysql_galera_hostgroups                            |
| mysql_group_replication_hostgroups                 |
| mysql_query_rules                                  |
| mysql_query_rules_fast_routing                     |
| mysql_replication_hostgroups                       |
| mysql_servers                                      |
| mysql_users                                        |
| 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)

MySQL [main]> 
查看disk库
MySQL [(none)]> use disk;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MySQL [disk]> show tables;
+----------------------------------------------------+
| tables                                             |
+----------------------------------------------------+
| global_variables                                   |
| mysql_aws_aurora_hostgroups                        |
| mysql_collations                                   |
| mysql_firewall_whitelist_rules                     |
| mysql_firewall_whitelist_sqli_fingerprints         |
| mysql_firewall_whitelist_users                     |
| mysql_galera_hostgroups                            |
| mysql_group_replication_hostgroups                 |
| mysql_query_rules                                  |
| mysql_query_rules_fast_routing                     |
| mysql_replication_hostgroups                       |
| mysql_servers                                      |
| mysql_users                                        |
| 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)

MySQL [disk]> 
此时发现两个库内容一致
stats库是统计信息库。这个库中的数据一般是在检索其内数据时临时填充的,它保存在内存中。因为没有相关的配置项,所以无需持久化
monitor库是监控后端MySQL节点相关的库,该库中只有几个log类的表,监控模块收集到的监控信息全都存放到对应的log表中(新版本没有log)
stats_history库是1.4.4版新增的库,用于存放历史统计数据。默认路径为 $DATADIR/proxysql_stats.db

ProxySQL 内部使用的是 SQLite3 数据库,无论是内存数据库还是磁盘数据库,都是通过SQLite3引 擎进行解析、操作的。它和 MySQL 的语法可能稍有不同,但ProxySQL会对不兼容的语法自动进行调整,最大程度上保证MySQL语句的有效率。
上面描述main库的时候,只是说了内存数据库需要持久化到disk库才能永久保存配置。但实际上,修改了main库中的配置后,并不会立即生效,它还需要load到runtime的数据结构中才生效,只有在runtime数据结构中的配置才是对ProxySQL当前有效的配置

ProxySQL多层配置系统

ProxySQL 的配置系统非常强大,它能在线修改几乎所有配置(仅有的两个需要重启才能生效的变量为 mysql-threads 和 mysql-stacksize ),并在线生效、持久化保存。这得益于它采用的多层配置系统。
多层配置系统结构如下:

  +-------------------------+
       |         RUNTIME         |
       +-------------------------+
              /|\          |
               |           |
           [1] |       [2] |
               |          \|/
       +-------------------------+
       |         MEMORY          |
       +-------------------------+ _
              /|\          |      |\
               |           |        \
           [3] |       [4] |         \ [5]
               |          \|/         \
       +-------------------------+  +---------------+
       |          DISK           |  |  CONFIG FILE  |
       +-------------------------+  +---------------+

最底层的是 disk 库和 config file 。这里需要注意,这里的 config file 就是传统的配置文件,默认为 /etc/proxysql.cnf , ProxySQL 启动时,主要是从 disk 库中读取配置加载到内存并最终加载到 runtime 生效,只有极少的几个特定配置内容是从 config file 中加载的,除非是第一次初始化 ProxySQL 运行环境(或者disk库为空)。

中间层的是 memory ,表示的是内存数据库,其实就是 main 库。通过管理接口修改的所有配置,都保存在内存数据库(main)中。当 ProxySQL 重启或者崩溃时,这个内存数据库中的数据会丢失,所以需要 save 到 disk 库中。

最上层的是 runtime ,它是 ProxySQL 有关线程运行时读取的数据结构。换句话说,该数据结构中的配置都是已生效的配置。所以,修改了 main 库中的配置后,必须 load 到 runtime 数据结构中才能使其生效。
在上面的多层配置系统图中,标注了[1]、[2]、[3]、[4]、[5]的序号。每个序号都有两个操作方向from/to,其实只是所站角度不同而已。以下是各序号对应的操作:

[1] :将内存数据库中的配置加载到RUNTIME数据结构中
        LOAD XXX FROM MEMORY
        LOAD XXX TO RUNTIME

[2] :将RUNTIME数据结构中的配置持久化到内存数据库中
        SAVE XXX FROM RUNTIME
        SAVE XXX TO MEMORY

[3] :将磁盘数据库中的配置加载到内存数据库中
        LOAD XXX FROM DISK
        LOAD XXX TO MEMORY

[4] :将内存数据库中的配置持久化到磁盘数据库中
        SAVE XXX FROM MEMORY
        SAVE XXX TO DISK

[5] :从传统配置文件中读取配置加载到内存数据库中
        LOAD XXX FROM CONFIG

DISK/MEMORY/RUNTIME/CONFIG 可以缩写,只要能识别即可。例如MEMORY可以缩写为MEM,runtime可以缩写为run

另外,上面的XXX是什么?这表示要加载/保存的是哪类配置。目前的ProxySQL支持以下几种:

mysql users
mysql servers
mysql variables
mysql query rules
admin variables
scheduler
proxysql_servers:目前ProxySQL集群功能还处于实验阶段,所以该类配置不应该去使用

从main库或者disk库就可以查看到

MySQL [(none)]> show tables from disk;
+------------------------------------+
| tables                             |
+------------------------------------+
| global_variables                   |  # (1)
| mysql_collations                   |  # (N)
| mysql_group_replication_hostgroups |  # (2)
| mysql_query_rules                  |  # (3)
| mysql_query_rules_fast_routing     |  # (4)
| mysql_replication_hostgroups       |  # (5)
| mysql_servers                      |  # (6)
| mysql_users                        |  # (7)
| proxysql_servers                   |  # (8)
| scheduler                          |  # (9)
+------------------------------------+
10 rows in set (0.00 sec)

对以上的解释
(1)中包含两类变量,以amdin-开头的表示admin variables,以mysql-开头的表示mysql variables。修改哪类变量,前文的XXX就代表哪类
(2,5,6)对应的都是mysql servers
(3,4)对应的是mysql query rules
(7)对应的mysql users
(9)对应的scheduler
(N)只是一张表,保存的是ProxySQL支持的字符集和排序规则,它是不用修改的
(8)是ProxySQL的集群配置表,该功能目前还处于实验阶段。如果想要配置该功能,则load/save proxysql_servers to/from ...

启动ProxySQL时如何加载配置

如果 ProxySQL 是刚安装的,或者磁盘数据库文件为空(甚至不存在),或者启动 ProxySQL 时使用了选项 --initial,这几种情况启动 ProxySQL 时,都会从传统配置文件 config file 中读取配置加载到内存数据库,并自动 load 到 runtime 数据结构、save到磁盘数据库,这是初始化 ProxySQL 运行环境的过程。

如果不是第一次启动 ProxySQL ,由于已经存在磁盘数据库文件,这时 ProxySQL 会从磁盘数据库中读取几乎所有的配置(即使传统配置文件中配置了某项,也不会去解析),但有3项是必须从传统配置文件中读取,它们分别是:

datadir:ProxySQL启动时,必须从配置文件中确定它的数据目录,因为磁盘数据库文件、日志以及其它一些文件是存放在数据目录下的。如果使用/etc/init.d/proxysql管理ProxySQL,则除了修改/etc/proxysql.cnf的datadir,还需要修改该脚本中的datadir。
restart_on_missing_heartbeats:MySQL线程丢失多少次心跳,就会杀掉这个线程并重启它。默认值为10。
execute_on_exit_failure:如果设置了该变量,ProxySQL父进程将在每次ProxySQL崩溃的时候执行已经定义好的脚本。建议使用它来生成一些崩溃时的警告和日志。注意,ProxySQL的重启速度可能只有几毫秒,因此很多其它的监控工具可能无法探测到ProxySQL的一次普通故障,此时可使用该变量

不同类型的读写分离方案解析

数据库中间件最基本的功能就是实现读写分离, ProxySQL 当然也支持。而且 ProxySQL 支持的路由规则非常灵活,不仅可以实现最简单的读写分离,还可以将读/写都分散到多个不同的组,以及实现分库 sharding (分表sharding的规则比较难写,但也能实现)。

本文只描述通过规则制定的语句级读写分离,不讨论通过 ip/port, client, username, schemaname 实现的读写分离。

下面描述了ProxySQL能实现的常见读写分离类型

最简单的读写分离

在这里插入图片描述
这种模式的读写分离,严格区分后端的master和slave节点,且slave节点必须设置选项read_only=1

在ProxySQL上,分两个组,一个写组HG=10,一个读组HG=20。同时在ProxySQL上开启monitor模块的read_only监控功能,让ProxySQL根据监控到的read_only值来自动调整节点放在HG=10(master会放进这个组)还是HG=20(slave会放进这个组)

这种模式的读写分离是最简单的,只需在mysql_users表中设置用户的默认路由组为写组HG=10,并在mysql_query_rules中加上两条简单的规则(一个select for update,一个select)即可

这种读写分离模式,在环境较小时能满足绝大多数需求。但是需求复杂、环境较大时,这种模式就太过死板,因为一切都是monitor模块控制的

多个读组或写组的分离模式

前面那种读写分离模式,是通过 monitor 模块监控 read_only 来调整的,所以每一个后端集群必须只能分为一个写组,一个读组。
但如果想要区分不同的 select ,并将不同的 select 路由到不同的节点上。例如有些查询语句的开销非常大,想让它们独占一个节点/组,其它查询共享一个节点/组,怎么实现?
在这里插入图片描述
看上去非常简单。但是却能适应各种需求。例如,后端做了分库,对某库的查询要路由到特定的主机组
至于各个主机组是同一个主从集群(下图左边),还是互相独立的主从集群环境(下图右边),要看具体的需求,不过这种读写分离模式都能应付
在这里插入图片描述

在实现这种模式时,前提是不能开启monitor模块的read_only监控功能,也不要设置mysql_replication_hostgroup 表

例如,下面的配置实现的是上图左边的结构:写请求路由给HG=10,对test1库的select语句路由给HG=20,其它select路由给HG=30

mysql_servers: 
+--------------+----------+------+--------+--------+
| hostgroup_id | hostname | port | status | weight |
+--------------+----------+------+--------+--------+
| 10           | host1(即IP地址)   | 3306 | ONLINE | 1      |
| 20           | host2    | 3306 | ONLINE | 1      |
| 30           | host3    | 3306 | ONLINE | 1      |
+--------------+----------+------+--------+--------+

mysql_users: 
+----------+-------------------+
| username | default_hostgroup |
+----------+-------------------+
| root     | 10                |
+----------+-------------------+

mysql_query_rules: 
+---------+-----------------------+----------------------+
| rule_id | destination_hostgroup | match_digest         |
+---------+-----------------------+----------------------+
| 1       | 10                    | ^SELECT.*FOR UPDATE$ |
| 2       | 20                    | ^SELECT.*test1\..*   |
| 3       | 30                    | ^SELECT              |
+---------+-----------------------+----------------------+

查看表结构方式

PRAGMA  table_info("表名");

ProxySQL实现读写分离示例

环境说明

名称IP应用
proxysql192.168.175.100proxysql
master192.168.175.150mysql
slave192.168.175.151mysql

准备工作
需要关闭防火墙,selinux,以及配置mysql主从
安装proxysql省略,可以参考上面安装步骤

由于以及安装proxysql,现在部署mysql主从

主安装mariadb
[root@master ~]# yum -y install mariadb*
启动
[root@master ~]# systemctl start mariadb
[root@master ~]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      50              *:3306                        *:*                  
LISTEN     0      128             *:22                          *:*                  
LISTEN     0      100     127.0.0.1:25                          *:*                  
LISTEN     0      128            :::22                         :::*                  
LISTEN     0      100           ::1:25                         :::*        

从安装mariadb
[root@slave ~]# yum -y install mariadb*
启动
[root@slave ~]# systemctl start mariadb
[root@slave ~]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      50              *:3306                        *:*                  
LISTEN     0      128             *:22                          *:*                  
LISTEN     0      100     127.0.0.1:25                          *:*                  
LISTEN     0      128            :::22                         :::*                  
LISTEN     0      100           ::1:25                         :::*                  

主库配置主从
[root@master ~]# mysql         //登录数据库
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.65-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> grant replication slave on *.* to 'repl'@'192.168.175.151' identified by 'repl123';       //给从库授权
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;                     //刷新权限表
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> 

主上编辑配置文件
[root@master ~]# vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

server-id = 10      //添加内容
log-bin = mysql_bin     //添加内容
重启数据库
[root@master ~]# systemctl restart mariadb

连数据库查看主的状态信息
[root@master ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.65-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql_bin.000001 |      245 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

从数据库修改配置文件
[root@slave ~]# vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

server-id = 20          //添加内容
relay-log =myrelay      //添加内容
重启
[root@slave ~]# systemctl restart mariadb

从上面同步主数据库
[root@slave ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.65-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> change master to master_host='192.168.175.150',master_user='repl',master_password='repl123',master_log_file='mysql_bin.000001',master_log_pos=245;
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.175.150
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql_bin.000001
          Read_Master_Log_Pos: 245
               Relay_Log_File: myrelay.000002
                Relay_Log_Pos: 529
        Relay_Master_Log_File: mysql_bin.000001
             Slave_IO_Running: Yes        //看到这两个yes即同步成功
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 245
              Relay_Log_Space: 815
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 10
1 row in set (0.00 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.01 sec)


验证主从是否一致
主创建tom库
MariaDB [(none)]> create database tom;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| tom                |
+--------------------+
5 rows in set (0.00 sec)
从上面查看是否一致
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| tom                |
+--------------------+
5 rows in set (0.00 sec)
同步成功

配置ProxySQL

mysql主库添加proxysql可以增删改查的账号
[root@master ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.65-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> grant all on *.* to 'proxysql'@'192.168.175.100' identified by 'pwproxysql';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> quit
Bye

从库可以查看到添加的内容
[root@slave ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 5.5.65-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show grants for 'proxysql'@'192.168.175.100';
+--------------------------------------------------------------------------------------------------------------------------------+
| Grants for proxysql@192.168.175.100                                                                                            |
+--------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'proxysql'@'192.168.175.100' IDENTIFIED BY PASSWORD '*BF27B4C7AAD278126E228AA8427806E870F64F39' |
+--------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

登录proxysql管理端

[root@proxysql ~]# export MYSQL_PS1="(\u@\h:\p) [\d]> "    //添加内容
[root@proxysql ~]# mysql -uadmin -padmin -h127.0.0.1 -P6032
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
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.

(admin@127.0.0.1:6032) [(none)]>     //添加的内容u对应用户、h对应本机、p对应端口号、d对应位置

查看数据库
[root@proxysql ~]# 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.

[(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)

数据库说明
main 内存配置数据库,表里存放后端db实例、用户验证、路由规则等信息。表名以 runtime开头的表示proxysql当前运行的配置内容,不能通过dml语句修改,只能修改对应的不以 runtime 开头的(在内存)里的表,然后 LOAD 使其生效, SAVE 使其存到硬盘以供下次重启加载
disk 是持久化到硬盘的配置,sqlite数据文件
stats 是proxysql运行抓取的统计信息,包括到后端各命令的执行次数、流量、processlist、查询种类汇总/执行时间等等
monitor 库存储 monitor 模块收集的信息,主要是对后端db的健康/延迟检查
stats_history 统计信息历史库

Proxysql管理端添加后端连接mysql主从数据库的配置

[(none)]> show tables from main;
+----------------------------------------------------+
| tables                                             |
+----------------------------------------------------+
| global_variables                                   |    
| mysql_aws_aurora_hostgroups                        |
| mysql_collations                                   |
| mysql_firewall_whitelist_rules                     |
| mysql_firewall_whitelist_sqli_fingerprints         |
| mysql_firewall_whitelist_users                     |
| mysql_galera_hostgroups                            |
| mysql_group_replication_hostgroups                 |
| mysql_query_rules                                  |
| mysql_query_rules_fast_routing                     |
| mysql_replication_hostgroups                       |
| mysql_servers                                      |
| mysql_users                                        |
| 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.01 sec)

[(none)]> 

runtime_ 开头的是运行时的配置,这些是不能修改的。要修改 ProxySQL 的配置,需要修改了非 runtime_ 表,修改后必须执行 LOAD … TO RUNTIME 才能加载到 RUNTIME 生效,执行 save … to disk 才能将配置持久化保存到磁盘

下面语句中没有先切换到 main 库也执行成功了,因为 ProxySQL 内部使用的 SQLite3 数据库引擎,和 MySQL 的解析方式是不一样的。即使执行了 USE main 语句也是无任何效果的,但不会报错

使用 insert 语句添加 mysql 主机到 mysql_servers 表中,其中:hostgroup_id 10 表示写组,20表示读组

proxysql上添加
[(none)]> insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(10,'192.168.175.150',3306,1,'Write Group');
Query OK, 1 row affected (0.00 sec)

[(none)]> insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(20,'192.168.175.151',3306,1,'Read Group');
Query OK, 1 row affected (0.00 sec)

[(none)]> select hostgroup_id,hostname,port,weight,comment from mysql_servers;               //查看
+--------------+-----------------+------+--------+-------------+
| hostgroup_id | hostname        | port | weight | comment     |
+--------------+-----------------+------+--------+-------------+
| 10           | 192.168.175.150 | 3306 | 1      | Write Group |
| 20           | 192.168.175.151 | 3306 | 1      | Read Group  |
+--------------+-----------------+------+--------+-------------+
2 rows in set (0.00 sec)

修改后,需要加载到RUNTIME,并保存到disk

[(none)]> load mysql servers to runtime;
Query OK, 0 rows affected (0.01 sec)

[(none)]> save mysql servers to disk;
Query OK, 0 rows affected (0.02 sec)

在 proxysql 主机的 mysql_users 表中添加刚才在 master 上创建的账号 proxysql,proxysql 客户端需要使用这个账号来访问数据库
default_hostgroup 默认组设置为写组,也就是10;
当读写分离的路由规则不符合时,会访问默认组的数据库;

[(none)]> insert into   mysql_users(username,password,default_hostgroup,transaction_persistent) values('proxysql','pwproxysql',10,1);       //添加用户
Query OK, 1 row affected (0.00 sec)

[(none)]> load mysql users to run;
Query OK, 0 rows affected (0.00 sec)

[(none)]> save mysql users to disk;
Query OK, 0 rows affected (0.01 sec)

查看
[(none)]> select * from mysql_users \G
*************************** 1. row ***************************
              username: proxysql
              password: pwproxysql
                active: 1
               use_ssl: 0
     default_hostgroup: 10
        default_schema: NULL
         schema_locked: 0
transaction_persistent: 1
          fast_forward: 0
               backend: 1
              frontend: 1
       max_connections: 10000
               comment: 
1 row in set (0.00 sec)

添加健康检测的帐号

在mysql的 master 端添加属于proxysql的只读账号
[root@master ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 73
Server version: 5.5.65-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> grant select on *.* to 'monitor'@'192.168.175.0/24' identified by 'monitor';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

在proxysql主机端修改变量设置健康检测的账号

proxysql上面设置
[(none)]> set mysql-monitor_username = 'monitor';
Query OK, 1 row affected (0.00 sec)

[(none)]> set mysql-monitor_password = 'monitor';
Query OK, 1 row affected (0.00 sec)

[(none)]> load mysql variables to run;
Query OK, 0 rows affected (0.00 sec)

[(none)]> save mysql variables to disk;
Query OK, 134 rows affected (0.01 sec)

添加读写分离的路由规则
需求:

将 select 查询语句全部路由至 hostgroup_id=20 的组(也就是读组)
但是 select * from tb for update 这样的语句是会修改数据的,所以需要单独定义,将它路由至 hostgroup_id=10 的组(也就是写组)
其他没有被规则匹配到的组将会被路由至用户默认的组(mysql_users 表中的 default_hostgroup)

配置读写分离
[(none)]> insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply) values(1,1,'^SELECT.*FOR UPDATE$',10,1);
Query OK, 1 row affected (0.00 sec)

[(none)]> insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply) values(30,1,'^SELECT',20,1);        //此时rule_id用的30代表至少可以插入30个值
Query OK, 1 row affected (0.00 sec)

[(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$ | 10                    | 1     |
| 30      | 1      | ^SELECT              | 20                    | 1     |
+---------+--------+----------------------+-----------------------+-------+
2 rows in set (0.00 sec)

[(none)]> load mysql query rules to run;
Query OK, 0 rows affected (0.00 sec)

[(none)]> save mysql query rules to disk;
Query OK, 0 rows affected (0.02 sec)

验证读写分离

登录用户是刚才我们在 mysql_user 表中创建的用户,端口为6033
[root@proxysql ~]# mysql -uproxysql -ppwproxysql -h127.0.0.1 -P6033
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
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.

MySQL [(none)]> show databases;       //查看库
ERROR 1045 (28000): Access denied for user 'proxysql'@'192.168.175.100' (using password: YES)
MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| tom                |
+--------------------+
5 rows in set (0.00 sec)
MySQL [(none)]> create database school;    //创建库
Query OK, 1 row affected (0.00 sec)

MySQL [(none)]> show databases;      //查看
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| test               |
| tom                |
+--------------------+
6 rows in set (0.00 sec)

MySQL [(none)]> select * from mysql.user\G     //查看
*************************** 1. row ***************************
                  Host: localhost
                  User: root
              Password: 
           Select_priv: Y
           Insert_priv: Y
           Update_priv: Y
           Delete_priv: Y
           Create_priv: Y
             Drop_priv: Y
           Reload_priv: Y
         Shutdown_priv: Y
          Process_priv: Y
             File_priv: Y
            Grant_priv: Y
       References_priv: Y
            Index_priv: Y
            Alter_priv: Y
          Show_db_priv: Y
         省略。。。。。。。

进行一系列命令去6032端口上验证
[(none)]> select * from stats_mysql_query_digest\G;
*************************** 1. row ***************************
        hostgroup: 10
       schemaname: information_schema
         username: proxysql
   client_address: 
           digest: 0x111A3F59AE71B7A2
      digest_text: create database school
       count_star: 1
       first_seen: 1597415316
        last_seen: 1597415316
         sum_time: 1385
         min_time: 1385
         max_time: 1385
sum_rows_affected: 1
    sum_rows_sent: 0
*************************** 2. row ***************************
        hostgroup: 20
       schemaname: information_schema
         username: proxysql
   client_address: 
           digest: 0x40B75DE8A4AD05EE
      digest_text: select * from mysql.user
       count_star: 1
       first_seen: 1597415372
        last_seen: 1597415372
         sum_time: 1960
         min_time: 1960
         max_time: 1960
sum_rows_affected: 0
    sum_rows_sent: 8
*************************** 3. row ***************************
        hostgroup: 10
       schemaname: information_schema
         username: proxysql
   client_address: 
           digest: 0x02033E45904D3DF0
      digest_text: show databases
       count_star: 5
       first_seen: 1597414457
        last_seen: 1597415330
         sum_time: 37937
         min_time: 612
         max_time: 13712
sum_rows_affected: 0
    sum_rows_sent: 11
*************************** 4. row ***************************
        hostgroup: 10
       schemaname: information_schema
         username: proxysql
   client_address: 
           digest: 0x226CD90D52A2BA0B
      digest_text: select @@version_comment limit ?
       count_star: 2
       first_seen: 1597414429
        last_seen: 1597414551
         sum_time: 0
         min_time: 0
         max_time: 0
sum_rows_affected: 0
    sum_rows_sent: 0
4 rows in set (0.01 sec)

ERROR: No query specified

[(none)]> 

以上数据发现show被默认分到10写的上面,此时是因为20读的操作只有^select而10写的操作只有^select.*for update$,所有需要在6032上添加show到读里面
[(none)]> insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply) values(2,1,"^SHOW",20,1);
Query OK, 1 row affected (0.00 sec)

[(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$ | 10                    | 1     |
| 2       | 1      | ^SHOW                | 20                    | 1     |
| 30      | 1      | ^SELECT              | 20                    | 1     |
+---------+--------+----------------------+-----------------------+-------+
3 rows in set (0.00 sec)
 [(none)]> load mysql query rules to run;
Query OK, 0 rows affected (0.00 sec)

[(none)]> save mysql query rules to disk;
Query OK, 0 rows affected (0.01 sec)

MySQL [(none)]> load admin variables to run;
Query OK, 0 rows affected (0.00 sec)

MySQL [(none)]> save admin variables to disk;
Query OK, 35 rows affected (0.01 sec)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值