OneProxy实现MySQL分库分表

本文涉及的产品
RDS MySQL DuckDB 分析主实例,集群系列 4核8GB
简介:

Part1:写在最前

    随着网站的壮大,MySQL数据库架构一般会经历一个过程:

wKiom1hc37iQe--XAACBs7m98DM499.png

当我们数据量比较小的时候,一台单实例数据库足矣。等我们数据量增大的时候,我们会采用一主多从的数据库架构来降低我们的读写io。当我们某张业务表达到几百万上千万甚至上亿时,就应该去进行分表处理。本文演示OneProxy对数据库实现分表处理,对前端应用是透明的。


Part2:环境简介

HE1:192.168.1.248 Master1

HE3:192.168.1.250 Master2

HE4:192.168.1.251 Oneproxy


环境构建

Part1:安装Oneproxy

Oneproxy的安装不是本文讲述的重点,需要的可移步至

OneProxy实现MySQL读写分离与负载均衡

http://suifu.blog.51cto.com/9167728/1884673


Part2:proxy.cnf

proxy.cnf文件是oneproxy的主要参数配置文件,新版的oneproxy对整个目录进行了重新的划分,配置文件都放在了conf目录里

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@HE4 oneproxy] # cat conf/proxy.conf 
[oneproxy]
keepalive = 1
event-threads = 4
log- file  = log /oneproxy .log
pid- file  = log /oneproxy .pid
lck- file  = log /oneproxy .lck
mysql-version = 5.7.16
proxy-address = :3307
proxy-master-addresses.1 = 192.168.1.248:3306@group1
proxy-master-addresses.2 = 192.168.1.250:3306@group2
proxy-user-list = sys_admin /1C6D087BA5D2607A27DECB2F2AFE247E911E877A @ test
proxy-part-tables.1 =  /root/oneproxy/conf/part .txt
#proxy-part-tables.2 = /root/oneproxy/conf/part2.txt
proxy-charset = utf8_bin
proxy-group-policy.1 = group1:master-only
proxy-group-policy.2 = group2:master-only
proxy-secure-client = 192.168.1.248
proxy-sequence.1 = default
proxy-httpserver = :8080
proxy-httptitle = OneProxy Monitor


Part3:part.txt

part.txt文件是分区策略配置文件,在本博文中,采取hash分区来进行简单演示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@HE4 oneproxy] # cat conf/part.txt 
[
   {
     "table"  "helei" ,
     "pkey"  "id" ,
     "type"  "int" ,
     "method"  "hash" ,
     "partitions"  :     
       [
         "suffix"  "_0" "group" "group1"  },
         "suffix"  "_1" "group" "group2"  },
         "suffix"  "_2" "group" "group1"  },
         "suffix"  "_3" "group" "group2" }
       ]
   }
]



实战

Part1:启动OneProxy

1
2
[root@HE4 oneproxy] # ./oneproxy.service start
Starting OneProxy ...                                      [  OK  ]


Part2:监控页面

我这里是两台Master

wKioL1hc466zsFDWAAJj-SS3Mng695.jpg



Part3:创建相关表

登录oneproxy管理库创建表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@HE1 ~] # mysql -usys_admin -pMANAGER -h192.168.1.251 -P3307 test
mysql: [Warning] Using a password on the  command  line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection  id  is 103
Server version: 5.7.16 OneProxy-Community-5.8.5 (OneXSoft)
Copyright (c) 2000, 2016, Oracle and /or  its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and /or  its
affiliates. Other names may be trademarks of their respective
owners.
Type  'help;'  or  '\h'  for  help. Type  '\c'  to  clear  the current input statement.
mysql> create table helei(
     ->  id  int(10) unsigned NOT NULL AUTO_INCREMENT,
     -> c1 int(10) NOT NULL DEFAULT  '0' ,
     -> c2 int(10) unsigned DEFAULT NULL,
     -> c5 int(10) unsigned NOT NULL DEFAULT  '0' ,
     -> c3 timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
     -> c4 varchar(200) NOT NULL DEFAULT  '' ,
     -> PRIMARY KEY( id ),
     -> KEY idx_c1(c1),
     -> KEY idx_c2(c2)
     -> )ENGINE=InnoDB ;
Query OK, 0 rows affected (0.27 sec)
mysql> \q


Part4:插入数据

1
2
3
4
5
6
7
8
[root@HE1 ~] # mysql -usys_admin -pMANAGER -h192.168.1.251 -P3307 test -e"insert into helei(id,c1,c2,c5,c4) values(1,1,1,1,'1')"
mysql: [Warning] Using a password on the  command  line interface can be insecure.
[root@HE1 ~] # mysql -usys_admin -pMANAGER -h192.168.1.251 -P3307 test -e"insert into helei(id,c1,c2,c5,c4) values(2,2,2,2,'2')"
mysql: [Warning] Using a password on the  command  line interface can be insecure.
[root@HE1 ~] # mysql -usys_admin -pMANAGER -h192.168.1.251 -P3307 test -e"insert into helei(id,c1,c2,c5,c4) values(3,3,3,3,'3')"
mysql: [Warning] Using a password on the  command  line interface can be insecure.
[root@HE1 ~] # mysql -usys_admin -pMANAGER -h192.168.1.251 -P3307 test -e"insert into helei(id,c1,c2,c5,c4) values(4,4,4,4,'4')"
mysql: [Warning] Using a password on the  command  line interface can be insecure.

