前言
由于项目中用户数量已经达到5亿,数据库存储性能有所降低,因此,需要将原来的数据表拆分出10张表来存放用户数据,提高mysql性能。
技术选型
据了解,mycat能够实现分库分表功能,但是,在这里我们还是选择sharking-proxy来做分表处理。好吧,那就开始苦逼的研究之旅了。
获取sharding-proxy
下载sharding-proxy的地址为
https://shardingsphere.apache.org/document/current/cn/downloads/,在这里,我下载的是最新版本的sharding-proxy来演示。
下载后,我们得到的文件为:apache-shardingsphere-incubating-4.0.1-sharding-proxy-bin.tar.gz
配置
将压缩包解压放到服务器10.2.38.5的/usr/local/目录下,并重命名为sharding-proxy。
为了避免干扰,删除多余配置,剩下这几个配置,如下图:
server.yaml为全局配置文件,配置如下:
authentication:
users:
root:#连接代理使用的用户名
password: root #连接代理使用的密码
# sharding:
# password: sharding
# authorizedSchemas: sharding_db
#
props:
max.connections.size.per.query: 1
acceptor.size: 16 # The default value is available processors count * 2.
executor.size: 16 # Infinite by default.
proxy.frontend.flush.threshold: 128 # The default value is 128.
# LOCAL: Proxy will run with LOCAL transaction.
# XA: Proxy will run with XA transaction.
# BASE: Proxy will run with B.A.S.E transaction.
proxy.transaction.type: LOCAL
proxy.opentracing.enabled: false
query.with.cipher.column: true
sql.show: false
config-sharding.yaml为数据配置文件,配置如下:
######################################################################################################
#
# If you want to connect to MySQL, you should manually copy MySQL driver to lib directory.
#
######################################################################################################
schemaName: sharding_db
#数据源配置
dataSources:
ds_0:
url: jdbc:mysql://localhost:3306/extmail?useUnicode=true&characterEncoding=utf8&useSSL=false
username: root
password: root
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
#规则配置
shardingRule:
tables:
mailbox:
actualDataNodes: ds_0.mailbox_${0..9}
tableStrategy:
inline:
shardingColumn: username
algorithmExpression: mailbox_${username.substring(10,11)}
keyGenerator:
type: SNOWFLAKE
column: username
domain:
actualDataNodes: ds_0.domain
tableStrategy:
none:
alias:
actualDataNodes: ds_0.alias
tableStrategy:
none:
defaultDataSourceName: ds_0
defaultTableStrategy:
none:
#主重配置
masterSlaveRule:
name: ms_ds
masterDataSourceName: ds_0
slaveDataSourceNames:
- ds_0
数据库相关参数解读:
dataSources配置数据源,可以多个数据源,现在只配置了一个数据源;
tabels是指被代理的数据表,现在我这里需要代理的数据表共有三张mailbox、domain、alias;
tableStrategy指的是分表策略,mailbox采用的策略是自定义策略,domain和alias表无策略,仅仅是代理;
actualDataNodes指得是我们程序通过代理读取的实际表的名称
algorithmExpression这是分表采用的策略,mailbox_${username.substring(10,11)}为截取第11位数字进行分表
启动服务
配置完服务后,可以启动了,执行./bin/start.sh命令,就可以启动了。
tail -100f ./logs/stdout.log 就可以查看启动日志了,也可以查看服务运行的日志。
使用工具连接
打开navicat数据库工具,连接数据库,其实是代理。
用户名/密码是server.yaml配置的用户名/密码,sharding-proxy默认启动的端口为3307,当然,我们可以在执行启动命令./bin/start.sh 端口,指定启动端口。
测试
这里是将mailbox表拆分为10张,分别为mailbox_0~mailbox_9。
我们可以在数据库手动创建这10张表,也可以直接在工具上创建mailbox表,代理会自动在数据库创建10张表的。
在这里,我就手动在数据库创建了10张表了。
首先,我测试一下mailbox_3表,现在这张表没有数据
然后,我执行插入命令插入数据,如下
因为我在配置中根据的是username这个字段的第11个数据进行分表的,因此,为了将数据插入mailbox_3需将username的第11位改为3。结果如下
现在,以username为条件查询结果如下:
没分表的表查询结果如下:
总结
使用sharding-proxy做分表代理能够实现功能,但是好像原数据库无需分表的数据表也需要进行代理,感觉有点麻烦,要是无需被分表的数据表很多的话。不知道有没有其他方法更好实现,还有待研究了。