ShardingSphere搭建MySQL分庫分表

參考:https://blog.csdn.net/tonghu_note/article/details/124578688
http://www.reibang.com/p/0157b86cd46c
https://blog.csdn.net/weixin_45892228/article/details/127629929

下載shardingsphere

wget  https://archive.apache.org/dist/shardingsphere/5.1.2/apache-shardingsphere-5.1.2-shardingsphere-proxy-bin.tar.gz
tar -zxvf apache-shardingsphere-5.1.2-shardingsphere-proxy-bin.tar.gz
mv apache-shardingsphere-5.1.2-shardingsphere-proxy-bin /data/software/
cd /data/software/apache-shardingsphere-5.1.2-shardingsphere-proxy-bin/conf

修改配置:
vi config-sharding.yaml

databaseName: sharding_db

dataSources:
 ds_0:
   url: jdbc:mysql://172.22.3.217:3306/demo_ds_0?serverTimezone=UTC&useSSL=false
   username: hzero
   password: Q4E%kizUy2#m
   connectionTimeoutMilliseconds: 30000
   idleTimeoutMilliseconds: 60000
   maxLifetimeMilliseconds: 1800000
   maxPoolSize: 50
   minPoolSize: 1
 ds_1:
   url: jdbc:mysql://172.22.3.217:3306/demo_ds_1?serverTimezone=UTC&useSSL=false
   username: hzero
   password: Q4E%kizUy2#m
   connectionTimeoutMilliseconds: 30000
   idleTimeoutMilliseconds: 60000
   maxLifetimeMilliseconds: 1800000
   maxPoolSize: 50
   minPoolSize: 1

rules:
- !SHARDING
 tables:
   t_order:
     actualDataNodes: ds_${0..1}.t_order_${0..1}
     tableStrategy:
       standard:
         shardingColumn: order_id
         shardingAlgorithmName: t_order_inline
     keyGenerateStrategy:
       column: order_id
       keyGeneratorName: snowflake
   t_order_item:
     actualDataNodes: ds_${0..1}.t_order_item_${0..1}
     tableStrategy:
       standard:
         shardingColumn: order_id
         shardingAlgorithmName: t_order_item_inline
     keyGenerateStrategy:
       column: order_item_id
       keyGeneratorName: snowflake
 bindingTables:
   - t_order,t_order_item
 defaultDatabaseStrategy:
   standard:
     shardingColumn: user_id
     shardingAlgorithmName: database_inline
 defaultTableStrategy:
   none:

 shardingAlgorithms:
   database_inline:
     type: INLINE
     props:
       algorithm-expression: ds_${user_id % 2}
   t_order_inline:
     type: INLINE
     props:
       algorithm-expression: t_order_${order_id % 2}
   t_order_item_inline:
     type: INLINE
     props:
       algorithm-expression: t_order_item_${order_id % 2}

 keyGenerators:
   snowflake:
     type: SNOWFLAKE

 scalingName: default_scaling
 scaling:
   default_scaling:
     input:
       workerThread: 40
       batchSize: 1000
     output:
       workerThread: 40
       batchSize: 1000
     streamChannel:
       type: MEMORY
       props:
         block-queue-size: 10000
     completionDetector:
       type: IDLE
       props:
         incremental-task-idle-seconds-threshold: 1800
     dataConsistencyChecker:
       type: DATA_MATCH
       props:
         chunk-size: 1000

vi server.yaml

mode:
  type: Standalone
  repository:
    type: File
    props:
      path: /data/software/apache-shardingsphere-5.1.2-shardingsphere-proxy-bin/repo_pg
  overwrite: false #追加
#注意:users為用于登錄計(jì)算節(jié)點(diǎn)的用戶名,授權(quán)主機(jī)和密碼的組合坑夯。格式:<username>@<hostname>:<password>除呵,hostname 為 % 或空字符串表示不限制授權(quán)主機(jī)
rules:
 - !AUTHORITY
   users:
     - root@%:root
     - sharding@:sharding
   provider:
     type: ALL_PERMITTED
 - !TRANSACTION
   defaultType: XA
   providerType: Atomikos
   # # When the provider type is Narayana, the following properties can be configured or not
   # props:
   #   recoveryStoreUrl: jdbc:mysql://127.0.0.1:3306/jbossts
   #   recoveryStoreDataSource: com.mysql.jdbc.jdbc2.optional.MysqlDataSource
   #   recoveryStoreUser: root
   #   recoveryStorePassword: 12345678
 - !SQL_PARSER
   sqlCommentParseEnabled: true
   sqlStatementCache:
     initialCapacity: 2000
     maximumSize: 65535
   parseTreeCache:
     initialCapacity: 128
     maximumSize: 1024