校验

Part1:校验oneproxy表内容

这里可以看到虚拟表helei中已经具有刚刚插入的内容;

1
2
3
4
5
6
7
8
9
10
[root@HE1 ~] # mysql -usys_admin -pMANAGER -h192.168.1.251 -P3307 test -e"select * from helei";
mysql: [Warning] Using a password on the  command  line interface can be insecure.
+----+----+------+----+---------------------+----+
id  | c1 | c2   | c5 | c3                  | c4 |
+----+----+------+----+---------------------+----+
|  4 |  4 |    4 |  4 | 2016-12-23 00:07:21 | 4  |
|  1 |  1 |    1 |  1 | 2016-12-23 16:07:04 | 1  |
|  2 |  2 |    2 |  2 | 2016-12-23 00:07:10 | 2  |
|  3 |  3 |    3 |  3 | 2016-12-23 16:07:16 | 3  |
+----+----+------+----+---------------------+----+


Part2:校验Master1中的内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
[root@HE1 ~] # mysql -uroot -pMANAGER test
mysql: [Warning] Using a password on the  command  line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection  id  is 158
Server version: 5.7.16-log MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and /or  its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and /or  its
affiliates. Other names may be trademarks of their respective
owners.
Type  'help;'  or  '\h'  for  help. Type  '\c'  to  clear  the current input statement.
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| checksums      |
| helei_0        |
| helei_2        |
| sbtest         |
+----------------+
4 rows  in  set  (0.00 sec)
mysql>  select  * from helei_0;
+----+----+------+----+---------------------+----+
id  | c1 | c2   | c5 | c3                  | c4 |
+----+----+------+----+---------------------+----+
|  4 |  4 |    4 |  4 | 2016-12-23 00:07:21 | 4  |
+----+----+------+----+---------------------+----+
1 row  in  set  (0.00 sec)
mysql>  select  * from helei_2;
+----+----+------+----+---------------------+----+
id  | c1 | c2   | c5 | c3                  | c4 |
+----+----+------+----+---------------------+----+
|  2 |  2 |    2 |  2 | 2016-12-23 00:07:10 | 2  |
+----+----+------+----+---------------------+----+
1 row  in  set  (0.00 sec)


Part3:校验Master2中的内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
[root@HE3 ~] # mysql -uroot -pMANAGER test
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection  id  is 2997
Server version: 5.7.16-log MySQL Community Server (GPL)
Copyright (c) 2000, 2013, Oracle and /or  its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and /or  its
affiliates. Other names may be trademarks of their respective
owners.
Type  'help;'  or  '\h'  for  help. Type  '\c'  to  clear  the current input statement.
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| checksums      |
| helei_1        |
| helei_3        |
+----------------+
3 rows  in  set  (0.00 sec)
mysql>  select  * from helei_1;
+----+----+------+----+---------------------+----+
id  | c1 | c2   | c5 | c3                  | c4 |
+----+----+------+----+---------------------+----+
|  1 |  1 |    1 |  1 | 2016-12-23 16:07:04 | 1  |
+----+----+------+----+---------------------+----+
1 row  in  set  (0.00 sec)
mysql>  select  * from helei_3;
+----+----+------+----+---------------------+----+
id  | c1 | c2   | c5 | c3                  | c4 |
+----+----+------+----+---------------------+----+
|  3 |  3 |    3 |  3 | 2016-12-23 16:07:16 | 3  |
+----+----+------+----+---------------------+----+
1 row  in  set  (0.00 sec)


注意

Warning:告1

不支持预编译语句 PreparedStatement,不支持Bind、Execute调用接口。


Warning:告2

不支持使用use命令来切换后端数据库,use命令可执行,但其含义是切换到不同的MySQL主备集群,OneProxy在支持分库分表功能后,就将一个主备集群视为一个数据库了,链接Oneproxy时如果指定了数据库名,则需替换成Server Group的名字


Warning:告3

禁止使用set命令,任何set命令都会直接返回成功,而不做任何处理。


Warning:告4

默认禁止CALL、PREPARE、EXECUTE、DEALLOCATE命令,不支持存储过程和函数。


Warning:告5

OneProxy支持master进行故障转移切换,但建议采用流行的高可用方案MHA实现。故障切换后,OneProxy可以自动识别哪台机器是master。另外,架构必须是一主带N从,不能是双主带N从。





——总结——

