kingshard(https://github.com/flike/king...)是一个由Go开发高性能MySQL Proxy项目,kingshard在知足基本的读写分离的功能上,致力于简化MySQL分库分表操做;可以让DBA经过kingshard轻松平滑地实现MySQL数据库扩容。前端
1.读写分离。 2.跨节点分表。 3.客户端IP访问控制。 4.平滑上线DB或下线DB,前端应用无感知。
如今开源的MySQL Proxy已经有几款了,而且有的已经在生产环境上普遍应用。但这些proxy在sharding方面,都是不能分子表的。也就是说一个node节点只能分一张表。但咱们的线上需求一般是这样的:node
**我有一张很是大的表,行数超过十亿,须要进行拆分处理。假设拆分因子是512。
若是采用单node单数据库的分表方式,那其实这512个子表仍是存在一个物理节点上,意义不大。mysql
若是采用他们的sharding功能,就须要512个物理节点,也不现实。 面对这种需求,现有的proxy就不能很好地知足要求了。一般咱们但愿将512张子表均分在几个MySQL节点上,从而达到系统的横向扩展。**
然而kingshard较好地实现了这种典型的需求。简单来讲,kingshard的分表方案采用两级映射的方式:git
1.kingshard将该表分红512张子表,例如:test_0000,test_0001,... test_511。 2.将shardKey经过hash或range方式定位到其要操做的记录在哪张子表上。 3.子表落在哪一个node上经过配置文件设置。
目前kingshard sharding支持insert, delete, select, update和replace语句, 全部这五类操做都支持跨子表。但写操做仅支持单node上的跨子表,select操做则能够跨node,跨子表。github
基于整数范围划分来获得子表下标。该方式的优势:基于范围的查询或更新速度快,由于查询(或更新)的范围有可能落在同一张子表中。这样能够避免所有子表的查询(更新)。缺点:数据热点问题。由于在一段时间内整个集群的写压力都会落在一张子表上。此时整个mysql集群的写能力受限与单台mysql server的性能。而且,当正在集中写的mysql 节点若是宕机的话,整个mysql集群处于不可写状态。基于range方式的分表字段类型受限。sql
kingshard采用(shardKey%子表个数)的方式获得子表下标。优势:数据分布均匀,写压力会比较平均地落在后端的每一个MySQL节点上,整个集群的写性能不会受限于单个MySQL节点。而且当某个分片节点宕机,只会影响到写入该节点的请求,其余节点的写入请求不受影响。分表字段类型不受限。由于任何一个类型的分表字段,均可以经过一个hash函数计算获得一个整数。缺点:基于范围的查询或更新,都须要将请求发送到所有子表,对性能有必定影响。但若是不是基于范围的查询或更新,则性能不会受到影响。数据库
在配置文件中,有关sharding设置是经过scheam设置:后端
schemas : - db : kingshard nodes: [node1,node2] rules: default: node1 shard: - #分表名字 table: test_shard_hash #sharding key key: id #子表分布的节点名字 nodes: [node1, node2] #sharding类型 type: hash #子表个数分布,表示[test_shard_hash_0000, test_shard_hash_0001, test_shard_hash_0002, test_shard_hash_003]在node1上。 #[test_shard_hash_0004, test_shard_hash_0005, test_shard_hash_0006, test_shard_hash_007]在node2上 locations: [4,4] - #分表名字 table: test_shard_range #sharding key key: id #sharding类型 type: range #子表分布的节点名字 nodes: [node1, node2] #子表个数分布,表示[test_shard_hash_0000, test_shard_hash_0001, test_shard_hash_0002, test_shard_hash_003]在node1上。 #[test_shard_hash_0004, test_shard_hash_0005, test_shard_hash_0006, test_shard_hash_007]在node2上 locations: [4,4] #每张子表的记录数。[0,10000)在test_shard_hash_0000上,[10000,20000)在test_shard_hash_0001上。.... table_row_limit: 10000
一个kingshard实例只能有一个schemas,从上面的配置能够看出,schema能够分为三个部分:架构
1.db,表示这个schemas使用的数据库。 2.nodes,表示子表分布的节点名字。 3.rules,sharding规则。其中rules又能够分为两个部分: - default,默认分表规则。全部操做不在shard(default规则下面的规则)中的表的SQL语句都会发向该node。 - hash,hash分表方式。 - range,range分表方式
经过kingshard能够很是方便地动态迁移子表,从而保证MySQL节点的不至于负载压力太大。大体步骤以下所述:tcp
简单演示一下kingshard的相关操做,感兴趣的同窗能够本身试一试。:)
#启动kingshard kingshard git:(master) ✗ ./bin/kingshard -config=etc/multi.yaml kingshard 2015/07/19 11:13:43 - INFO - server.go:[205] - [server] "NewServer" "Server running" "netProto=tcp|address=127.0.0.1:9696" conn_id=0 #另外一个终端链接kingshard mysql -ukingshard -pkingshard -h127.0.0.1 -P9696; 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 10001 Server version: kingshard-1.0 Homebrew Copyright (c) 2000, 2014, 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>use kingshard; Database changed mysql> select/*master*/ * from kingshard_test_conn; +----+----------+------+-------+------+------+ | id | str | f | e | u | i | +----+----------+------+-------+------+------+ | 1 | a | 3.14 | test1 | NULL | NULL | | 5 | ""''\abc | NULL | NULL | NULL | NULL | | 6 | 中国 | NULL | NULL | NULL | NULL | +----+----------+------+-------+------+------+ 3 rows in set (0.01 sec) mysql> select * from test_shard_hash where id in(6,10); +----+-------+------+-------+------+------+ | id | str | f | e | u | i | +----+-------+------+-------+------+------+ | 10 | world | 2.1 | test1 | 1 | 1 | +----+-------+------+-------+------+------+ 1 row in set (0.03 sec) mysql> show tables; +----------------------------+ | Tables_in_kingshard | +----------------------------+ | kingshard_test_conn | | kingshard_test_proxy_conn | | kingshard_test_proxy_stmt | | kingshard_test_shard_hash | | kingshard_test_shard_range | | kingshard_test_stmt | | test_shard_hash_0000 | | test_shard_hash_0001 | | test_shard_hash_0002 | | test_shard_hash_0003 | | test_shard_range_0000 | | test_shard_range_0001 | | test_shard_range_0002 | | test_shard_range_0003 | +----------------------------+ 14 rows in set (0.00 sec)
很是欢迎您发邮件至flikecn#126.com与做者取得联系,或者加入QQ群(147926796)交流。
github:https://github.com/flike/king...
欢迎关注后端技术快讯公众号,有关kingshard的最新消息与后端架构设计类的文章,都会在这个公众号分享。