8、ShardingSphere 之 Sharding-Proxy 实现分库分表
发布日期:2021-05-06 17:44:39 浏览次数:23 分类:原创文章

本文共 2062 字,大约阅读时间需要 6 分钟。

文章目录

1 创建两个数据库edudb1、edudb2

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

schemaName: sharding_dbdataSources:  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: 50shardingRule:  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;

在这里插入图片描述

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

在这里插入图片描述

上一篇:9、ShardingSphere 之 Sharding-Proxy 读写分离
下一篇:7、ShardingSphere 之 Sharding-Proxy

发表评论

最新留言

很好
[***.229.124.182]2025年03月28日 06时14分37秒