MySQL-ProxySQL中间件Admin Schemas介绍

目录

    mysql-proxysql中间件(一)| proxysql基本概念: https://www.cnblogs.com/sqlserver2012/p/10972593.html

    mysql-proxysql中间件(二)| admin schemas介绍:https://www.cnblogs.com/sqlserver2012/p/10972761.html

正文

    这次我们主要来介绍下proxysql中核心数据库main的表结构及mgr监控配置脚本(内附彩蛋:mysql 8.0 mgr 监控脚本)

    计划其余数据库,随着功能介绍再做详细说明,本文不做过多扩展。

  the admin schemas

    proxysql管理界面是一个使用mysql协议的界面,任何能够通过这种界面发送命令的客户端都很容易配置它。 

    proxysql解析通过此接口发送的查询以查找特定于proxysql的任何命令,如果适当,则将它们发送到嵌入式sqlite3引擎以运行查询。

    请注意,sqlite3和mysql使用的sql语法不同,因此并非所有适用于mysql的命令都适用于sqlite3。 例如,尽管管理界面接受use命令,但它不会更改默认架构,因为sqlite3中不提供此功能。

        连接到proxysql管理界面时,我们可以看到有一些数据库可用。 proxysql将show databases命令转换为sqlite3的等效命令。

 

 

这些数据库的作用如下:

main:内存配置数据库 。
使用此数据库,可以轻松地以自动方式查询和更新proxysql的配置。

使用load mysql users from memory和类似命令,存储在此处的配置可以在运行时传播到proxysql使用的内存数据结构。

disk:基于磁盘的“main”镜像。
在重新启动时,“main”不会持久存在,并且可以从“磁盘”数据库或配置文件中加载,具体取决于启动标志和磁盘数据库的存在。

stats:包含从代理的内部功能收集的运行时指标。 
示例度量标准包括每个查询规则匹配的次数,当前运行的查询等。

monitor:包含与proxysql连接的后端服务器相关的监控指标。 
示例度量标准包括连接到后端服务器或对其进行ping操作的最短和最长时间。

myhgm:仅在调试版本中启用
此外,使用这两种类型的用户使用这些默认凭据访问管理数据库:

user:admin / password:admin - 具有对所有表的读写访问权限

user:stats / password:stats - 具有对统计表的只读访问权限。 这用于从proxysql中提取指标,而不会暴露太多的数据库

上述的访问凭据,可通过变量admin-admin_credentials和admin-stats_credentials进行配置。

main数据库

    主要包含了以下数据表:

    

核心配置表

1.    mysql_server

    

 

  字段定义    

hostgroup_id

    包含此mysqld实例的主机组。 请注意,同一实例可以是多个主机组的一部分

hostname,port

    可以联系mysqld实例的tcp端点

gtid_port                

    proxysql binlog reader侦听gtid跟踪的后端服务器端口

status

    online - 后端服务器完全可以运行

    shunned - 后端服务器暂时停止使用,因为在时间太短或复制延迟超过允许阈值的情况下连接错误太多

    offline_soft - 当服务器进入offline_soft模式时,不再接受新的传入连接,而现有连接将保持不变,直到它们变为非活动状态。 换句话说,连接一直在使用,直到当前事务完成。 这允许优雅地分离后端

    offline_hard - 当服务器进入offline_hard模式时,现有连接被丢弃,而新的传入连接也不被接受。 这相当于从主机组中删除服务器,或暂时将其从主机组中取出以进行维护工作

weight

    服务器相对于其他权重的权重越大,从主机组中选择服务器的概率就越高

compression

    如果该值大于0,则与该服务器的新连接将使用压缩

max_connections

    proxysql将向此后端服务器打开的最大连接数。 即使此服务器具有最高权重,但一旦达到此限制,就不会向其打开新连接。 请确保后端配置了正确的max_connections值,以避免proxysql尝试超出该限制

max_replication_lag

    如果更大且为0,proxysql将定期监视复制延迟,如果超出此阈值,它将暂时避开主机,直到复制赶上

