可以参考:https://www.cnblogs.com/f-ck-need-u/p/9383126.html#配置proxysql
以下为参考官文档:
proxysql 是基于mysql数据库的,所以安装proxysql之前,还是需要在本地安装msyql数据库!
安装最新版本:
wget http://repo.proxysql.com/ProxySQL/proxysql-1.4.x/centos/7/proxysql-1.4.12-1-centos7.x86_64.rpm
预发行版本为2.x
安装proxysql:
#rpm -ivh proxysql-1.4.12-1-centos7.x86_64.rpm
启动proxysql服务
#service proxysql start
显示:使用配置文件/etc/proxysql.cnf
关闭命令:
#service proxysql stop
使用命令登录proxysql
mysql -u admin -padmin -h 127.0.0.1 -P6032 --prompt='Admin> ’
或者
mysql -u admin -padmin -h 127.0.0.1 -P6032
mysql>\R Admin>
通过\R Admin>进入管理界面,用于关闭proxysql
Admin> proxysql stop
添加后台被监控主机:
Admin> insert into mysql_servers(hostgroup_id,hostname,port) values(1,‘10.6.248.199’,3306)
Admin> insert into mysql_servers(hostgroup_id,hostname,port) values(1,‘10.6.248.199’,3307)
Admin> insert into mysql_servers(hostgroup_id,hostname,port) values(1,‘10.6.248.199’,3308)
在mysql后台数据库中添加proxysql访问账号:
199:33061
grant replication slave,replication client on . to ‘monitor’@’%’ identified by ‘monitor’;
flush privileges;
199:33062
grant replication slave,replication client on . to ‘monitor’@’%’ identified by ‘monitor’;
flush privileges;
199:33063
grant replication slave,replication client on . to ‘monitor’@’%’ identified by ‘monitor’;
flush privileges;
注意:需要校验是否可以进行远程登录,否则无法进行监视
mysql -umonitor -p -h10.6.248.199 -P3306
show global variables like ‘port’;
mysql -umonitor -p -h10.6.248.199 -P3307
show global variables like ‘port’;
mysql -umonitor -p -h10.6.248.199 -P3308
show global variables like ‘port’;
配置监视账号:
UPDATE global_variables SET variable_value=‘monitor’ WHERE variable_name=‘mysql-monitor_username’;
配置监视密码:
UPDATE global_variables SET variable_value=‘monitor’ WHERE variable_name=‘mysql-monitor_password’;
配置监视间隔(毫秒)
UPDATE global_variables SET variable_value=‘2000’ WHERE variable_name IN (‘mysql-monitor_connect_interval’,‘mysql-monitor_ping_interval’,‘mysql-monitor_read_only_interval’);
检查配置结果
SELECT * FROM global_variables WHERE variable_name LIKE ‘mysql-monitor_%’;
加载到RUNTIME中
Admin > LOAD MYSQL VARIABLES TO RUNTIME;
加载到DISK 中
Admin > SAVE MYSQL VARIABLES TO DISK;
检验配置:
Admin > show databases;
Admin > show tables from monitor;
SELECT * FROM monitor.mysql_server_connect_log ORDER BY time_start_us DESC LIMIT 10;
SELECT * FROM monitor.mysql_server_ping_log ORDER BY time_start_us DESC LIMIT 10;
select * from mysql_servers;
配置复制主机组
SELECT * FROM monitor.mysql_server_read_only_log ORDER BY time_start_us DESC LIMIT 10;
表mysql_replication_hostgroups是用于传统的主从复制的
SELECT * FROM mysql_replication_hostgroups;
INSERT INTO mysql_replication_hostgroups VALUES (1,2,‘cluster1’);
其中不可在配置了mysql_replication_hostgroups后,再对mysql_group_replication_hostgroups,否则会出现错误!
表mysql_group_replication_hostgroups 则是用于innodb cluster 或者 group replication的
insert into mysql_group_replication_hostgroups(writer_hostgroup,reader_hostgroup,backup_writer_hostgroup,offline_hostgroup,active,max_writers,writer_is_also_reader,max_transactions_behind)
values(1,2,3,1,4,5,1,5000);
delete from mysql_group_replication_hostgroups;
【假设max_writers=2,则node1、node2、node3其中2个节点(假设node1、node2)在写组hg=10中,node3在备写组hg=20中。此时必须设置writer_is_also_reader=1,否则没有节点负责读操作,所以hg=30中有node1、node2、node3共3个节点。假如node2节点故障,node3节点将从hg=20转移到hg=10,读组hg=30也只有node1和node3,node2会转移到hg=40中,并ProxySQL不断监控它是否上线。
所以,ProxySQL代理多主模型的MGR时,必须设置writer_is_also_reader=1。】
检查:
select hostgroup_id, hostname, port,status from runtime_mysql_servers;
表示,1为写组
2为读组
当他们中有read_only = 0 ,则将被移动到写组中
当他们中有read_only = 1 ,则将被移动到读组中
或者通过mysql_servers之前就将读写组分好。将hostgroup 改为 2为读组
Admin> insert into mysql_servers(hostgroup_id,hostname,port) values(1,‘10.6.248.199’,3308)
Admin> LOAD MYSQL SERVERS TO RUNTIME;
Admin > SAVE MYSQL VARIABLES TO DISK;
注意:
将mysql后台主机设置为只读的方法:
show global variables like “%read_only%”;
flush tables with read lock;
set global read_only = 1;
将mysql后台主机设置为读写状态的命令:
unlock tables;
set global read_only = 0;
然后检查:
SELECT * FROM monitor.mysql_server_read_only_log ORDER BY time_start_us DESC LIMIT 10;
查看是否已经变成只读!
Mysql Users
它用于数据链接传递到后台数据库
使用monitor账号:
Admin> INSERT INTO mysql_users(username,password,default_hostgroup) VALUES (‘root’,’’,1);
Query OK, 1 row affected (0.00 sec)
INSERT INTO mysql_users(username,password,default_hostgroup) VALUES (‘monitor’,‘monitor’,1);
Query OK, 1 row affected (0.00 sec)
LOAD MYSQL USERS TO RUNTIME;
SAVE MYSQL USERS TO DISK;
进行检查:
mysql -umonitor -pmonitor -h 127.0.0.1 -P6033 -e “SELECT 1”
mysql -umonitor -pmonitor -h 127.0.0.1 -P6033 -e “SELECT @@port”
查看统计信息:
Admin> SHOW TABLES FROM stats;
Admin> SELECT * FROM stats.stats_mysql_connection_pool;
Admin> SELECT * FROM stats_mysql_commands_counters WHERE Total_cnt;
Admin> SELECT * FROM stats_mysql_query_digest ORDER BY sum_time DESC;
Admin> SELECT hostgroup hg, sum_time, count_star, digest_text FROM stats_mysql_query_digest ORDER BY sum_time DESC;
启用查询缓存:
UPDATE mysql_query_rules set cache_ttl = 5000 WHERE active = 1 AND destination_hostgroup = 2 ;
LOAD MYSQL QUERY RULES TO RUNTIME;
SAVE MYSQL USERS TO DISK;
测试,如果写库断开了,是否会将只读自动变成可写:
在后台服务器上面设置read_only的影响:
如果当只有一台服务器为read_only为0,也就是可读可写,那么这台服务器死掉后,会将整个数据库对外的链接断开!
当不在后台服务器上面设置read_only为0或者1的时候,会出现以下情况:
测试方式1:组内数据库连接断开后,还是会继续在proxysql上进行查询!
测试方式2:将写库数据库断开,组内的两个本位读组的会变成写组!
2020-02-29更改
: 快速配置PROXYSQL 的脚本
delete from mysql_servers;
insert into mysql_servers(hostgroup_id,hostname,port)
values(1,‘IP’,3306);
insert into mysql_servers(hostgroup_id,hostname,port)
values(2,‘IP’,3306);
注意:版本2.0以后的从库必须设置为 只读
update mysql_servers set weight=5 where hostname=‘IP’
update mysql_servers set max_connections = 6000;
update global_variables set variable_value=‘monitor’
where variable_name=‘mysql-monitor_username’;
update global_variables set variable_value=‘Monitor@123’
where variable_name=‘mysql-monitor_password’;
/** 以下三个参数是读写分离必要的参数,少了后,读写分离会出现问题
update global_variables set variable_value=‘Monitor@123’
where variable_name=‘mysql-monitor_password’;
update global_variables set variable_value=‘Monitor@123’
where variable_name=‘mysql-monitor_password’;
update global_variables set variable_value=‘Monitor@123’
where variable_name=‘mysql-monitor_password’;
update global_variables set variable_value=288000
where variable_name=‘mysql-wait_timeout’;
update global_variables set variable_value=1073741824
where variable_name=‘mysql-max_allowed_packet’
update global_variables set variable_value=20
where variable_name=‘mysql-connect_retries_on_failure’;
update global_variables set variable_value=10000
where variable_name=‘mysql-connectretries_delay’;
其中延迟查询的配置为:mysql-default_query_delay; 设置为不延迟为 0;
提交上面的语句:
load mysql users to runtime;
save mysql users to memory;
load mysql users to memory;
save mysql users to disk;
load mysql users from config;
load mysql query rules to run;
save mysql query rules to mem;
load mysql query rules to mem;
save mysql query rules to disk;
load mysql query rules from config;
load mysql variables rules to run;
save mysql variables to mem;
load mysql variables to mem;
save mysql variables to disk;
load mysql variables from config;
load admin variables rules to run;
save admin variables to mem;
load admin variables to mem;
save admin variables to disk;
load admin variables from config;