proxySQL是MySQL的中间件产品,是灵活强大的代理层,实现读写分离,支持Query路由功能,支持动态指定某个SQL进行缓存,支持动态加载配置,故障切换和一些SQL
过滤功能
环境:
192.168.139.152 proxy SQL中间件 server-id=1
192.168.139.153 master mysql server-id=2
192.168.139.154 slave mysql server-id=3
proxy SQL更多信息可以访问 https://github.com/sysown/proxysql/wiki
MYSQL安装(all node)
要点:所有节点都安装MySQL并修改/etc/my.cnf 中的server-id
更新root密码123qweASD!
修改配置文件/etc/my.cnf
[mysqld]
server-id=1
mysql主从部署(master-slave)
从节点read_only=on
PROXYSQL安装(proxysql node)
安装依赖包:
yum install -y perl-DBD-MySQL perl-DBI perl-Time-HiRes perl-IO-Socket-SSL
创建repo文件
cat <<EOF | tee /etc/yum.repos.d/proxysql.repo [proxysql_repo] name= ProxySQL YUM repository baseurl=http://repo.proxysql.com/ProxySQL/proxysql-1.4.x/centos/\$releasever gpgcheck=1 gpgkey=http://repo.proxysql.com/ProxySQL/repo_pub_key EOF 安装proxy SQLyum install -y proxysql proxysql配置文件的路径为 /etc/proxysql.cnf 启动proxy SQLservice proxysql start 查看proxysql版本:proxy SQL--version proxy SQL 默认用户名/密码 admin/admin proxysql 管理端口6032 对外服务端口6033登陆proxysql :mysql -u admin -padmin -h 127.0.0.1 -P 6032MySQL>show databases; 显示5个数据库 main disk stats monitor stats_historymain 内存配置数据库,即MEMORY ,表里存放后端db实例,用户验证,路由规则等信息 主要表:mysql_servers 后端可以连接MySQL服务器的列表 mysql_users 配置后端数据库的账户和监控账户 mysql_query_rules 指定Query路由到后端不同服务器的规则列表注:runtime 开头的表,表示当前运行的配置,不可修改;其余表修改后,LOAD使其生效,SAVE使其存到硬盘以供下次重启加载 master 节点创建proxy SQL监控账户:create user ‘monitor‘@‘192.168.139.%‘ identified by ‘123qweASD!‘;grant all privileges on *.* to ‘monitor‘@‘192.168.139.%‘ with grant option; master节点创建对外访问账户:mysql> create user ‘zs‘@‘192.168.139.%‘ identified by ‘123qweASD!‘;mysql> grant all privileges on *.* to ‘zs‘@‘192.168.139.%‘ with grant option; proxy SQL节点:添加主服务器列表 将主从MySQL加进去,自己不加mysql -uadmin -padmin -h 127.0.0.1 -P 6032mysql>insert into mysql_servers(hostgroup_id,hostname,port) values(10,‘192.168.139.153‘,3306);mysql>insert into mysql_servers(hostgroup_id,hostname,port) values(10,‘192.168.139.154‘,3306);mysql>load mysql servers to runtime;mysql>save mysql servers to disk;
机器的状态都是online状态
proxysql配置监控账号mysql> set mysql-monitor_username=‘monitor‘;mysql> set mysql-monitor_password=‘123qweASD!‘; ===》 /etc/proxysql.cnf 中的monitor_password也要修改 配置proxy SQL主从分组mysql> insert into mysql_replication_hostgroups values(10,20,‘proxy‘);mysql> load mysql servers to runtime;mysql> save mysql servers to disk;
配置对外访问账号,默认指定库,并对该用户开启事务持久化保护
mysql> insert into mysql_users(username,password,default_hostgroup) values(‘zs‘,‘123qweASD!‘,10);mysql> update mysql_users set transaction_persistent=1 where username=‘zs‘;mysql> load mysql users to runtime;mysql> save mysql users to disk; 验证登陆服务器就:是主库
读写分离策略配置 mysql> insert into mysql_query_rules(active,match_pattern,destination_hostgroup,apply) values(1,‘^SELECT.*FOR UPDATE$‘,10,1); ===》写mysql> insert into mysql_query_rules(active,match_pattern,destination_hostgroup,apply) values(1,‘^SELECT‘,20,1); ===》读mysql> load mysql query rules to runtime;mysql> save mysql query rules to disk; 读写分离测试: mysql -u zs -p123qweASD! -h 192.168.139.152 -P 6033 登陆proxysql所在机器登陆数据库mysql> select * from mysql.user; 读操作mysql> create database zhangyu ; 写操作 登陆proxysql管理账户
10 是写库组 20是读库组
调整权重,让某个机器承受更多读操作
mysql> update mysql_servers set weight=10 where hostname=‘192.168.139.154‘;
mysql> load mysql servers to runtime;
mysql> save mysql servers to disk;
原文地址:https://www.cnblogs.com/zy1234567/p/10220232.html