use_ssl

    如果设置为1,则与后端的连接将使用ssl

max_latency_ms

    定期监视ping时间。 如果主机的ping时间大于max_latency_ms,则它将从连接池中排除(尽管服务器保持online状态)

comment

    可用于用户定义的任何目的的文本字段。 可以是主机存储内容的描述,添加或禁用主机的提醒,或某些检查器脚本处理的json。

2.   mysql_replication_hostgroups

    表mysql_replication_hostgroups定义用于传统主/从异步或者半同步或者增强半同步复制的复制主机组。 

    如果使用group replication / innodb cluster或galera / percona xtradb cluster进行复制,则应使用mysql_group_replication_hostgroups或mysql_galera_hostgroups(在2.x版中提供)。

    mysql_replication_hostgroups中的每一行代表一对writer_hostgroup和reader_hostgroup。 

    proxysql将监视指定主机组中所有服务器的read_only值,并根据read_only的值将服务器分配给writer组或reader组。 

    字段的注释可用于存储任意数据。

  字段定义   

writer_hostgroup - 默认情况下将发送所有请求的主机组,mysql中read_only = 0的节点将分配给该主机组。

reader_hostgroup - 应该发送读取请求的主机组,应该定义查询规则或单独的只读用户将流量路由到此主机组,将read_only = 1的节点分配给该主机组。

check_type - 执行只读检查时检查的mysql变量,默认情况下为read_only(也可以使用super_read_only)。 对于aws aurora,应使用innodb_read_only。

comment - 可用于用户定义的任何目的的文本字段。 可以是集群存储内容的描述,添加或禁用主机组的提醒,或某些检查器脚本处理的json。

3.    mysql_group_replication_hostgroups(mgr)

 

    字段定义

writer_hostgroup - 默认情况下将发送所有请求的主机组,mysql中read_only = 0的节点将分配给该主机组。

backup_writer_hostgroup - 如果集群有多个节点,其read_only = 0和max_writers,则proxysql会将其他节点(超过max_writes的节点)放入backup_writer_hostgroup中。

reader_hostgroup - 应该发送读请求的主机组,将read_only = 1的节点分配给该主机组。

offline_hostgroup - 当proxysql的监视确定节点为offline时,它将被放入offline_hostgroup。

active - 启用时,proxysql监视主机组并在适当的主机组之间移动节点。

max_writers' - 此值确定writer_hostgroup中应允许的最大节点数,超过此值的节点将放入backup_writer_hostgroup中

writer_is_also_reader - 确定是否应将同一个节点添加到reader_hostgroup和writer_hostgroup中。

max_transactions_behind - 确定在屏蔽节点之前,proxysql应允许的写入器后面的最大事务数,以防止读取落后过多(这是通过查询mysql中sys.gr_member_routing_candidate_status表的transactions_behind字段来确定的)。

comment - 可用于用户定义的任何目的的文本字段。 可以是集群存储内容的描述,添加或禁用主机组的提醒,或某些检查器脚本处理的json。

4.    mysql_galera_hostgroups(pxc)

    表mysql_galera_hostgroups(在proxysql 2.x及更高版本中可用)定义了用于galera cluster / percona xtradb cluster的主机组。

    字段定义

writer_hostgroup - 默认情况下将发送所有流量的主机组,mysql中read_only = 0的节点将分配给该主机组。

backup_writer_hostgroup - 如果集群有多个节点,其read_only = 0和max_writers,则proxysql会将其他节点(超过max_writes)放入backup_writer_hostgroup中。

reader_hostgroup - 应该发送读取流量的主机组,应该定义查询规则或单独的只读用户将流量路由到此主机组,将read_only = 1的节点分配给该主机组。

offline_hostgroup - 当proxysql的监控确定主机处于offline时,它将被放入offline_hostgroup

active - 启用时,proxysql监视主机组并在适当的主机组之间移动服务器。

max_writers - 此值确定writer_hostgroup中应允许的最大节点数,超过此值的节点将放入backup_writer_hostgroup中

