美文网首页
基于1主1从 搭建ProxySQL

基于1主1从 搭建ProxySQL

作者: 芒果之夏 | 来源:发表于2020-04-29 16:30 被阅读0次

    1、环境

    角色 ip port db user passwod
    Master 172.16.215.201 3306 db01 xp a
    Slave 172.16.215.202 3306 db01 xp a
    Slave 172.16.215.203 3306 db01 xp a
    ProxySQL sysbench 172.16.215.200 3306 db01 xp a

    2、 数据库设置

    • 准备业务账号
      create user 'xp'@'%' identified with mysql_native_password by 'a';
      grant all on . to 'xp'@'%';

    • 监控账号
      create user 'monitor'@'%' identified with mysql_native_password by 'monitor';
      grant replication client on . to 'monitor'@'%';

    • 设置从库只读
      set global read_only=1;


    以下操作步骤在ProxySQL服务器执行


    3、 安装ProxySQL


    4、 创建分组

    insert into mysql_replication_hostgroups (writer_hostgroup,reader_hostgroup,comment)values(100,101,'myHG');

    查看:
    select * from mysql_replication_hostgroups;


    15881472107780.jpg

    5、 配置服务:数据库映射到分组

    insert into mysql_servers(hostgroup_id,hostname,port,max_connections)values(10,'172.16.215.201',3306,200);
    insert into mysql_servers(hostgroup_id,hostname,port,max_connections)values(101,'172.16.215.202',3306,200);
    insert into mysql_servers(hostgroup_id,hostname,port,max_connections)values(101,'172.16.215.203',3306,200);

    load mysql servers to run;
    save mysql servers to disk;

    查看:
    select * from mysql_servers;


    15881472735977.jpg

    6、 配置业务账号

    insert into mysql_users(username,password,default_hostgroup,default_schema,max_connections) values('xp','a',100,'db01',1000);

    load mysql users to run;
    save mysql users to disk;

    查看:
    select * from mysql_users;


    15881473095013.jpg

    7、 配置监控账号

    set mysql-monitor_username='monitor';
    set mysql-monitor_password='monitor';

    load mysql variables to run;
    save mysql variables to disk;

    查看:
    select * from global_variables;


    15881475337619.jpg

    8、测试ProxySQL到数据库的连通性

    select * from mysql_server_ping_log;


    15881475608553.jpg

    9、 配置读写分离

    • 关写节点的read:(重启服务生效)
      set mysql-monitor_writer_is_also_reader='false';
      load mysql variables to run;
      save mysql variables to disk;

    • 读写策略一:读一个组,写一个组:
      INSERT INTO mysql_query_rules (rule_id,active,match_pattern,destination_hostgroup,apply)
      VALUES
      (1,1,'SELECT.*FOR UPDATE$',100,1),
      (2,1,'SELECT',101,1);

      LOAD MYSQL QUERY RULES TO RUNTIME;
      SAVE MYSQL QUERY RULES TO DISK;

      查看:
      select * from mysql_query_rules\G

    • 读写策略二:某些重点语句一个组
      清空环境:
      DELETE FROM mysql_query_rules;
      LOAD MYSQL QUERY RULES TO RUNTIME;
      SAVE MYSQL QUERY RULES TO DISK;

      INSERT INTO mysql_query_rules (rule_id,active,digest,destination_hostgroup,apply)
      VALUES
      (1,1,'0xAC80A5EA0101522E',101,1),
      (2,1,'0xC19480748AE79B4B',101,1);

      LOAD MYSQL QUERY RULES TO RUNTIME;
      SAVE MYSQL QUERY RULES TO DISK;

      查看:
      select * from mysql_query_rules\G

    10、 sysbench 上流量测试

    1、 编写压测脚本

    • 配置参数:
      vim env.sh
    #!/bin/bash
    
    DBIP='172.16.215.200'
    DBPORT=6033
    DBUSER='xp'
    DBPASSWD='a'
    NOW=`date +'%Y%m%d%H%M'`
    DBNAME='db01'
    REPORT_INTERVAL=1
    TBLCNT=2
    WARMUP=300
    ROWS=1000000
    thread=1
    T=1000
    events=2000000
    
    
    • 准备压测数据:
      vim prepare.sh
    #!/bin/bash
    
    source ./env.sh
    sysbench /usr/local/share/sysbench/oltp_write_only.lua \
    --table-size=$ROWS \
    --tables=$TBLCNT \
    --mysql-port=$DBPORT \
    --threads=${thread} \
    --db-driver=mysql \
    --mysql-db=$DBNAME \
    --mysql-host=$DBIP \
    --mysql-user=$DBUSER \
    --mysql-password=$DBPASSWD prepare
    
    
    • 运行脚本:
      vim run.sh
    #!/bin/bash
    
    source ./env.sh
    sysbench /usr/local/share/sysbench/oltp_read_write.lua \
      --events=${events} \
      --mysql-host=$DBIP \
      --mysql-port=$DBPORT \
      --mysql-user=$DBUSER \
      --mysql-password=$DBPASSWD \
      --mysql-db=$DBNAME \
      --tables=$TBLCNT \
      --table-size=$ROWS \
      --report-interval=$REPORT_INTERVAL \
      --threads=${thread} \
      --db-ps-mode=disable \
      --skip_trx=1 \
      --time=${T} \
      --auto_inc=0 run
    
    

    2、 授权、执行脚本
    chmod +x env.sh prepare.sh vim run.sh
    ./prepare.sh
    ./run.sh

    11、 查看分流效果

    select hostgroup,digest_text,digest,first_seen,last_seen,count_star from stats_mysql_query_digest;


    15881480272645.jpg

    相关文章

      网友评论

          本文标题:基于1主1从 搭建ProxySQL

          本文链接:https://www.haomeiwen.com/subject/xgknwhtx.html