Sharding-Proxy 分库配置

x33g5p2x  于2021-11-12 转载在 其他  
字(1.7k)|赞(0)|评价(0)|浏览(240)

一 创建两个数据库 

edu_db_1

edu_db_2

二 配置 config-sharding.yaml

schemaName: sharding_db

dataSources:
  ds_0:
    url: jdbc:mysql://127.0.0.1:3306/edu_db_1?serverTimezone=UTC&useSSL=false
    username: root
    password: Mima123456
    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: Mima123456
    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:

三 启动 Sharding-Proxy 服务

四 打开 cmd ,连接 Sharding-Proxy 服务

C:\Users\chengqiuming>mysql -P3307 -uroot -p
Enter password: **********
ERROR 1045 (28000): Access denied for user 'root'@'0:0:0:0:0:0:0:1' (using password: YES)

C:\Users\chengqiuming>mysql -P3307 -uroot -p
Enter password: **********
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.4-Sharding-Proxy 4.0.0

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

五 创建数据库表

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

此时生成的数据表如下。

 六 插入一条记录

mysql> insert into t_order(order_id,user_id,status) values(11,1,'init');
Query OK, 1 row affected (0.15 sec)

插入数据如下

相关文章