- 求職 : Linux運維
- 論壇徽章:
- 203
|
kingshard(https://github.com/flike/kingshard)是一個由Go開發(fā)高性能MySQL Proxy項目,kingshard在滿足基本的讀寫分離的功能上,致力于簡化MySQL分庫分表操作;能夠讓DBA通過kingshard輕松平滑地實現(xiàn)MySQL數(shù)據(jù)庫擴容。
主要功能:
1.讀寫分離。
2.跨節(jié)點分表。
3.客戶端IP訪問控制。
4.平滑上線DB或下線DB,前端應(yīng)用無感知。
kingshard sharding介紹
現(xiàn)在開源的MySQL Proxy已經(jīng)有幾款了,并且有的已經(jīng)在生產(chǎn)環(huán)境上廣泛應(yīng)用。但這些proxy在sharding方面,都是不能分子表的。也就是說一個node節(jié)點只能分一張表。但我們的線上需求通常是這樣的:
我有一張非常大的表,行數(shù)超過十億,需要進行拆分處理。假設(shè)拆分因子是512。
如果采用單node單數(shù)據(jù)庫的分表方式,那其實這512個子表還是存在一個物理節(jié)點上,意義不大。
如果采用他們的sharding功能,就需要512個物理節(jié)點,也不現(xiàn)實。
面對這種需求,現(xiàn)有的proxy就不能很好地滿足要求了。通常我們希望將512張子表均分在幾個MySQL節(jié)點上,從而達到系統(tǒng)的橫向擴展。
然而kingshard較好地實現(xiàn)了這種典型的需求。簡單來說,kingshard的分表方案采用兩級映射的方式:
1.kingshard將該表分成512張子表,例如:test_0000,test_0001,...
test_511。
2.將shardKey通過hash或range方式定位到其要操作的記錄在哪張子表上。
3.子表落在哪個node上通過配置文件設(shè)置。
sharding支持的操作
目前kingshard sharding支持insert, delete, select, update和replace語句, 所有這五類操作都支持跨子表。但寫操作僅支持單node上的跨子表,select操作則可以跨node,跨子表。
sharding方式
range方式
基于整數(shù)范圍劃分來得到子表下標。該方式的優(yōu)點:基于范圍的查詢或更新速度快,因為查詢(或更新)的范圍有可能落在同一張子表中。這樣可以避免全部子表的查詢(更新)。缺點:數(shù)據(jù)熱點問題。因為在一段時間內(nèi)整個集群的寫壓力都會落在一張子表上。此時整個mysql集群的寫能力受限與單臺mysql server的性能。并且,當正在集中寫的mysql 節(jié)點如果宕機的話,整個mysql集群處于不可寫狀態(tài);趓ange方式的分表字段類型受限。
hash方式
kingshard采用(shardKey%子表個數(shù))的方式得到子表下標。優(yōu)點:數(shù)據(jù)分布均勻,寫壓力會比較平均地落在后端的每個MySQL節(jié)點上,整個集群的寫性能不會受限于單個MySQL節(jié)點。并且當某個分片節(jié)點宕機,只會影響到寫入該節(jié)點的請求,其他節(jié)點的寫入請求不受影響。分表字段類型不受限。因為任何一個類型的分表字段,都可以通過一個hash函數(shù)計算得到一個整數(shù)。缺點:基于范圍的查詢或更新,都需要將請求發(fā)送到全部子表,對性能有一定影響。但如果不是基于范圍的查詢或更新,則性能不會受到影響。
sharding相關(guān)的配置介紹
在配置文件中,有關(guān)sharding設(shè)置是通過scheam設(shè)置:
```
schemas :
db : kingshard
nodes: [node1,node2]
rules:
default: node1
shard:
-
#分表名字
table: test_shard_hash
#sharding key
key: id
#子表分布的節(jié)點名字
nodes: [node1, node2]
#sharding類型
type: hash
#子表個數(shù)分布,表示[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
#子表分布的節(jié)點名字
nodes: [node1, node2]
#子表個數(shù)分布,表示[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]
#每張子表的記錄數(shù)。[0,10000)在test_shard_hash_0000上,[10000,20000)在test_shard_hash_0001上。....
table_row_limit: 10000
```
一個kingshard實例只能有一個schemas,從上面的配置可以看出,schema可以分為三個部分:
1.db,表示這個schemas使用的數(shù)據(jù)庫。
2.nodes,表示子表分布的節(jié)點名字。
3.rules,sharding規(guī)則。其中rules又可以分為兩個部分:
- default,默認分表規(guī)則。所有操作不在shard(default規(guī)則下面的規(guī)則)中的表的SQL語句都會發(fā)向該node。
- hash,hash分表方式。
- range,range分表方式
kingshard架構(gòu)圖
基于kingshard的子表遷移方案
通過kingshard可以非常方便地動態(tài)遷移子表,從而保證MySQL節(jié)點的不至于負載壓力太大。大致步驟如下所述:
通過自動數(shù)據(jù)遷移工具開始數(shù)據(jù)遷移。
數(shù)據(jù)差異小于某一臨界值,阻塞老子表寫操作(read-only)
等待新子表數(shù)據(jù)同步完畢
更改kingshard配置文件中的對應(yīng)子表的路由規(guī)則。
刪除老節(jié)點上的子表。
Exaple
簡單演示一下kingshard的相關(guān)操作,感興趣的同學(xué)可以自己試一試。
#啟動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)
反饋
目前kingshard還是1.0版本,比較核心的功能已經(jīng)實現(xiàn)了。但還有很多地方不完善。如果您在使用kingshard的過程中發(fā)現(xiàn)BUG或者有新的功能需求,非常歡迎您發(fā)郵件至flikecn#126.com與作者取得聯(lián)系,或者加入QQ群(147926796)交流。
github:https://github.com/flike/kingshard |
|