MySQL Query Rules
Here is the statement used to create the mysql_query_rules 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),
reconnect
INT CHECK (reconnect
IN (
0,
1)) DEFAULT
NULL,
timeout
INT UNSIGNED,
retries
INT CHECK (retries
>=0 AND retries
<=1000),
delay
INT UNSIGNED,
mirror_flagOUT
INT UNSIGNED,
mirror_hostgroup
INT UNSIGNED,
error_msg
VARCHAR,
sticky_conn
INT CHECK (sticky_conn
IN (
0,
1)),
multiplex
INT CHECK (multiplex
IN (
0,
1)),
log
INT CHECK (log
IN (
0,
1)),
apply
INT CHECK(apply
IN (
0,
1))
NOT NULL DEFAULT
0,
comment VARCHAR)
The fields have the following semantics 语义:
-
rule_id - the unique id of the rule. Rules are processed in rule_id order 唯一的规则id编号,规则按照rule_id的顺序执行的
-
active - only rules with active=1 will be considered by the query processing module active=1 的时候,查询模块才会考虑这一条规则
-
username - filtering criteria 准确的 matching username. If is non-NULL, a query will match only if the connection is made with the correct username username 用来匹配精确的用户名,如果它是一个非空的值,查询将会只匹配从这个username发起的查询
-
schemaname - filtering criteria matching schemaname. If is non-NULL, a query will match only if the connection uses schemaname as default schema 用来指定连接进来匹配的shcemaname
-
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) 这三个是重要的配置。它们是用来创建各个规则之间的规则链的,也就是用来进行连接多个规则的。如果一条规则中定义的输入标识符为0,那么只有包含输入标识符为0 的规则才会在一开始就应用这条规则。当被匹配到的规则被发现是一个特定的查询,在输出标识符是非空的情况下,这个查询会被打上对应的输出标识符。如果输出标识符和输入标识符不一致,查询将离开当前的规则链并且进入到下一个规则中(这个规则的输入标识符和前面的输出标识符一致),它将会成为下一条规则的输入标识符。如果输出标识符和输入标识符一致,查询将会回到前面标记有同样标识符的规则中。这种情形会循环发生,直到没有更多的匹配规则,如果apply被设置为1,就意味着这是规则链中的最后一条规则,规则将在这里被终止。
-
client_addr - match traffic from a specific source 匹配发起查询的应用的IP
-
proxy_addr - match incoming traffic on a specific local IP 匹配查询指定的本地的proxysql的IP
-
proxy_port - match incoming traffic on a specific local port 指定本地proxysql的查询端口
-
digest - match queries with a specific digest, as returned bystats_mysql_query_digest.digest 匹配查询指定的摘要,可以在stats_mysql_query_digest.diges列中查到,不指定的化会被自动分配一个
-
match_digest - regular expression that matches the query digest. See also mysql-query_processor_regex 匹配查询摘要的正则表达式 类型有两种 PCRE 和 RE2 默认是PCRE
-
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 如果这个参数被设置为1,只有没有被匹配到的查询规则的查询会被执行。它是一个正则相反的操作,和match_pattern or match_digest两个参数相对应。
-
re_modifiers - comma separated list of options to modify the behavior of the RE engine. WithCASELESS 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.逗号隔开的列表选项来用来修改正则引擎的操作。caseless 选项表明不进行递归,只能够匹配到查询中的第一个匹配到的字符串。global选项下,也就是全局选项下,能够替换到查询语句中所有的陪匹配到的文本。为了向后兼容,默认设置的是 caseless.
-
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 (seemysql_users table). 指定目的组的编号,只有在起了事务和登录的用户ransaction_persistent flag被设置为1 的时候才生效。ransaction_persistent flag 默认就是被设置为1
-
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 缓冲查询结果的毫秒数 proxysql是按秒为单位的
-
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_timeoutapplies 匹配或者重定向能够占用的最大的超时时间。如果一个查询执行了超过设置阈值的大小,它会被自动kill。如果时间没有被设置,会默认使用mysql-default_query_timeout;默认10个小时 mysql-default_query_timeout | 36000000
-
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 当一个查询失败后,它重新去执行的最大次数。如果没有设置,默认是 1 | mysql-query_retries_on_failure | 1 |
-
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.延迟执行查询的毫秒数。这是一个限制机制,用来提高某些查询的优先级别。默认是0,以后的版本会更高级的延迟机制。
-
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 查询被阻断后,返回给客户端的错误信息
-
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). Default is NULL, thus not modifying multiplexing policies 如果是0 会禁止多路复用,如果是1,会使用多路复用(当其他的条件和多路复用没有冲突的时候,比如用户变量或者事务)。
-
log - query will be logged 是否写入日志
-
comment - free form text field, usable for a descriptive comment of the query rule 对规则的语言描述,说明它的功能