8、ShardingSphere 之 Sharding-Proxy 实现分库分表

文章目录

      • 1 创建两个数据库edudb1、edudb2
      • 2 进入conf目录配置conf-sharding.yaml
      • 3 启动Sharding-Proxy服务
      • 4 通过Navicat for MySQL 连接sharding-proxy 服务端
        • 4.1 查看数据库
        • 4.2 向数据库sharding_db中创建表
        • 4.3 向t_order表中添加数据
        • 4.4 查看添加的结果
      • 5 打开本地3306数据库,发现已经在对应的分库创建好了对应的分表并添加好了数据

1 创建两个数据库edudb1、edudb2

2 进入conf目录配置conf-sharding.yaml

schemaName: sharding_db

dataSources:
  ds_0:
    url: jdbc:mysql://127.0.0.1:3306/edudb1?serverTimezone=UTC&useSSL=false
    username: root
    password: chengwen
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
  ds_1:
    url: jdbc:mysql://127.0.0.1:3306/edudb2?serverTimezone=UTC&useSSL=false
    username: root
    password:
    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:

3 启动Sharding-Proxy服务

进入sharding-proxy的bin目录启动服务

chengwen@Mchengwen bin % ./start.sh   
Starting the Sharding-Proxy ...
Please check the STDOUT file: /Users/chengwen/apache-shardingsphere-incubating-4.0.1-sharding-proxy-bin/logs/stdout.log

启动日志出现端口号3307 和ACTIVE表示成功

[INFO ] 11:10:49.004 [nioEventLoopGroup-2-1] i.n.handler.logging.LoggingHandler - [id: 0x4ebfb303] REGISTERED
[INFO ] 11:10:49.006 [nioEventLoopGroup-2-1] i.n.handler.logging.LoggingHandler - [id: 0x4ebfb303] BIND: 0.0.0.0/0.0.0.0:3307
[INFO ] 11:10:49.008 [nioEventLoopGroup-2-1] i.n.handler.logging.LoggingHandler - [id: 0x4ebfb303, L:/0.0.0.0:3307] ACTIVE

4 通过Navicat for MySQL 连接sharding-proxy 服务端

4.1 查看数据库

show DATABASES;
sharding_db

4.2 向数据库sharding_db中创建表

create table if not exists ds_0.t_order(
order_id bigint not null,
user_id int not null,
status varchar(50),
primary key(order_id)
);

4.3 向t_order表中添加数据

insert into t_order(order_id,user_id,status) values (11,1,'init');

4.4 查看添加的结果

select * from t_order;

8、ShardingSphere 之 Sharding-Proxy 实现分库分表_第1张图片

5 打开本地3306数据库,发现已经在对应的分库创建好了对应的分表并添加好了数据

你可能感兴趣的:(ShardingSphere,Sharding-Proxy)