proxysql实现mysql主从的读写分离

文章目录

proxysql实现mysql主从的读写分离_第1张图片
安装proxysql

proxysql

yum install proxysql-1.4.16-1-centos7.x86_64.rpm mariadb -y
service proxysql start
mysql -uadmin -padmin -P6032 -h127.0.0.1
MySQL [(none)]> select * from sqlite_master where name='mysql_servers'\G  查看表结构

向ProxySQL中添加MySQL节点
proxysql

MySQL [(none)]> insert into mysql_servers(hostgroup_id,hostname,port) values(20,'192.168.43.17',3306);
MySQL [(none)]> insert into mysql_servers(hostgroup_id,hostname,port) values(10,'192.168.43.7',3306);
MySQL [(none)]> load mysql servers to runtime;
MySQL [(none)]> save mysql servers to disk;

添加监控后端节点的用户
master

MariaDB [(none)]> grant replication client on *.* to monitor@'192.168.43.%' identified by 'monitor';

ProxySQL上配置监控用户
proxysql

MySQL [(none)]> set mysql-monitor_username='monitor';
MySQL [(none)]> set mysql-monitor_password='monitor';
MySQL [(none)]> load mysql variables to runtime;
MySQL [(none)]> save mysql variables to disk;

监控模块的指标保存在monitor库的log表中
查看监控连接是否正常的 (对connect指标的监控):(如果connect_error的结果为NULL则表示正常)
proxysql

MySQL [(none)]> select * from mysql_server_connect_log;

测试后端服务器是否正常
proxysql

MySQL> select * from mysql_server_ping_log;

查看read_only和replication_lag的监控日志
proxysql

MySQL> select * from mysql_server_read_only_log;
MySQL> select * from mysql_server_replication_lag_log;

分组信息
proxysql

MySQL [(none)]> insert into mysql_replication_hostgroups values(10,20,"test");
MySQL [(none)]> load mysql servers to runtime;
MySQL [(none)]> save mysql servers to disk;
MySQL [(none)]> select hostgroup_id,hostname,port,status,weight from mysql_servers;

配置发送SQL语句的用户
master

MariaDB [(none)]> grant all on *.* to sqluser@'192.168.43.%' identified by 'sqluser'; 

proxysql

MySQL [(none)]> insert into mysql_users(username,password,default_hostgroup)values('sqluser','sqluser',10);
MySQL [(none)]> load mysql users to runtime;
MySQL [(none)]> save mysql users to disk;

创建路由规则
proxysql

MySQL [(none)]> insert into mysql_query_rules (rule_id,active,match_digest,destination_hostgroup,apply)values(1,1,'^SELECT.*FOR UPDATE$',10,1),(2,1,'^SELECT',20,1);
MySQL [(none)]> load mysql query rules to runtime;
MySQL [(none)]> save mysql query rules to disk;
MySQL [(none)]> select rule_id,active,match_digest,destination_hostgroup,apply from mysql_query_rules;

客户端测试
client

yum install mysql -y
mysql -usqluser -psqluser -h192.168.43.27 -P6033 -e "create table t1(id int)" db1

查看路由信息
proxysql

MySQL [(none)]> SELECT hostgroup hg,sum_time, count_star, digest_text FROM stats_mysql_query_digest ORDER BY sum_time DESC;

proxysql实现mysql主从的读写分离_第2张图片

你可能感兴趣的:(MYSQL)