一、所需环境
系统为centos7
数据库版本为5.7.26
PROXYSQL 主机IP 10.0.0.31
MYSQL 主库IP地址 10.0.0.31
MYSQL从库I[地址10.0.0.41
MYSQL从库I[地址10.0.0.51
MYSQL已经搭建好主从环境
二、部署搭建proxysql
2.1 安装perl语言包支持
yum -y install perl perl-devel libaio libaio-devel perl-Time-HiRes perl-DBD-MySQL libev
2.2 下载proxysql
wget https://github.com/sysown/proxysql/releases/download/v2.0.7/proxysql-2.0.7-1-centos7.x86_64.rpm
2.3 安装proxysql
rpm -ivh proxysql-2.0.7-1-centos7.x86_64.rpm
proxysql --version 查看是否安装成功
2.4 启动
chkconfig proxysql on
systemctl start proxysql
systemctl status proxysql
netstat -lnp|grep proxysql
proxysql有两个端口,管理端是6032,设置规则和各种配置登陆6032
客户端连接的是6033,后期测试读写分离是否成功,连接6033
三、连接到proxysql管理端配置
3.1 连接到管理端
mysql -uadmin -padmin -h127.0.0.1 -P6032
默认的proxysql下载好后会自动生成admin用户,密码也是admin 端口6032管理端
3.2 添加后端mysql 主机
Insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(1,'10.0.0.31',3306,1,'Write Group');
insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(2,'10.0.0.41',3306,1,'Read Group');
insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(2,'10.0.0.51',3306,1,'Read Group');
其中:hostgroup_id 1 表示写组,2表示读组。
查看下是否添加成功
select * from mysql_servers;
3.3 添加可以访问后端主机的账号
后端主节点上操作:
GRANT ALL ON *.* TO 'proxysql'@'10.0.0.%' IDENTIFIED BY '123456';
proxysql 管理端操作:
insert into mysql_users(username,password,default_hostgroup,transaction_persistent)values('proxysql','123456',1,1);
#在proxysql主机的mysql_users表中添加刚才创建的账号,proxysql客户端需要使用这个账号来访问数据库。
#default_hostgroup默认组设置为写组,也就是1
#当读写分离的路由规则不符合时,会访问默认组的数据库
查看下是否成功
select*frommysql_users\G
3.4 添加检查账号
后端主节点添加账号
GRANT SELECT ON *.* TO 'monitor'@'10.0.0.%' IDENTIFIED BY 'monitor';
proxysql管理端执行:
set mysql-monitor_username='monitor'
set mysql-monitor_password='monitor'
四、添加读写分离规则
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);
查看是否配置成功:
select rule_id,active,match_digest,destination_hostgroup,apply from mysql_query_rules;
五 、保存
将刚才我们修改的数据加载至RUNTIME中
load mysql users to runtime;
load mysql servers to runtime;
load mysql query rules to runtime;
load mysql variables to runtime;
load admin variables to runtime;
#load进runtime,是配置生效
save mysql users to disk;
save mysql servers to disk;
save mysql query rules to disk;
save mysql variables to disk;
save admin variables to disk;
#save到磁盘(/var/lib/proxysql/proxysql.db)中,永久保存配置
MySQL [(none)]> load mysql users to runtime;
Query OK, 0 rows affected (0.00 sec)
... ...
... ...
MySQL [(none)]> save admin variables to disk;
六、测试读写分离
连接到proxy的客户端。也是跟前端业务对接的端口
mysql -uproxysql -p123456 -h127.0.0.1 -P6033
登陆到数据库后,进行增删改查操作。
连接到proxysql的管理端,查看是否配置成功
mysql -uadmin -padmin -h 127.0.0.1 -P6032
select * from stats_mysql_query_digest