陈老师:1415968548 郑老师:2735197625 乐老师:354331153
客服热线:
19941464235/19906632509(微信同号)

客服微信

腾讯云TDSQL数据库 读写分离的实现

作者:胡毅
原创
发布时间:2023-12-19 10:11
浏览量:761


作者:胡毅


TDSQL是分布式数据库,所以支持读写分离,以解决数据库读写压力是必备的技能。而据我了解,TDSQL有3种方式将只读查询发送到备库。它们分别是:


1

查看实例当前的主从节点分布情况

图片

通过赤兔我们可以发现,我的主库现在是10,85.10.51 ,从库分别是10,85.10.52,10,85.10.53


2

以默认的方式验证是否会读写分离

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

MySQL [(none)]> use information_schema;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -A Database changedMySQL [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/logtail -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

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


图片

3

测试/*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 8704Server version: 5.7.17-11-V2.0R540D002-20191226-1152-log Source distributionCopyright (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 namesYou can turn off this feature to get a quicker startup with -ADatabase changedMySQL [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调试。


4

测试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 namesYou can turn off this feature to get a quicker startup with -ADatabase changedMySQL [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 对实时性要求不是太高。


5

创建只读账号的方式

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

图片

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

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

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

*禁止转载,可转发(转发原创文章请注明出处)