当前位置:   article > 正文

MySQL 主从分离 中间件_Mysql中间件Proxysql实现mysql主从架构读写分离

mysql 主从分离中间价

实验主机

Master: 192.168.0.17

Slave: 192.168.0.20

Proxysql:192.168.0.30

建议关闭防火墙

建立Master: 192.168.0.17与Slave: 192.168.0.20的MySQL主从复制架构

Proxysql:192.168.0.30主机上基于yum安装proxysql

[root@centos6 ~ 20:21:05]#cat <

> [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

f209d07163a94e984a7695e11b244aa5.png

查看yum源配置是否成功

a43cc8128b48b1a7ace32768f9aaa3e8.png

成功了可以开始安装proxysql

yum install proxysql

9b6c2ca52ff9501d2e78e5ee72cc9179.png

用mysql工具进入proxy管理页面

[root@centos6 ~ 20:08:36]#mysql -uadmin -padmin -P6032 -h127.0.0.1

cbad61def61e3c4ed65289beaf545ff5.png

查看proxy的表

f3452bdaf0753c265d76be039e0b8ef6.png

对表mysql_servers中插入主从节点信息,加载到RUNTIME,并保存到disk

MySQL [(none)]> insert into mysql_servers(hostgroup_id,hostname,port) values(10,'192.168.0.17',3306);

MySQL[(none)]>insert into mysql_servers(hostgroup_id,hostname,port) values(10,'192.168.0.20',3306);

MySQL [(none)]> load mysql servers to runtime;

MySQL [(none)]> save mysql servers to disk;

a439d0f5c8670e9e6eb226797a19992c.png

添加监控后端节点的用户。ProxySQL通过每个节点的read_only值来自动调整它们是属于读组还是写组

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

MariaDB [(none)]> flush privileges;

d4d41afc840d1fec7a6aee59b2006d15.png

ProxySQL上配置监控用户,加载到RUNTIME,并保存到disk

MySQL [(none)]> set mysql-monitor_username='monitor';

MySQL [(none)]> set mysql-monitor_password='123';

MySQL [(none)]> load mysql variables to runtime;

MySQL [(none)]> save mysql variables to disk;

f81f581d73112aba3754efa74a310650.png

查看监控连接是否正常

MySQL> select * from mysql_server_connect_log;

2ef0e00d4fa3a065bb039606cf82a2c4.png

查看监控心跳信息 (对ping指标的监控):

MySQL> select * from mysql_server_ping_log;

8ab27ceb2ccec8f962e0db8639d18941.png

查看read_only和replication_lag的监控日志

MySQL> select * from mysql_server_read_only_log;

MySQL> select * from mysql_server_replication_lag_log;

设置分组信息:

需要修改的是main库中的mysql_replication_hostgroups表,该表有3个字段:writer_hostgroup,reader_hostgroup,comment, 指定写组的id为10,读组的id为20

MySQL> insert into mysql_replication_hostgroups values(10,20,"test");

读组proxysql会自动根据my.cnf的read_only来判断你是否是读组自动更改

将mysql_replication_hostgroups表的修改加载到RUNTIME生效

MySQL> load mysql servers to runtime;

MySQL> save mysql servers to disk;

7f0aa0bce08f4ca9d84e1ef104228389.png

Monitor模块监控后端的read_only值,按照read_only的值将节点自动移动到读/写组

MySQL> select hostgroup_id,hostname,port,status,weight from mysql_servers;

d922f5e3bc45f4805ea31c42a2cd4413.png

在master节点上创建访问用户

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

MariaDB [(none)]> flush privileges;

5316b37973cb6e24dacde490d0feb06d.png

在slave节点上创建访问用户

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

MariaDB [(none)]> flush privileges;

a5cfabe5ab56aadf38a74fa0804a4e07.png

在ProxySQL配置,将用户sqluser添加到mysql_users表中, default_hostgroup默认组设置为写组10,当读写分离的路由规则不符合时,会访问默认组的数据库

MySQL> insert into mysql_users(username,password,default_hostgroup) values('sqluser','123',10);

MySQL> load mysql users to runtime;

MySQL> save mysql users to disk;

5a2207d70620fa7d90cbbbb81a07b64b.png

使用sqluser用户测试是否能路由到默认的10写组实现读、写数据

mysql -usqluser –p123 -P6033 -h127.0.0.1 -e 'select @@server_id'

mysql -usqluser –p123 -P6033 -h127.0.0.1 -e 'create database testdb'

mysql -usqluser –p123 testdb -P6033 -h127.0.0.1 -e 'create table t(id int)'

5b69d55f566e780132a867dcf311e254.png

在proxysql上配置路由规则,实现读写分离

与规则有关的表:mysql_query_rules和mysql_query_rules_fast_routing,后者是前者的扩展表,1.4.7之后支持

插入路由规则:将select语句分离到20的读组,select语句中有一个特殊语句SELECT...FOR UPDATE它会申请写锁,应路由到10的写组

MySQL> 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> load mysql query rules to runtime;

MySQL> save mysql query rules to disk;

注意:因ProxySQL根据rule_id顺序进行规则匹配,select ... for update规则的rule_id必须要小于普通的select规则的rule_id

f248b7a3981252d921fa7309cc1c3d39.png

测试读操作是否路由给20的读组

mysql -usqluser -pmagedu -P6033 -h127.0.0.1 -e 'select @@server_id'

c2fe0fa8673921bfc9cdda5ca1e25251.png

测试写操作,以事务方式进行测试

mysql -usqluser -pmagedu -P6033 -h127.0.0.1 -e 'start transaction;select @@server_id;commit;select @@server_id'

路由的信息:查询stats库中的stats_mysql_query_digest表

MySQL > SELECT hostgroup hg,sum_time, count_star, digest_text FROM stats_mysql_query_digest ORDER BY sum_time DESC;

37be586eb7ef7bd955c5f43f309808a0.png

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/230573
推荐阅读
相关标签
  

闽ICP备14008679号