sharding


官方文档

sharding-jdbc

总结

☆、日志里面会打印查询什么库,什么表。
在这里插入图片描述
☆、表的主键在配置文件中指定生成策略,项目里面指定的是SNOWFLAKE(雪花算法生成)。
☆、水平分库插入数据、查询数据时用到的表、库分片策略的字段都不能为空,否则会报错。
☆、sharding垂直分库感觉没什么用,多此一举。
☆、公共表:在配置的数据源里面都加上一张相同的表,当程序里面操作这张表的数据时,三个数据源的数据都会改变。

配置从服务器补充

(配置从库失败,后面的章节没有跟着做)
☆、windows配置mysql从服务器,要以管理员身份运行cmd,标准命令(注意空格):

mysqld install mysqls1 --defaults‐file="D:\mysql-s1\my.ini"

☆、创建主从复制的账号:用root账号也可以,但是最好创建一个专门的账号。
☆、命令行登录的操作都可以用navicat直接连,这一步直接用navicat连接上主库后执行2就行了。创建后在mysql库的user表里面会有这个账号。
db_sync是账号和密码。
在这里插入图片描述
☆、查看主服务器的状态:
为表3生成的bin.log文件是1,它的值是107。
在这里插入图片描述

sharding-proxy

总结

☆、是数据库的一个代理端,可以直接当做数据库使用。

配置

☆、修改配置文件server.yaml

authentication:
  users:
    #连接sharding-proxy的用户是root
    root:
      #连接sharding-proxy的密码是root
      password: root 
    sharding:
      password: sharding 
      #连接sharding-proxy的库是sharding_db
      authorizedSchemas: sharding_db
#连接的参数
props:
  max.connections.size.per.query: 1
  acceptor.size: 16  # The default value is available processors count * 2.
  executor.size: 16  # Infinite by default.
  proxy.frontend.flush.threshold: 128  # The default value is 128.
    # LOCAL: Proxy will run with LOCAL transaction.
    # XA: Proxy will run with XA transaction.
    # BASE: Proxy will run with B.A.S.E transaction.
  proxy.transaction.type: LOCAL
  proxy.opentracing.enabled: false
  query.with.cipher.column: true
  sql.show: false

☆、修改配置文件config-sharding.yaml
1.首先把mysql的连接驱动jar包复制sharding-proxybin目录下(在自己的maven仓库里面)。
2.

#数据库代理端的名字
schemaName: sharding_db
#数据源
dataSources:
  ds_0:
    url: jdbc:mysql://127.0.0.1:3306/edu_db_1?serverTimezone=UTC&useSSL=false
    username: root
    password: root
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
  ds_1:
    url: jdbc:mysql://127.0.0.1:3306/edu_db_2?serverTimezone=UTC&useSSL=false
    username: root
    password: root
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
#分库分表的规则
shardingRule:
  tables:
    t_order:
      actualDataNodes: ds_${0..1}.t_order_${1..2}
      tableStrategy:
        inline:
          shardingColumn: order_id
          algorithmExpression: t_order_${order_id % 2 + 1}
      keyGenerator:
        type: SNOWFLAKE
        column: order_id
  bindingTables:
    - t_order
  defaultDatabaseStrategy:
    inline:
      shardingColumn: user_id
      algorithmExpression: ds_${user_id % 2}
  defaultTableStrategy:
    none:

使用

☆、启动默认端口号 是3307,想要以其它端口启动:

start.bat 3308

☆、连接
1.用cmd连接,用工具连接可能连不上,有版本要求。

问题

水平分库、分表后分页怎么弄
事务
https://www.jianshu.com/p/20c0d4114632

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值