writer_is_also_reader - 确定是否应将节点添加到reader_hostgroup以及在提升后的writer_hostgroup。

max_transactions_behind - 确定在避开节点之前proxysql应允许的集群后面的最大写集数,以防止过时读取(这是通过查询wsrep_local_recv_queue galera变量确定的)。

comment - 可用于用户定义的任何目的的文本字段。 可以是集群存储内容的描述,添加或禁用主机组的提醒,或某些检查器脚本处理的json。

5.    mysql_users

    表mysql_users定义mysql用户,用于连接后端。

 

    字段定义

username,password - 用于连接mysqld或proxysql实例的凭据。

active - 将在数据库中跟踪active = 0的用户,但永远不会在内存数据结构中加载

default_hostgroup - 如果此用户发送的查询没有匹配规则,则生成的流量将发送到指定的主机组

default_schema - 默认情况下连接应更改的架构

schema_locked - 尚不支持(todo:check)

transaction_persistent - 如果是一个事务内的多条sql,只会路由到一个主机组中。

fast_forward - 如果设置,它绕过查询处理层(重写,缓存)并直接将查询传递给后端服务器。

frontend - 如果设置为1,则此(用户名,密码)对用于对proxysql实例进行身份验证

backend - 如果设置为1,则此(用户名,密码)对用于针对任何主机组对mysqld服务器进行身份验证

max_connections - 定义特定用户的最大允许前端连接数。

comment - 可用于用户定义的任何目的的文本字段。 可以是集群存储内容的描述,添加或禁用主机组的提醒,或某些检查器脚本处理的json。

    注意,目前所有用户都需要将“frontend”和“后backend”都设置为1。 

    proxysql的未来版本将在前端和后端之间分离证书。 

    通过这种方式,前端永远不会知道直接连接到后端的凭证,强制通过proxysql进行所有连接并提高系统的安全性。

    fast_forward 注意事项:

1.    它不需要不同的端口:完整的功能代理逻辑和“快进”逻辑在同一代码/模块中实现

2.    fast_forward是基于每个用户实现的:取决于连接到proxysql的用户,启用或禁用fast_forward

3.    验证后启用fast_forward算法:客户端仍然对proxysql进行身份验证,当客户端开始发送流量时,proxysql将创建连接。 这意味着在连接阶段仍然会处理连接错误。

4.    fast_forward不支持ssl

5.    如果使用压缩,则必须在两端启用它

注意:mysql_users中的用户也不应该与admin-admin_credentials和admin-stats_credentials里的配置相同

6.    mysql_query_rules

    表mysql_query_rules定义了路由策略和属性。

 

    字段定义

rule_id - 规则的唯一id。 规则以rule_id顺序处理

 

active - 查询处理模块将仅考虑active = 1的规则,并且只将活动规则加载到运行时。

 

username - 匹配用户名的过滤条件。 如果为非null,则仅当使用正确的用户名建立连接时,查询才会匹配

 

schemaname - 匹配schemaname的过滤条件。 如果为非null,则仅当连接使用schemaname作为默认模式时,查询才会匹配(在mariadb / mysql schemaname中等效于databasename)

flagin,flagout,apply -

这些允许我们创建一个接一个地应用的“规则链”。 
输入标志值设置为0,并且在开始时仅考虑flagin = 0的规则。 
当为特定查询找到匹配规则时,将评估flagout,如果为not null,则将使用flagout中的指定标志标记查询。 
如果flagout与flagin不同,则查询将退出当前链并输入一个新的规则链,其中flagin作为新的输入标志。 
如果flagout与flagin匹配,则将针对具有所述flagin的第一个规则再次重新评估查询。 
这种情况会发生,直到没有更多匹配规则,或者apply设置为1(这意味着这是要应用的最后一条规则)


client_addr - 匹配来自特定源的流量

 

proxy_addr - 匹配特定本地ip上的传入流量

 

proxy_port - 匹配特定本地端口上的传入流量

