当前位置:网站首页>分布式 | 几步快速拥有读写分离
分布式 | 几步快速拥有读写分离
2022-06-29 17:39:00 【ActionTech】
作者:王娟
爱可生 dble 团队测试成员,主要负责 dble 需求测试,自动化编写和社区问题解答。人狠话不多。
本文来源:原创投稿
*爱可生开源社区出品,原创内容未经授权不得随意使用,转载请联系小编并注明来源。
dble 从 3.20.10 版本开始⽀持单纯的读写分离功能,可以和分库分表功能分开使⽤。
如何快速拥有读写分离呢?
第一步,准备好一组 mysql 实 例,并确保这组 mysql 实例的主从复制关系正常。如下使用一主二从的mysql:
主:172.100.9.6:3307
从:172.100.9.2:3307、172.100.9.3:3307
分别到2个从实例上执行 show slave status ,检查复制关系是否正常。

第二步,在 db.xml 配置 mysql 实例。如下:
<dbGroup rwSplitMode="1" name="ha_group1" delayThreshold="100">
<heartbeat>show slave status</heartbeat>
<dbInstance name="hostM1" password="******" url="172.100.9.6:3307" user="test" maxCon="1000" minCon="10" primary="true"/>
<dbInstance name="hostS1" password="******" url="172.100.9.2:3307" user="test" maxCon="1000" minCon="10" readWeight="1"/>
<dbInstance name="hostS2" password="******" url="172.100.9.3:3307" user="test" maxCon="1000" minCon="10" readWeight="1"/>
</dbGroup>
配置 dbGroup 时需要注意以下参数:
rwSplitMode: 读操作的负载均衡模式,可选值 0/1/2/3
在进⾏读负载均衡的时候会根据这个配置进⾏
0:不做均衡,直接分发到主实例,从实例将被忽略,不会尝试建⽴连接池,但会有⼼跳连接
1:读操作在所有从实例中均衡,当所有从实例都不可⽤时,下发语句会报错。
2:读操作在所有实例中均衡。
3:读操作在所有从实例中均衡,当所有从实例都不可⽤时,将语句发往主实例。delayThreshold:指定主从延迟阀值,单位秒,默认 -1 ,表⽰⽆延迟
在进⾏读取负载均衡的时候会根据最近⼀次的⼼跳状态以及读库和主库的延迟进⾏判断,如果主从复制不⼯作或者复制延迟超过 delayThreshold 配置,则认为此节点不适合进⾏读取,依赖于⼼跳语句为 show slave status 。如果 delayThreshold=-1 那么读负载均衡选取的时候不会进⾏延迟检测。readWeight:节点权重(负载均衡时候使⽤)
负载均衡过程中会查看所有节点的权重是否相等,如果不相等,那么就会根据权重来配置压⼒。该值需是⼤于等于 0 的整数。如果配为 0 表⽰该节点不参与读。需注意,总权重(所有节点权重之和)必须⼤于 0。如何区分读节点与写节点?
写节点:primary=“true”
读节点:primary 没配置或者 primary=“false”
第三步,user.xml增加读写分离用户。通过dbGroup指定到db.xml里dbGroup的name。
<rwSplitUser name="rwSplit1" password="111111" dbGroup="ha_group1" />
第四步,到 dble 管理端执行 reload @@config_all ,使配置生效。这时就可以使用新创建的读写分离用户 rwSplit1 登录 dble 了。
如何验证读写分离配置是否生效呢?
分别到 3 个 mysql 实例执行:set global general_log = on ,开启 general log 。

使用 rwSplit1 用户登录 dble 8066 端口,执行以下 sql :
insert into test_table values (1, ‘name1’),(2, ‘name2’);
select * from test_table;

查看写节点的 general log ,insert 语句发到写节点上。

分点查看 2 个读节点的 general log ,select 语句发到其中一个读节点上。

补充:
1、同时开启 dble 读写分离和分库分表的功能时,分库分表引⽤的 dbGroup和 读写分离引⽤的 dbGroup 必须相互独⽴。rwSplitUser 引⽤的 dbGroup ,仅需在 db.xml 中定义即可。
2、多个 rwSplitUser 可以引⽤同⼀个 dbGroup 。
3、dble 读写分离哪些语句发往主实例,哪些语句发往从实例呢?参考:https://github.com/actiontech/dble/discussions/3145
边栏推荐
- The soft youth under the blessing of devcloud makes education "smart" in the cloud
- 2022 spring summer collection koreano essential reshapes the vitality of fashion
- Online sql to CSV tool
- 0 basic self-study STM32 (wildfire) - register lit LED
- Inherit Chinese virtues, pay attention to the health of the middle-aged and the elderly, and Yurun milk powder has strong respect for the elderly
- 育润多维发力慈善领域,勇抗企业公益大旗
- R language ggplot2 visualization: use the patchwork package (directly use the plus sign +) to horizontally combine a ggplot2 visualization result and a plot function visualization result to form the f
- Open source warehouse contribution - submit pr
- 基于STM32F103ZET6库函数PWM输出实验
- What is a SCM system? What are the advantages of a supply chain management system?
猜你喜欢

How to solve MySQL 1045 error in Linux

从一个被应用商店坑了的BUG说起

Digital twin energy system, creating a "perspective" in the low-carbon era

mysql支持外键吗

MySQL触发器如何创建与删除
![[webdriver] upload files using AutoIT](/img/69/8c27626d515976b47f1df4831d09c8.png)
[webdriver] upload files using AutoIT

How to solve the 2003 error of MySQL in Linux

Industry application of smart city based on GIS 3D visualization

Automatic vending machine

与爱同行,育润走进贫困家庭,助推公益事业
随机推荐
R language uses GLM function to build Poisson logarithm linear regression model, processes three-dimensional contingency table data to build saturation model, uses exp function and coef function to ob
Split palindrome string [dp + DFS combination]
Bags of Binary Words for Fast Place Recognition in Image Sequenc
Selenium file upload method
The aggregate function in the epidisplay package of R language divides numerical variables into different subsets based on factor variables, and calculates the summary statistics and aggregate data. W
Collaborative development of epidemic home outsourcing project 𞓜 community essay solicitation
Teach you how to install the latest version of mysql8.0 database on windows, nanny level teaching
What value can SRM systems bring to the enterprise?
How to use the chart control of the b/s development tool devextreme - customize the axis position?
How to establish and use KUKA subroutines / functions
Li Kou today's question -535 Encryption and decryption of tinyurl
填充每个节点的下一个右侧节点指针[利用好每个点->尽可能降低时空复杂度]
基于STM32F103ZET6库函数串口实验
分割回文串[dp + dfs组合]
Selenium key combination operation
Mysql中锁的使用场景是什么
R language uses user-defined functions to write deep learning leaky relu activation functions and visualize leaky relu activation functions
Inherit Chinese virtues, pay attention to the health of the middle-aged and the elderly, and Yurun milk powder has strong respect for the elderly
How to solve the 2003 error of MySQL in Linux
Segment tree and tree array template (copy and paste are really easy to use)