使用 Mycat 进行 MySQL 单库分表
一、Mycat 安装部署
准备:
安装环境 Linux
目前只有 1.6.5 版本支持单库分表。
下载 Mycat 1.6.5 wget http://dl.mycat.io/1.6.5/Mycat-server-1.6.5-release-20171029183033-linux.tar.gz
解压后,修改配置文件,在 mycat/conf 下
1.修改 server.xml

设置 user name,password 及 schemas, name 和 password 要在 Mycat 连接 MySQL 数据库时使用,schemas 是逻辑库,要和 schema.xml 里的 schema name 对应。
2.修改 schema.xml

如图,schema name 要和 server.xml 对应上,user 表设置分表十张,只能设置一个 datanote,rule 采用取模模式 mod-long。writeHost 和 readHost 设为同一台数据库。
3.修改 rule.xml

此次选用默认的 mod-long 规则,只需修改 count 数,对应上 subTables 数即可。
修改 property count 等于 10。
这样,配置已经完成。
二、启动 Mycat 及连接 MySQL
先在 MySQL 库上,建 10 张表,user1-user10。
1.进入 Mycat 包下的 bin 目录,启动 Mycat
2.连接 MySQL

3.查询 Mycat 用户

4.查询分表

10 张子表全部查出
5.往库中 insert 数据



之前已经插入 40 条数据。 由图可以得知,50 条数据是均分到 10 张子表上。
注:插入时要自己写 ID。
由此,实现 MySQL 单库分表及实现数据均分。
总结
MySQL 数据库分库分表不是开发中必须的一步,并不是必选项,他是根据你的业务量以及其他综合因素决定的,当你的数据表中的数据量过多导致当个文件过大时,你可以根据数据存储的具体情况使用相应的策略进行切片,这能避免大幅度修改代码甚至无需修改代码即可解决问题。如果根据策略使用分库分表,需要对项目进行一定的修改,而且分表也给数据的读取增加了一定的复杂度,特别是多表联合查询。还需要注意的是,如果到了不得不分库分表的时候,设计分库分表的策略一定想得长远一点,想得细一点,以应对后续的业务变化
版权声明: 本文为 InfoQ 作者【迷彩】的原创文章。
原文链接:【http://xie.infoq.cn/article/51155d34d9fdacba342f7bc05】。文章转载请联系作者。
评论