欢迎来到徐庆高(Tea)的个人博客网站
磨难很爱我,一度将我连根拔起。从惊慌失措到心力交瘁,我孤身一人,但并不孤独无依。依赖那些依赖我的人,信任那些信任我的人,帮助那些给予我帮助的人。如果我愿意,可以分裂成无数面镜子,让他们看见我,就像看见自己。察言观色和模仿学习是我的领域。像每个深受创伤的人那样,最终,我学会了随遇而安。
当前位置: 日志文章 > 详细内容

Sharding-Proxy分库分表的实现方案

2025年08月07日 ar
一 安装:https://www.jb51.net/article/237937.htm二资源rolemaster1slave1slave2sharding三为什么分库分表业务越来越大,数据库扛不住啊

一 安装:

https://www.jb51.net/article/237937.htm

二资源

  • role
  • master1
  • slave1
  • slave2
  • sharding

三为什么分库分表

业务越来越大,数据库扛不住啊。

扛不住会怎样呢?数据库性能降低

解决方法:

1 增加mysql硬件资源

2 历史数据归档

3 增加数据库—— 读写分离

4 增加数据库—— 分库分表

5 云数据库tidb

四如何分库分表

1 垂直拆分 -- 微服务化 -- 一个服务一个库

1) 每个库(表)的数据结构一般不同
2) 每个库(表)至少有一个列一样—— 为了join

缺点:
1)数据不均衡。

2 水平拆分--分库分表 -- 数据存的不一样

1)每个库(表)的数据结构都一样
2)双十一订单大,订单一定均分到012库中,解决数据热点问题

缺点:1)扩容的问题,麻烦

3 水平拆分和垂直拆分结合:垂直拆库,水平拆表

五sharding-proxy分库分表实战

1 master建库建表

库:
bookstore_0
bookstore_1
在库中分别建表:
bookinfo_0
bookinfo_1

2 修改config-sharding.yaml

①把mysql驱动jar包复制到lib文件夹下

②config-sharding.yaml

schemaname: bookstore

datasources:
  bookstore_0:
    url: jdbc:mysql://ip:3306/bookstore_0?servertimezone=utc&usessl=false
    username: root
    password: 
    connectiontimeoutmilliseconds: 30000
    idletimeoutmilliseconds: 60000
    maxlifetimemilliseconds: 1800000
    maxpoolsize: 50
  bookstore_1:
    url: jdbc:mysql://ip:3306/bookstore_0?servertimezone=utc&usessl=false
    username: root
    password: 
    connectiontimeoutmilliseconds: 30000
    idletimeoutmilliseconds: 60000
    maxlifetimemilliseconds: 1800000
    maxpoolsize: 50

shardingrule:
  tables:
    bookinfo:
      actualdatanodes: bookstore_${0..1}.bookinfo_${0..1}
      databasestrategy:
        inline:
          shardingcolumn: storeid
          algorithmexpression: bookstore_${storeid % 2}
      tablestrategy:
        inline:
          shardingcolumn: bookid
          algorithmexpression: bookinfo_${bookid % 2}
      #keygenerator:
        #type: snowflake
        #column: order_id

  bindingtables:
    - bookinfo
  defaultdatabasestrategy:
    inline:
      shardingcolumn: storeid
      algorithmexpression: bookstore_${storeid % 2}
  defaulttablestrategy:
    none:
  •  mysql -hip -uroot -p3307 -p     

4 进行sql命令操作,只能看到一个库

5 插入数据。sharding-proxy代理端里有数据了

6 连接真正的数据库

这里有个问题:
分库失败,全部写入bookstore_0
分表成功

到此这篇关于sharding-proxy分库分表的文章就介绍到这了,更多相关sharding-proxy分库分表内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!