kingshard--一个支持sharding的MySQL Proxy项目
kingshard簡介
kingshard(https://github.com/flike/king...)是一個由Go開發高性能MySQL Proxy項目,kingshard在滿足基本的讀寫分離的功能上,致力于簡化MySQL分庫分表操作;能夠讓DBA通過kingshard輕松平滑地實現MySQL數據庫擴容。
主要功能:
1.讀寫分離。 2.跨節點分表。 3.客戶端IP訪問控制。 4.平滑上線DB或下線DB,前端應用無感知。kingshard sharding介紹
現在開源的MySQL Proxy已經有幾款了,并且有的已經在生產環境上廣泛應用。但這些proxy在sharding方面,都是不能分子表的。也就是說一個node節點只能分一張表。但我們的線上需求通常是這樣的:
**我有一張非常大的表,行數超過十億,需要進行拆分處理。假設拆分因子是512。
如果采用單node單數據庫的分表方式,那其實這512個子表還是存在一個物理節點上,意義不大。
然而kingshard較好地實現了這種典型的需求。簡單來說,kingshard的分表方案采用兩級映射的方式:
1.kingshard將該表分成512張子表,例如:test_0000,test_0001,... test_511。 2.將shardKey通過hash或range方式定位到其要操作的記錄在哪張子表上。 3.子表落在哪個node上通過配置文件設置。sharding支持的操作
目前kingshard sharding支持insert, delete, select, update和replace語句, 所有這五類操作都支持跨子表。但寫操作僅支持單node上的跨子表,select操作則可以跨node,跨子表。
sharding方式
range方式
基于整數范圍劃分來得到子表下標。該方式的優點:基于范圍的查詢或更新速度快,因為查詢(或更新)的范圍有可能落在同一張子表中。這樣可以避免全部子表的查詢(更新)。缺點:數據熱點問題。因為在一段時間內整個集群的寫壓力都會落在一張子表上。此時整個mysql集群的寫能力受限與單臺mysql server的性能。并且,當正在集中寫的mysql 節點如果宕機的話,整個mysql集群處于不可寫狀態。基于range方式的分表字段類型受限。
hash方式
kingshard采用(shardKey%子表個數)的方式得到子表下標。優點:數據分布均勻,寫壓力會比較平均地落在后端的每個MySQL節點上,整個集群的寫性能不會受限于單個MySQL節點。并且當某個分片節點宕機,只會影響到寫入該節點的請求,其他節點的寫入請求不受影響。分表字段類型不受限。因為任何一個類型的分表字段,都可以通過一個hash函數計算得到一個整數。缺點:基于范圍的查詢或更新,都需要將請求發送到全部子表,對性能有一定影響。但如果不是基于范圍的查詢或更新,則性能不會受到影響。
sharding相關的配置介紹
在配置文件中,有關sharding設置是通過scheam設置:
schemas : -db : kingshardnodes: [node1,node2]rules:default: node1shard:- #分表名字table: test_shard_hash#sharding keykey: 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 keykey: 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架構圖
基于kingshard的子表遷移方案
通過kingshard可以非常方便地動態遷移子表,從而保證MySQL節點的不至于負載壓力太大。大致步驟如下所述:
Exaple
簡單演示一下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 HomebrewCopyright (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的最新消息與后端架構設計類的文章,都會在這個公眾號分享。
總結
以上是生活随笔為你收集整理的kingshard--一个支持sharding的MySQL Proxy项目的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Apache CXF实现WebServi
- 下一篇: [剑指offer]8.重建二叉树