使用stats_mysql_query_digest.digest返回的特定摘要匹配查询

 

match_digest - 与查询摘要匹配的正则表达式。 另请参见https://github.com/sysown/proxysql/wiki/global-variables#mysql-query_processor_regex

 

match_pattern - 与查询文本匹配的正则表达式。 另请参见https://github.com/sysown/proxysql/wiki/global-variables#mysql-query_processor_regex

 

negate_match_pattern - 如果将其设置为1,则只有与查询文本不匹配的查询才会被视为匹配项。 这在与match_pattern或match_digest匹配的正则表达式前面充当not运算符

 

re_modifiers -

用逗号分隔的选项列表,用于修改re引擎的行为。 使用caseless,匹配不区分大小写。

使用global,替换是全局的(替换所有匹配而不仅仅是第一个匹配)。

为了向后兼容,默认情况下仅启用caseless。 有关更多详细信息,另请参见https://github.com/sysown/proxysql/wiki/global-variables#mysql-query_processor_regex。

 

replace_pattern -

这是用于替换匹配模式的模式。

它是使用re2 :: replace完成的,因此值得一看有关的在线文档:https://github.com/google/re2/blob/master/re2/re2.h#l378。

请注意,这是可选的,当缺少此选项时,查询处理器将仅缓存,路由或设置其他参数而不重写。

 

destination_hostgroup -

将匹配的查询路由到此主机组。

除非存在已启动的事务且登录用户将transaction_persistent标志设置为1(请参阅mysql_users表),否则会发生这种情况。

 

cache_ttl - 缓存查询结果的毫秒数。 注意:在proxysql 1.1中,cache_ttl只需几秒钟

 

cache_empty_result -

控制是否缓存没有行的结果集

重新连接 - 未使用的功能

 

timeout -

应执行匹配或重写查询的最大超时(以毫秒为单位)。

如果查询运行的时间超过特定阈值,则会自动终止查询。 如果未指定timeout,则应用全局变量mysql-default_query_timeout

 

retries - 在执行查询期间检测到失败时需要重新执行查询的最大次数。 如果未指定重试,则应用全局变量mysql-query_retries_on_failure

delay -

延迟执行查询的毫秒数。

这本质上是一种限制机制和qos,允许优先考虑某些查询而不是其他查询。

此值将添加到适用于所有查询的mysql-default_query_delay全局变量中。 未来版本的proxysql将提供更高级的限制机制。

 

mirror_flagout和mirror_hostgroup - 与镜像相关的设置 https://github.com/sysown/proxysql/wiki/mirroring。

 

error_msg - 将阻止查询,并将指定的error_msg返回给客户端

 

ok_msg - 将为使用已定义规则的查询返回指定的消息

 

sticky_conn - 尚未实现

 

multiplex -

如果为0,则禁用multiplex。

如果为1,如果没有任何其他条件阻止此操作(如用户变量或事务),则可以重新启用multiplex。

如果为2,则不会仅针对当前查询禁用多路复用。 请参阅wiki默认为null,因此不会修改多路复用策略

gtid_from_hostgroup - 定义哪个主机组应该用作gtid一致性读取的领导者(通常是复制主机组对中定义的writer主机组)

 

log- 将记录查询

 

apply - 当设置为1时,在匹配和处理此规则后,将不再评估进一步的查询(注意:之后不会评估mysql_query_rules_fast_routing规则)

 

comment- 自由格式文本字段,可用于查询规则的描述性注释

 

7.    mysql_query_rules_fast_routing

    表mysql_query_rules_fast_routing是mysql_query_rules的扩展,之后会对快速路由策略和属性进行评估(仅在proxysql 1.4.7+中可用)。

 

    字段定义

username - 与用户名匹配的过滤条件,只有在使用正确的用户名建立连接时,查询才会匹配

schemaname - 匹配schemaname的过滤条件,只有当连接使用schemaname作为默认模式时,查询才会匹配(在mariadb / mysql schemaname中,这相当于databasename)

