【云贝学院】云贝学院TDSQL TCP认证课程已上线,学院有不定期公开课 ,需要进讨论群的同学可以加老师微信:19941464235

作者:胡毅

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

总结: 默认方式都在主库,未进行读写分离转发

https://www.yunbee.net/Home/Course/detail/course_id/85.html

 

三、测试/*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发到从库执行。

2. 这种方式需要将/*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 操作都将发往从库,能够达到读写分离的效果。

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

 

五、创建只读账号的方式
直接通过赤兔创建只读帐号,选择读写方式,创建如下(测试略过):

总结:1.这种只读帐号,也会将查询分发到从库,并且根据配置,可以选择从库的优先级别。

2.这种方式比较适合只读的业务。

【云贝学院】腾讯云TDSQL 专栏

https://mp.weixin.qq.com/mp/appmsgalbum?__biz=MzU5MTQ3ODY3MQ==&action=getalbum&album_id=1636905594903150592&scene=21#wechat_redirect

 

最后,TDSQL 提供了多种读写分离的技术,在生产环境完全可以利用这些特性,结合实际情况,更加合理的使用这些资源,以达到我们数据库性能最优的效果。

*禁止转载,可转发(转发原创文章请注明出处)https://mp.weixin.qq.com/s?__biz=MzU5MTQ3ODY3MQ==&mid=2247485814&idx=1&sn=63622ddb67b0cab34576a50763eeb0dc&chksm=fe2f2396c958aa808c5ffb2ede9114cef7c04096740bde435a8c3fab2e9ec1942ad8b2dc4158&token=823607480&lang=zh_CN#rd

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