Sharding-Proxy之安装以及水平分表

一、下载 Sharding-Proxy

1、下载地址

        Index of /dist/shardingsphere/5.0.0

 这里我们选择的是5.0.0的Linux安装版本

2、将sharding-proxy安装包上传到Linux服务器

 3、解压安装包

        tar -zxvf apache-shardingsphere-5.0.0-shardingsphere-proxy-bin.tar.gz

  mv apache-shardingsphere-5.0.0-shardingsphere-proxy-bin shardingsphere-proxy-5.0.0       

 


二、配置 Sharding-Proxy

1、修改 /home/dhapp/software/shardingsphere-proxy-5.0.0/conf/server.yaml 文件(此文件为Sharding-Proxy的配置),去除掉rules和props的注释

rules:
  - !AUTHORITY
    users:
      - dhapp@%:xxxxxx
      - sharding@:sharding
    provider:
      type: ALL_PRIVILEGES_PERMITTED
  - !TRANSACTION
    defaultType: XA #支持 XA 事务
    providerType: Atomikos #默认的事务管理器为 Atomikos

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-opentracing-enabled: false
  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
  sql-comment-parse-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
    # if client connections are more than proxy-frontend-netty-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

注意:

        上面的rules用于执行登录 Sharding Proxy 的权限验证。 配置用户名、密码、可访问的数据库后,必须使用正确的用户名、密码才可登录。

 2、修改 config-sharding.yaml 配置文件(此文件为分库分表的配置)

        2.1、将mysql的驱动jar拷贝到/home/dhapp/software/shardingsphere-proxy-5.0.0/lib/目录下,如下在config-sharding.yaml中以及提示了

 下面开始在config-sharding.yaml中配置数据源:

注意:下面我们配置了一个数据源,并且对t_user表进行水平分表

schemaName: sharding_db #逻辑库名称

dataSources:
  ds_0:
    url: jdbc:mysql://192.168.56.20:3306/course_db?allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: Tz@202011
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
    minPoolSize: 1

rules:
- !SHARDING
  tables:
    t_user:
      actualDataNodes: ds_${0}.t_user_${0..1}
      tableStrategy:
        standard:
          shardingColumn: user_id #定义分表的字段
          shardingAlgorithmName: t_user_inline
      keyGenerateStrategy:
        column: user_id #定义主键生成的算法策略
        keyGeneratorName: snowflake
  bindingTables:
    - t_user
  defaultDatabaseStrategy:
    standard:
      shardingColumn: user_id
      shardingAlgorithmName: database_inline  
  defaultTableStrategy:
    none:

  shardingAlgorithms:
    database_inline:
      type: INLINE
      props:
        algorithm-expression: ds_${0}
    t_user_inline:
      type: INLINE
      props:
        algorithm-expression: t_user_${user_id % 2}

  keyGenerators:
    snowflake:
      type: snowflake

三、启动 Sharding-Proxy 服务

1、进入/home/dhapp/software/shardingsphere-proxy-5.0.0/bin/目录下启动sharding-proxy

        sh ./start.sh

 2、连接sharding-proxy

        注意:空格然后加上端口号,可以指定端口,默认3307,例如:sh ./start.sh 3308

        mysql -h192.168.56.20 -udhapp -P3307 -p

 

四、上面标示连接成功,下面进行测试

1、新建一张表,并插入一条数据

use sharding_db;

create table t_user(
	`user_id` bigint(20) primary key,
	`username` varchar(100) not null,
	`ustatus` varchar(50) not null
);

insert into t_user(`user_id`,`username`,`ustatus`)values(11,'张三','test');

查看数据库:

 

 可以发现在mysql中自动创建了t_user_0和t_user_1两张表

我们是根据user_id分表,11应该分在t_user_1表中,我们看一下:

 

由上可以发现,完全正确,没问题! 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值