props:
 max-connections-size-per-query: 1
 kernel-executor-size: 16  # Infinite by default.
 proxy-frontend-flush-threshold: 128  # The default value is 128.
 proxy-hint-enabled: false
 sql-show: false
 check-table-metadata-enabled: false
 show-process-list-enabled: false
   # Proxy backend query fetch size. A larger value may increase the memory usage of ShardingSphere Proxy.
   # The default value is -1, which means set the minimum value for different JDBC drivers.
 proxy-backend-query-fetch-size: -1
 check-duplicate-table-enabled: false
 proxy-frontend-executor-size: 0 # Proxy frontend executor size. The default value is 0, which means let Netty decide.
   # Available options of proxy backend executor suitable: OLAP(default), OLTP. The OLTP option may reduce time cost of writing packets to client, but it may increase the latency of SQL execution
   # and block other clients if client connections are more than `proxy-frontend-executor-size`, especially executing slow SQL.
 proxy-backend-executor-suitable: OLAP
 proxy-frontend-max-connections: 0 # Less than or equal to 0 means no limitation.
 sql-federation-enabled: false
   # Available proxy backend driver type: JDBC (default), ExperimentalVertx
 proxy-backend-driver-type: JDBC
 proxy-mysql-default-version: 5.7.22 # In the absence of schema name, the default version will be used.
 proxy-default-port: 3307 # Proxy default port.
 proxy-netty-backlog: 1024 # Proxy netty backlog.

啟動(dòng)兆旬、查看日志

https://cdn.mysql.com/archives/mysql-connector-java-8.0/mysql-connector-j-8.0.32.zip
#下載mysql驅(qū)動(dòng)到該目錄下: 
cd /data/software/apache-shardingsphere-5.1.0-shardingsphere-proxy-bin/lib
#啟動(dòng)
 ../bin/start.sh 3308
#停用
 ../bin/stop.sh 3308
ps -ef|grep shard
tail -f -n 1000 stdout.log 

連接鹦赎、創(chuàng)建表

mysql -h 192.168.16.144 -P 3308 -u root -proot sharding_db
image.png

創(chuàng)建表、數(shù)據(jù)

create table t_order(user_id int, order_id int primary key);
insert into t_order values(1, 1);
insert into t_order values(2, 1);
insert into t_order values(1, 2);
insert into t_order values(2, 2);
insert into t_order values(1, 3);
insert into t_order values(2, 3);
image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末其爵,一起剝皮案震驚了整個(gè)濱河市咖耘,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌蜀踏,老刑警劉巖维蒙,帶你破解...
    沈念sama閱讀 219,539評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異果覆,居然都是意外死亡颅痊,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,594評(píng)論 3 396
  • 文/潘曉璐 我一進(jìn)店門局待,熙熙樓的掌柜王于貴愁眉苦臉地迎上來斑响,“玉大人,你說我怎么就攤上這事钳榨〗⒎#” “怎么了?”我有些...
    開封第一講書人閱讀 165,871評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵重绷,是天一觀的道長沸停。 經(jīng)常有香客問我,道長昭卓,這世上最難降的妖魔是什么愤钾? 我笑而不...
    開封第一講書人閱讀 58,963評(píng)論 1 295
  • 正文 為了忘掉前任瘟滨,我火速辦了婚禮,結(jié)果婚禮上能颁,老公的妹妹穿的比我還像新娘杂瘸。我一直安慰自己,他們只是感情好伙菊,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,984評(píng)論 6 393
  • 文/花漫 我一把揭開白布败玉。 她就那樣靜靜地躺著,像睡著了一般镜硕。 火紅的嫁衣襯著肌膚如雪运翼。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,763評(píng)論 1 307
  • 那天兴枯,我揣著相機(jī)與錄音血淌,去河邊找鬼。 笑死财剖,一個(gè)胖子當(dāng)著我的面吹牛悠夯,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播躺坟,決...
    沈念sama閱讀 40,468評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼沦补,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了咪橙?” 一聲冷哼從身側(cè)響起夕膀,我...
    開封第一講書人閱讀 39,357評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎匣摘,沒想到半個(gè)月后店诗,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,850評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡音榜,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,002評(píng)論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了捧弃。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片赠叼。...
    茶點(diǎn)故事閱讀 40,144評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖违霞,靈堂內(nèi)的尸體忽然破棺而出嘴办,到底是詐尸還是另有隱情,我是刑警寧澤买鸽,帶...
    沈念sama閱讀 35,823評(píng)論 5 346
  • 正文 年R本政府宣布涧郊,位于F島的核電站,受9級(jí)特大地震影響眼五,放射性物質(zhì)發(fā)生泄漏妆艘。R本人自食惡果不足惜彤灶,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,483評(píng)論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望批旺。 院中可真熱鬧幌陕,春花似錦、人聲如沸汽煮。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,026評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽暇赤。三九已至心例,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間鞋囊,已是汗流浹背止后。 一陣腳步聲響...
    開封第一講書人閱讀 33,150評(píng)論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留失暴,地道東北人坯门。 一個(gè)月前我還...
    沈念sama閱讀 48,415評(píng)論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像逗扒,于是被迫代替她去往敵國和親古戴。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,092評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容