• ProxySQL 读写分离策略


    1.配置读写分离策略:路由规则
    配置读写分离,就是配置ProxySQL 路由规则,ProxySQL 的路由规则非常灵活,可以基于用户,基于schema,以及单个sql语句实现路由规则定制。
     
    注意:我这只是试验,只是配置了几个简单的路由规则,实际情况配置路由规则,不应该是就根据所谓的读、写操作来进行读写分离,而是从收集(慢日志)的各项指标找出压力大,执行频繁的语句单独写规则,做缓存等等。比如 先在测试几个核心sql语句,分析性能提升的百分比,在逐渐慢慢完善路由规则。 
    生产中使用读写分离 :建议基于hash code 值做读写分离,不要建太多的规则
    和查询规则有关的表有两个:mysql_query_rules和mysql_query_rules_fast_routing 
    表mysql_query_rules_fast_routing是mysql_query_rules的扩展,并在以后评估快速路由策略和属性(仅在ProxySQL 1.4.7+中可用)。
     
    介绍一下改表mysql_query_rules的几个字段: 
    active:是否启用这个规则,1表示启用,0表示禁用 
    match_pattern 字段就是代表设置规则 
    destination_hostgroup 字段代表默认指定的分组
    apply 代表真正执行应用规则。
     
    2.创建规则:
    A.把所有以select 开头的语句全部分配到读组中,读组编号是0
     
    B.把 select .. for update 语句,这是一个特殊的select语句,会产生一个写锁(排他锁),把他分到编号为1 的写组中,其他所有操作都会默认路由到写组中
    insert into mysql_query_rules(rule_id,active,match_pattern,destination_hostgroup,apply) values (1,1,'^select.*for update$',1,1);
    
    insert into mysql_query_rules(rule_id,active,match_pattern,destination_hostgroup,apply) values (2,1,'^select',0,1);
        
    load mysql query rules to runtime;
    
    save mysql query rules to disk;

    select … for update规则的rule_id必须要小于普通的select规则的rule_id,因为ProxySQL是根据rule_id的顺序进行规则匹配的
    通过对外访问账号测试。
     
    如果想在 ProxySQL 中查看SQL请求路由信息stats_mysql_query_digest
    [stats]> select hostgroup,schemaname,username,digest_text,count_star from  stats_mysql_query_digest;
    +-----------+--------------------+----------+----------------------------------+------------+
    | hostgroup | schemaname         | username | digest_text                      | count_star |
    +-----------+--------------------+----------+----------------------------------+------------+
    | 1         | information_schema | yoon     | select @@server_id               | 1          |
    | 1         | information_schema | yoon     | show databases                   | 1          |
    | 1         | information_schema | yoon     | select @@version_comment limit ? | 1          |
    +-----------+--------------------+----------+----------------------------------+------------+

    count_start 统计sql 语句次数,可以分析哪些 sql ,频繁执行
     
    读写分离设置成功后,还可以调权重,比如让某台机器承受更多的读操作 
    update mysql_servers set weight=10 hostname='192.168.1.2';
    
    load mysql servers to runtime;
    
    save mysql servers to disk;
  • 相关阅读:
    python中__dict__和dir()
    python学习之copy模块
    python学习之optparse
    python join和split和strip用法
    浅谈 Python 的 with 语句
    Python:itertools模块
    OpenStack Swift client开发
    OpenStack Swift集群部署流程与简单使用
    python bisect模块
    Python中的导入
  • 原文地址:https://www.cnblogs.com/hankyoon/p/16416753.html
Copyright © 2020-2023  润新知