flagin - 与mysql_query_rules中flagin相同,并与mysql_query_rules表中指定的flagout / apply相关联

destination_hostgroup - 将匹配的查询路由到此主机组。 

comment- 自由格式文本字段,可用于查询规则的描述性注释

8.    global_variables\mysql_collations\scheduler 暂不介绍了,后面会逐步投在功能介绍里提及

 

runtime层对应表

        表以runtime_开头,其余基本与main库中的表名相同,例如:runtime_mysql_servers 是 内存层 mysql_servers表的对应表

    请注意,如果proxysql重新启动,如果内容未保存在磁盘数据库中,则内存表(main数据库)的所有内容都将丢失。

disk层对应表

        “disk”数据库与“main”数据库具有完全相同的表,具有相同的语义。 

        唯一的主要区别是这些表存储在磁盘上,而不是存储在内存中。 

        每当重新启动proxysql时,将从此数据库开始填充内存中的“main”数据库。

监控mgr,需要在mysql实例中配置一些监控脚本(mysql 5.7 和 mysql 8.0略有不同)

    该脚本需要配置到sys库下,因笔记web显示问题,无法显示折行,但是不影响复制,可以自行复制粘贴出来即可。

    先看看执行效果 

?

1
2
3
4
5
6
7
8
9
10
11
select from sys.gr_member_routing_candidate_status;
+ ------------------+-----------+---------------------+----------------------+
| viable_candidate | read_only | transactions_behind | transactions_to_cert |
+ ------------------+-----------+---------------------+----------------------+
| yes              |  no        |                   0 |                    0 |
+ ------------------+-----------+---------------------+----------------------+
viable_candidate:mgr当前节点是否正常
read_only:当前节点是否开启了只读
transactions_behind:mgr应用relay log的队列中,积压事务数
transactions_to_cert:mgr当前节点的验证队列,积压事务数

    mysql5.7,配置脚本为:

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use sys;
delimiter $$
create function ifzero(a  int , b  int returns int deterministic  return if(a = 0, b, a)$$
create function locate2(needle text(10000), haystack text(10000), offset  int )
returns int deterministic  return ifzero(locate(needle, haystack, offset), length(haystack) + 1)$$
create function gtid_normalize(g text(10000))
returns text(10000) deterministic  return gtid_subtract(g,  '' )$$
create function gtid_count(gtid_set text(10000))
returns int deterministic  begin declare result  bigint default 0;  declare colon_pos  int ;
declare next_dash_pos  int declare next_colon_pos  int declare next_comma_pos  int ;
set gtid_set = gtid_normalize(gtid_set);  set colon_pos = locate2( ':' , gtid_set, 1);
while colon_pos != length(gtid_set) + 1 do  set next_dash_pos = locate2( '-' , gtid_set, colon_pos + 1);
set next_colon_pos = locate2( ':' , gtid_set, colon_pos + 1);  set next_comma_pos = locate2( ',' , gtid_set, colon_pos + 1);
if next_dash_pos < next_colon_pos  and next_dash_pos < next_comma_pos
then set result = result + substr(gtid_set, next_dash_pos + 1,
least(next_colon_pos, next_comma_pos) - (next_dash_pos + 1)) - substr(gtid_set, colon_pos + 1, next_dash_pos - (colon_pos + 1)) + 1;
else set result = result + 1;  end if;
set colon_pos = next_colon_pos;
end while;  return result;
end $$
create function gr_applier_queue_length()
returns int deterministic
begin return
( select sys.gtid_count( gtid_subtract( ( select received_transaction_set
                                         from performance_schema.replication_connection_status
                                         where channel_name =  'group_replication_applier' ), ( select @@ global .gtid_executed) )));
end $$
create function gr_member_in_primary_partition()
returns varchar (3) deterministic  begin return ( select if( member_state= 'online' and ((
     select count ()  from performance_schema.replication_group_members  where member_state !=  'online' ) >= ((
         select count ()  from performance_schema.replication_group_members)/2) = 0),  'yes' 'no' )
                                                from performance_schema.replication_group_members
                                                join performance_schema.replication_group_member_stats using(member_id));
end $$
create view gr_member_routing_candidate_status  as
     select
         sys.gr_member_in_primary_partition()  as viable_candidate,
         if(( select
                     ( select
                                 group_concat(variable_value)
                             from
                                 performance_schema.global_variables
                             where
                                 variable_name  in ( 'read_only' 'super_read_only' )) !=  'off,off'
                 ),
             'yes' ,
             'no' as read_only,
         sys.gr_applier_queue_length()  as transactions_behind,
         count_transactions_in_queue  as 'transactions_to_cert'
     from
         performance_schema.replication_group_member_stats$$
delimiter ;

    mysql8.0,配置脚本为:

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use sys;
delimiter $$
create function ifzero(a  int , b  int )
returns int
deterministic
return if(a = 0, b, a)$$
create function locate2(needle text(10000), haystack text(10000), offset  int )
returns int
deterministic
return ifzero(locate(needle, haystack, offset), length(haystack) + 1)$$
create function gtid_normalize(g text(10000))
returns text(10000)
deterministic
return gtid_subtract(g,  '' )$$
create function gtid_count(gtid_set text(10000))
returns int
deterministic
begin
   declare result  bigint default 0;
   declare colon_pos  int ;
   declare next_dash_pos  int ;
   declare next_colon_pos  int ;
   declare next_comma_pos  int ;
   set gtid_set = gtid_normalize(gtid_set);
   set colon_pos = locate2( ':' , gtid_set, 1);
   while colon_pos != length(gtid_set) + 1 do
      set next_dash_pos = locate2( '-' , gtid_set, colon_pos + 1);
      set next_colon_pos = locate2( ':' , gtid_set, colon_pos + 1);
      set next_comma_pos = locate2( ',' , gtid_set, colon_pos + 1);
      if next_dash_pos < next_colon_pos  and next_dash_pos < next_comma_pos  then
        set result = result +
          substr(gtid_set, next_dash_pos + 1,
                 least(next_colon_pos, next_comma_pos) - (next_dash_pos + 1)) -
          substr(gtid_set, colon_pos + 1, next_dash_pos - (colon_pos + 1)) + 1;
      else
        set result = result + 1;
      end if;
      set colon_pos = next_colon_pos;
   end while;
   return result;
end $$
create function gr_applier_queue_length()
returns int
deterministic
begin
   return ( select sys.gtid_count( gtid_subtract( ( select
received_transaction_set  from performance_schema.replication_connection_status
where channel_name =  'group_replication_applier' ), ( select
@@ global .gtid_executed) )));
end $$
create function gr_member_in_primary_partition()
returns varchar (3)
deterministic
begin
   return ( select if( member_state= 'online' and (( select count (*)  from
performance_schema.replication_group_members  where member_state !=  'online' ) >=
(( select count (*)  from performance_schema.replication_group_members)/2) = 0),
'yes' 'no' from performance_schema.replication_group_members  join
performance_schema.replication_group_member_stats using(member_id)
           where performance_schema.replication_group_members.member_host=@@hostname);
end $$
create view gr_member_routing_candidate_status  as
     select
         sys.gr_member_in_primary_partition()  as viable_candidate,
         if(( select
                     ( select
                                 group_concat(variable_value)
                             from
                                 performance_schema.global_variables
                             where
                                 variable_name  in ( 'read_only' 'super_read_only' )) !=  'off,off'
                 ),
             'yes' ,
             'no' as read_only,
         sys.gr_applier_queue_length()  as transactions_behind,
         count_transactions_in_queue  as 'transactions_to_cert'
     from
         performance_schema.replication_group_member_stats a
             join
         performance_schema.replication_group_members b  on a.member_id = b.member_id
     where
         b.member_host  in ( select
                 variable_value
             from
                 performance_schema.global_variables
             where
                 variable_name =  'hostname' )$$
delimiter ;


猜你喜欢

转载自blog.51cto.com/14354846/2408083