至此,OneProxy对MySQL的分库分表测试完成,对于前端应用而言,表名是透明的。无需变更代码。由于笔者的水平有限,编写时间也很仓促,文中难免会出现一些错误或者不准确的地方,不妥之处恳请读者批评指正。




参考文献:

http://hcymysql.blog.51cto.com/5223301/1685146

http://www.onexsoft.com




 本文转自 dbapower 51CTO博客,原文链接:http://blog.51cto.com/suifu/1885581,如需转载请自行联系原作者


相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。   相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情: https://www.aliyun.com/product/rds/mysql 
相关文章
|
5月前
|
关系型数据库 MySQL Java
MySQL 分库分表 + 平滑扩容方案 (秒懂+史上最全)
MySQL 分库分表 + 平滑扩容方案 (秒懂+史上最全)
|
存储 算法 关系型数据库
(二十二)全解MySQL之分库分表后带来的“副作用”一站式解决方案!
上篇《分库分表的正确姿势》中已经将分库分表的方法论全面阐述清楚了,总体看下来用一个字形容,那就是爽!尤其是分库分表技术能够让数据存储层真正成为三高架构,但前面爽是爽了,接着一起来看看分库分表后产生一系列的后患问题,注意我这里的用词,是一系列而不是几个,也就是分库分表虽然好,但你要解决的问题是海量的。
1177 3
|
9月前
|
关系型数据库 MySQL 数据库
|
11月前
|
Java 关系型数据库 MySQL
MySQL 分库分表方案
本文总结了数据库分库分表的相关概念和实践,针对单张表数据量过大及增长迅速的问题,介绍了垂直和水平切分的方式及其适用场景。文章分析了分库分表后可能面临的事务支持、多库结果集合并、跨库join等问题,并列举了几种常见的开源分库分表中间件。最后强调了不建议水平分库分表的原因,帮助读者在规划时规避潜在问题。
1114 20
|
11月前
|
关系型数据库 MySQL 中间件
MySQL 中如何实现分库分表?常见的分库分表策略有哪些?
在MySQL中,分库分表(Sharding)通过将数据分散到多个数据库或表中,以应对大量数据带来的性能和扩展性问题。常见策略包括:哈希分片(分布均匀,查询效率高)、范围分片(适合范围查询)、列表分片(适用于特定值查询)、复合分片(灵活性高)和动态分片(灵活应对负载变化)。每种策略各有优劣,需根据业务需求选择。常用工具如MyCAT、ShardingSphere和TDDL可简化实现过程。
|
存储 SQL 关系型数据库
一篇文章搞懂MySQL的分库分表,从拆分场景、目标评估、拆分方案、不停机迁移、一致性补偿等方面详细阐述MySQL数据库的分库分表方案
MySQL如何进行分库分表、数据迁移?从相关概念、使用场景、拆分方式、分表字段选择、数据一致性校验等角度阐述MySQL数据库的分库分表方案。
1842 15
一篇文章搞懂MySQL的分库分表,从拆分场景、目标评估、拆分方案、不停机迁移、一致性补偿等方面详细阐述MySQL数据库的分库分表方案
|
SQL 算法 Java
(二十六)MySQL分库篇:Sharding-Sphere分库分表框架的保姆级教学!
前面《MySQL主从原理篇》、《MySQL主从实践篇》两章中聊明白了MySQL主备读写分离、多主多写热备等方案,但如果这些高可用架构依旧无法满足业务规模,或业务增长的需要,此时就需要考虑选用分库分表架构。
5829 4
|
存储 SQL 关系型数据库
(二十一)MySQL之高并发大流量情况下海量数据分库分表的正确姿势
从最初开设《全解MySQL专栏》到现在,共计撰写了二十个大章节详细讲到了MySQL各方面的进阶技术点,从最初的数据库架构开始,到SQL执行流程、库表设计范式、索引机制与原理、事务与锁机制剖析、日志与内存详解、常用命令与高级特性、线上调优与故障排查.....,似乎涉及到了MySQL的方方面面。但到此为止就黔驴技穷了吗?答案并非如此,以《MySQL特性篇》为分割线,整个MySQL专栏从此会进入“高可用”阶段的分析,即从上篇之后会开启MySQL的新内容,主要讲述分布式、高可用、高性能方面的讲解。
939 1
|
算法 搜索推荐 NoSQL
面试题MySQL问题之分库分表后的富查询问题处理如何解决
面试题MySQL问题之分库分表后的富查询问题处理如何解决
153 3
|
算法 关系型数据库 MySQL
MySQL分库分表
【7月更文挑战第11天】分库分表策略涉及数据源、库和表的划分,如订单表可能分布于多层结构中。面试时,主键生成是关键点。自增主键在不分库分表时适用,但在分表场景下会导致冲突。例如,按`buyer_id % 2`分两张表,自增ID无法保证全局唯一。因此,需要全局唯一且能自增的ID,如雪花算法,兼顾性能和高并发需求。
163 1

推荐镜像

更多