环境:
MySQL主从配置略过。
# 安装 yum localinstall -y ./proxysql-2.2.0-1-centos7.x86_64.rpm # 启动 systemctl start proxysql
客户端口号: 6033。代理 mysql 服务的端口,也就是应用连接使用的
管理端口号: 6032。管理 proxysql配置 的端口,只能本地登录
# 1. 登录管理端 mysql -uadmin -padmin -h127.0.0.1 -P6032 # 2. 配置主从库 use main; insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(1,'192.168.0.10',3306,1,'master1'); insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(2,'192.168.0.11',3306,1,'slave1'); # 2.2 检查添加结果 select * from main.mysql_servers; # 2.3 加载配置到RUNTIME load mysql servers to runtime; # 2.4 写盘保存 save mysql servers to disk; # 3. 配置proxysql客户端账户密码。 # 设置所有请求默认到hostgroupid为1的实例然后再根据路由分发 # transaction_persistent为1开启事务支持 insert into mysql_users(username,password,default_hostgroup,transaction_persistent)values('root','123456',1,1); # 3.2 查询账户添加结果 select * from mysql_users; # 3.3 保存 load mysql users to runtime; save mysql users to disk; # 4. 配置健康检测账号 # 4.1 登录后端master的mysql控制台 GRANT replication client ON *.* TO 'monitor'@'%' IDENTIFIED BY '123456'; flush privileges; # 4.2 切换回proxy的控制台 set mysql-monitor_username='monitor'; set mysql-monitor_password='123456'; load mysql variables to runtime; save mysql variables to disk; # 5. 配置读写分离路由 # 将select语句全部路由至hostgroup_id=2的组,也就是从节点 # select * from tb for update这样的语句是修改数据的,所以需要单独定义,将它路由至hostgroup_id=1的组(也就是主节点) # 其他没有被规则匹配到的组将会被路由至用户默认的组(mysql_users表中的default_hostgroup) insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply)values(1,1,'^SELECT.*FOR UPDATE$',1,1); insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply)values(2,1,'^SELECT',2,1); load mysql query rules to runtime; load admin variables to runtime; save mysql query rules to disk; save admin variables to disk;
mysql -uroot -p'123456' -P6033 -h 127.0.0.1 # 随便执行点sql语句
select * from stats.stats_mysql_query_digest; -- 或者 select hostgroup,schemaname,digest_text,count_star,sum_time,min_time,max_time,sum_rows_affected,sum_rows_sent from stats.stats_mysql_query_digest;
set mysql-threads=16; -- 查看 show variables like '%mysql-threads%';