拓扑环境:
主机名 IP地址 角色 版本
node250 172.16.1.250 mysql-master 8.0.25
node251 172.16.1.251 mysql-slave 8.0.25
node253 172.16.1.253 DBLE 3.21.06.0
操作系统环境:
[root@node251 ~]# uname -a
Linux node251 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
[root@node251 ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
DBLE-3.21.06.0安装略
MySQL主从复制环境安装略
DBLE从3.x版本开始,配置读写分离只需要修改两个配置文件,分别为user.xml db.xml
另外需要注意:
1.当在user.xml中仅配置rwSplitUser时,dble不会加载sharding.xml配置文件,即dble不具备分库分表的功能。
2. 当同时开启dble读写分离和分库分表的功能,分库分表引用的dbGroup和读写分离引用的dbGroup必须相互独立。rwSplitUser引用的dbGroup,仅需在db.xml中定义即可。shardingUser引用的dbGroup,需要被配置的schemas对应的sharding.xml中的shardingNode所引用。
3. 多个rwSplitUser可以引用同一个dbGroup。
4. 被读写分离或者分库分表使用的dbGroup内的instance才会有心跳和连接池;未被有效使用的dbGroup内的instance只有心跳,不会初始化连接池。
根据官文文档以上的配置要求,做如下修改:
修改db.xml文件,这个文件中的读写分离dbGroup需要单独配置,也就是dbGroup的name属性不能分库分库的name属性一样,
配置如下:
cat db.xml
<?xml version="1.0"?>
<!--
~ Copyright (C) 2016-2020 ActionTech.
~ License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher.
-->
<dble:db xmlns:dble="http://dble.cloud/" version="4.0">
<dbGroup name="rwSplitdbGroup" rwSplitMode="3" delayThreshold="100">
<heartbeat>show slave status</heartbeat><dbInstance name="instanceM1" url="172.16.1.250:3306" user="root" password="Rscpass123." maxCon="1000" minCon="10" primary="true" readWeight="1" id="11">
<property name="testOnCreate">true</property>
</dbInstance>
<dbInstance name="instanceS1" url="172.16.1.251:3306" user="root" password="Rscpass123." maxCon="200" minCon="50" primary="false">
<property name="heartbeatPeriodMillis">60000</property>
</dbInstance>
<!--<dbInstance name="instanceS1" url="ip3:3306" user="your_user" password="your_psw" maxCon="1000" minCon="10" readWeight="2" disabled="true">-->
<!--<property name="testOnCreate">false</property>-->
<!--</dbInstance>-->
</dbGroup>
</dble:db>
上面配置dbGroup的name属性为rwSplitdbGroup,dbInstance标签,第一个配置主库,第二个配置从库
配置user.xml文件:
<dble:user xmlns:dble="http://dble.cloud/" version="4.0">
<managerUser name="man1" password="654321" maxCon="100"/>
<managerUser name="user" usingDecrypt="true" whiteIPs="127.0.0.1,0:0:0:0:0:0:0:1" readOnly="true"
password="AqEkFEuIFAX6g2TJQnp4cJ2r7Yc0Z4/KBsZqKhT8qSz18Aj91e8lxO49BKQElC6OFfW4c38pCYa8QGFTub7pnw=="/>
<shardingUser name="root" password="123456" schemas="testdb" readOnly="false" maxCon="1000000"/>
<shardingUser name="root2" password="123456" schemas="testdb,testdb2" maxCon="20" tenant="tenant1">
<privileges check="true">
<schema name="testdb" dml="0110">
<table name="tb_global1" dml="0000"/>
<table name="tb_global2" dml="1111"/>
</schema>
</privileges>
</shardingUser>
<!--rwSplitUser not work for now-->
<!--<rwSplitUser name="rwsu1" password="123456" dbGroup="dbGroup1" blacklist="blacklist1" maxCon="20"/>-->
<rwSplitUser name="rwsplit" password="123456" dbGroup="rwSplitdbGroup" maxCon="2000"/>
<blacklist name="blacklist1">
<property name="selelctAllow">true</property>
</blacklist>
</dble:user>
红色加粗的为读写分离配置独立的用户名和密码,前端APP应用直接通过这个帐号密码就可以来实现读写分离
测试:
连接到DBLE:
[root@node253 ~]# mysql -urwsplit -p123456 -h 172.16.1.253 -P8066 --prompt="(\u@DBLE-SQL) [\d]>\_"
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.11-dble-3.21.06.0-9c90cabacaa870e85742d8e5014101661c014f4b-20210812073019 MySQL Community Server - GPL
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
(root@DBLE-SQL) [(none)]> show databases;
+----------------------------+
| Database |
+----------------------------+
| db_1 |
| db_3 |
| db_5 |
| information_schema |
| mysql |
| performance_schema |
| replication_manager_schema |
| sys |
| test |
+----------------------------+
9 rows in set (0.02 sec)
在test库中
插入操作:
(root@DBLE-SQL) [(none)]> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
(root@DBLE-SQL) [test]> select * from rsc;
+----+------+
| id | name |
+----+------+
| 1 | rsc |
| 2 | gmy |
| 3 | aaa |
| 4 | bbb |
| 5 | ccc |
| 6 | ddd |
| 7 | eee |
| 8 | ddd |
+----+------+
8 rows in set (0.01 sec)
(root@DBLE-SQL) [test]> insert into test.rsc(id,name) values(9,'rsc');
Query OK, 1 row affected (0.04 sec)
查看主库的general_log日志:
2021-10-07T09:42:16.805561+08:00 22 Query insert into test.rsc(id,name) values(9,'rsc')
查看从库的general_log日志:
2021-10-07T09:42:16.833449+08:00 7 Query COMMIT /* implicit, from Xid_log_event */
更新操作:
dble:
(root@DBLE-SQL) [test]> update test.rsc set name='1234' where id=8;
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0
查看主库的general_log日志:
2021-10-07T09:44:19.665539+08:00 22 Query update test.rsc set name='1234' where id=8
查看从库的general_log日志:
2021-10-07T09:44:19.678828+08:00 7 Query COMMIT /* implicit, from Xid_log_event */
删除操作:
dble:
(root@DBLE-SQL) [test]> delete from test.rsc where id=8;
Query OK, 1 row affected (0.01 sec)
查看主库的general_log日志:
2021-10-07T09:44:54.878146+08:00 22 Query delete from test.rsc where id=8
查看从库的general_log日志:
2021-10-07T09:44:54.892595+08:00 7 Query COMMIT /* implicit, from Xid_log_event */
查询操作:
dble:
(root@DBLE-SQL) [test]> select * from test.rsc where id in (1,2,3,4);
+----+------+
| id | name |
+----+------+
| 1 | rsc |
| 2 | gmy |
| 3 | aaa |
| 4 | bbb |
+----+------+
4 rows in set (0.01 sec)
查看主库的general_log日志:
没有日志
查看从库的general_log日志:
2021-10-07T09:47:36.810664+08:00 92 Query select * from test.rsc where id in (1,2,3,4)
以上配置验证了读写分离的功能,以上配置主库只提供写入的SQL,不负载读的请求。
可以通过参数做更改:
读操作的负载均衡模式
rwSplitMode 参数的可选值:0/1/2/3
必需项,无默认值,候选值0/1/2/3
在进行读负载均衡的时候会根据这个配置进行
0:不做均衡,直接分发到主实例,从实例将被忽略,不会尝试建立连接池,但会有心跳连接
1:读操作在所有从实例中均衡,当所有从实例都不可用时,下发语句会报错。
2:读操作在所有实例中均衡。
3:读操作在所有从实例中均衡,当所有从实例都不可用时,将语句发往主实例。