关于数据库:腾讯云TDSQL-读写分离的三种方式

2次阅读

共计 3970 个字符,预计需要花费 10 分钟才能阅读完成。

TDSQL 是分布式数据库,所以反对读写拆散,以解决数据库读写压力是必备的技能。而据我理解,TDSQL 有 3 种形式将只读查问发送到备库。它们别离是:

通过 /slave/,须要在连贯的时候通过 “-c” 开启透传性能
通过 proxy 的主动读写拆散,应该要设置 rw_split=”2″ 参数值
创立只读帐号
上面咱们来测试一下前 2 种读写形式,以验证是否可能达到读写拆散的成果(创立只读帐号的形式比较简单,这里就不在演示了)
一、查看实例以后的主从节点散布状况

通过赤兔咱们能够发现,我的主库当初是 10,85.10.51 , 从库别离是 10,85.10.52,10,85.10.53

二、以默认的形式验证是否会读写拆散

1)间接在主库上执行 select TABLE_NAME from tables limit 2 ;

MySQL [(none)]> use information_schema;
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

MySQL [information_schema]> select TABLE_NAME from tables limit 2 ;
TABLE_NAME
CHARACTER_SETS
CLIENT_STATISTICS

2 rows in set (0.03 sec)
2)在前面查看 proxy 的 interf_instance 日志

cd /data/tdsql_run/15002/gateway/log
tail -100f interf_instance_15002.2021-01-24.0
[2021-01-24 10:55:02 497068] INFO topic=group_1611378066_37&tid=14537&con=0x7fa133c5fc00&clientIP=10.85.10.51:44063&sql_size=39&sql_type=3&sub_sql_type=0&sql=select TABLE_NAME from tables limit 2&db=information_schema&user=huyi&10.85.10.51:4002=1&backend=10.85.10.51:4002&autocommit=1&new_connnum=0&conn_tc=0&select_result_sum=2&affect_num=0&hold_conns=2&resultcode=0&timecost=4
从 interf_instance_15002.2021-01-24.0 接口日志中能够发现:

发动 SQL 的客户端是:clientIP=10.85.10.51:44063

而后执行的 IP 是:backend=10.85.10.51:4002

总结:默认形式都在主库,未进行读写拆散转发

三、测试 /slave/ 形式

1)连贯主库,加上”-c“开启透传性能

[root@tdsql1 ~]# mysql -uhuyi -phuyi -h10.85.10.51 -P15002 -c
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 8704
Server version: 5.7.17-11-V2.0R540D002-20191226-1152-log Source distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
MySQL [(none)]>
MySQL [(none)]>
2)执行查问 /slave/ select TABLE_NAME from tables limit 2 ;

MySQL [(none)]> use information_schema;
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

MySQL [information_schema]> /slave/ select TABLE_NAME from tables limit 2 ;
TABLE_NAME
CHARACTER_SETS
CLIENT_STATISTICS

2 rows in set (0.03 sec)
3)在接口日志文件中查看

cd /data/tdsql_run/15002/gateway/log
tail -100f interf_instance_15002.2021-01-24.0
[2021-01-24 10:49:14 939216] INFO topic=group_1611378066_37&tid=14537&con=0x7fa133c5fc00&clientIP=10.85.10.51:44063&sql_size=51&sql_type=3&sub_sql_type=0&sql= /slave/ select TABLE_NAME from tables limit 2&db=information_schema&user=huyi&10.85.10.52:4002=2&backend=10.85.10.52:4002&autocommit=1&new_connnum=1&conn_tc=0&select_result_sum=2&affect_num=0&hold_conns=2&resultcode=0&timecost=23
从 interf_instance_15002.2021-01-24.0 接口日志中能够发现:

发动 SQL 的客户端是:clientIP=10.85.10.51:44063

而后执行的 IP:backend=10.85.10.52:4002

而 10.85.10.52,10.85.10.53 正是我的从库

总结:1. 开启 TDSQL 透传形式,能够通过 /slave/ 关键字,将 SQL 发到从库执行。

  1. 这种形式须要将 /slave/ 写入到 SQL 语句中,罕用于 SQL 调试。

四、测试 rw_split 形式

1)更改 proxy 网关参数, 并重启网关:

vi /data/tdsql_run/15002/gateway/conf/instance_15002.cnf
将 rw_split=”1″ 参数值 改为 rw_split=”2″
./restart_cgroup.sh ../conf/instance_15001
2)通过一般形式登录,并执行查问 select TABLE_NAME from tables limit 2 ;

MySQL [(none)]> use information_schema;
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

MySQL [information_schema]> select TABLE_NAME from tables limit 2 ;
TABLE_NAME
CHARACTER_SETS
CLIENT_STATISTICS

2 rows in set (0.03 sec)
3)在接口日志中确认该 SQL 是否发往从库

cd /data/tdsql_run/15002/gateway/log
tail -100f interf_instance_15002.2021-01-24.0
[2021-01-24 11:06:43 613504] INFO topic=group_1611378066_37&tid=80211&con=0x7f8bfac46c00&clientIP=10.85.10.51:52241&sql_size=39&sql_type=3&sub_sql_type=0&sql=select TABLE_NAME from tables limit 2&db=information_schema&user=huyi&10.85.10.53:4002=1&backend=10.85.10.53:4002&autocommit=1&new_connnum=0&conn_tc=0&select_result_sum=2&affect_num=0&hold_conns=3&resultcode=0&timecost=49
从 interf_instance_15002.2021-01-24.0 接口日志中能够发现:

发动 SQL 的客户端是:clientIP=10.85.10.51:52241

而后执行的 IP 是:backend=10.85.10.53:4002

而 10.85.10.52,10.85.10.53 正是我的从库。通过测试发现,达到了读写拆散的成果。

总结:1. 通过参数调整会发现,但凡通过这个网关发动所有的 select 操作都将发往从库,可能达到读写拆散的成果。

  1. TDSQL 多个从库,怎么保障 SQL 发往提早较低的从库,以保证数据统一呢 ; 或者说发送从库的 SELECT 对实时性要求不是太高。

五、创立只读账号的形式
间接通过赤兔创立只读帐号,抉择读写形式,创立如下(测试略过):

总结:1. 这种只读帐号,也会将查问散发到从库,并且依据配置,能够抉择从库的优先级别。

2. 这种形式比拟适宜只读的业务。

文章起源:云贝学院

正文完
 0