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
    评论
Sharding-Proxy 是一个开源的分库分表中间件,它可以帮助应用程序实现无感知的分库分表操作。下面是一个简单的步骤来利用 Sharding-Proxy 进行分库分表: 1. 安装和配置 Sharding-Proxy:首先,你需要下载 Sharding-Proxy安装包,并解压到你的服务器上。然后,根据你的需求修改配置文件,配置数据源和分片规则等信息。 2. 创建数据库和表:在进行分库分表之前,你需要创建相应的数据库和表结构。你可以选择手动创建,或者使用 Sharding-Proxy 提供的自动建表功能。 3. 配置分片规则:在 Sharding-Proxy 的配置文件中,你需要定义分片规则,指定如何将数据分散到不同的数据库和表中。可以使用基于范围、哈希、精确等多种分片算法。 4. 连接到 Sharding-Proxy:在应用程序中,需要修改数据库连接信息,将原来连接数据库的地址改为连接 Sharding-Proxy 的地址。这样应用程序就可以通过 Sharding-Proxy 访问分片后的数据。 5. 进行分库分表操作:现在你可以在应用程序中执行正常的数据库操作,而无需关心具体的分库分表细节。Sharding-Proxy 会根据配置的规则自动将数据路由到正确的库和表中。 需要注意的是,使用 Sharding-Proxy 进行分库分表操作需要仔细考虑数据一致性、事务处理、跨库查询等问题。在配置和使用过程中,建议参考官方文档和示例来确保正确性和性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值