Sharding-Proxy分库分表

Sharding-Proxy分库分表

    • 1.下载
    • 2.准备两个数据库,两张表
    • 3.修改 conf文件夹下面的server.yaml 和 config-sharding.yaml
    • 4. 启动,进入bin目录
    • 5. 连接shardingSphere虚拟的数据库
    • 6.插入4条数据查看日志打印

1.下载

https://shardingsphere.apache.org/document/current/cn/downloads/
https://archive.apache.org/dist/shardingsphere/5.0.0-alpha/更多版本下载,建议使用5.0.0的alpha版本

2.准备两个数据库,两张表

-- 两个库
create schema demo_ds_0;  
create schema demo_ds_1;  
-- 4张表
create table demo_ds_0.t_order_0(order_id bigint not null auto_increment,user_id int not null,status varchar(50),primary key(order_id));  
create table demo_ds_0.t_order_1(order_id bigint not null auto_increment,user_id int not null,status varchar(50),primary key(order_id));  
create table demo_ds_1.t_order_0(order_id bigint not null auto_increment,user_id int not null,status varchar(50),primary key(order_id));  
create table demo_ds_1.t_order_1(order_id bigint not null auto_increment,user_id int not null,status varchar(50),primary key(order_id));  

3.修改 conf文件夹下面的server.yaml 和 config-sharding.yaml

– server.yaml

# 应用连接 Sharding-Proxy 时需要的账户和密码
authentication:
 users:
   root:
     password: root
   # sharding:
   #   password: sharding 
   #   authorizedSchemas: sharding_db

props:
 max-connections-size-per-query: 1
 acceptor-size: 16  # The default value is available processors count * 2.
 executor-size: 16  # Infinite by default.
 proxy-frontend-flush-threshold: 128  # The default value is 128.
   # LOCAL: Proxy will run with LOCAL transaction.
   # XA: Proxy will run with XA transaction.
   # BASE: Proxy will run with B.A.S.E transaction.
 proxy-transaction-type: LOCAL
 proxy-opentracing-enabled: false
 proxy-hint-enabled: false
 query-with-cipher-column: true
 sql-show: true
 check-table-metadata-enabled: false


– config-sharding.yaml

# 应用连接 Sharding-Proxy 的数据库名称
schemaName: sharding_db

dataSourceCommon:
 username: root
 password: root
 connectionTimeoutMilliseconds: 30000
 idleTimeoutMilliseconds: 60000
 maxLifetimeMilliseconds: 1800000
 maxPoolSize: 50
 minPoolSize: 1
 maintenanceIntervalMilliseconds: 30000

dataSources:
 ds_0:
   url: jdbc:mysql://127.0.0.1:3306/demo_ds_0?serverTimezone=UTC&useSSL=false
 ds_1:
   url: jdbc:mysql://127.0.0.1:3306/demo_ds_1?serverTimezone=UTC&useSSL=false

rules:
- !SHARDING
 tables:
   t_order:
     actualDataNodes: ds_${0..1}.t_order_${0..1}
     tableStrategy:
       standard:
         shardingColumn: order_id
         shardingAlgorithmName: t_order_inline
     keyGenerateStrategy:
       column: order_id
       keyGeneratorName: snowflake
   # t_order_item:
   #   actualDataNodes: ds_${0..1}.t_order_item_${0..1}
   #   tableStrategy:
   #     standard:
   #       shardingColumn: order_id
   #       shardingAlgorithmName: t_order_item_inline
     # keyGenerateStrategy:
     #   column: order_item_id
     #   keyGeneratorName: snowflake
 # bindingTables:
 #   - t_order,t_order_item
 defaultDatabaseStrategy:
   standard:
     shardingColumn: user_id
     shardingAlgorithmName: database_inline
 defaultTableStrategy:
   none:
 
 shardingAlgorithms:
   database_inline:
     type: INLINE
     props:
       algorithm-expression: ds_${user_id % 2}
   t_order_inline:
     type: INLINE
     props:
       algorithm-expression: t_order_${order_id % 2}
   # t_order_item_inline:
   #   type: INLINE
   #   props:
   #     algorithm-expression: t_order_item_${order_id % 2}
 
 keyGenerators:
   snowflake:
     type: SNOWFLAKE
     props:
       worker-id: 123


4. 启动,进入bin目录

windows命令:start.bat
linux命令:start.sh

D:\store\important\apache-shardingsphere-5.0.0-alpha-shardingsphere-proxy-bin\bin>start.bat
Starting the ShardingSphere-Proxy ...
[INFO ] 20:48:39.971 [main] ShardingSphere-metadata - Loading 0 tables' meta data for unconfigured tables.
[INFO ] 20:48:39.975 [main] ShardingSphere-metadata - Loading 0 tables' meta data for unconfigured tables.
[INFO ] 20:48:39.979 [main] ShardingSphere-metadata - Loading 2 tables' meta data for unconfigured tables.
[INFO ] 20:48:39.980 [main] ShardingSphere-metadata - Loading 2 tables' meta data for unconfigured tables.
[INFO ] 20:48:39.984 [main] o.a.s.i.c.s.SchemaContextsBuilder - Load meta data for schema sharding_db finished, cost 85 milliseconds.
Thanks for using Atomikos! Evaluate http://www.atomikos.com/Main/ExtremeTransactions for advanced features and professional support
or register at http://www.atomikos.com/Main/RegisterYourDownload to disable this message and receive FREE tips & advice
[INFO ] 20:48:40.071 [main] o.a.s.p.i.i.AbstractBootstrapInitializer - Database name is `MySQL`, version is `5.7.20-log`
[INFO ] 20:48:40.908 [main] o.a.s.p.frontend.ShardingSphereProxy - ShardingSphere-Proxy start success.

5. 连接shardingSphere虚拟的数据库

  -注意需要加 -A才可以正常执行sql命令,原因似乎是不加A会初始化元数据
  -默认端口号3307

# 默认端口号3307,注意需要加 -A才可以正常执行sql命令,原因似乎是不加A会初始化元数据
>mysql -h127.0.0.1 -P3307 -uroot -proot -A

>mysql -h127.0.0.1 -P3307 -uroot -proot -A
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.20-log-ShardingSphere-Proxy 5.0.0-RC1

Copyright (c) 2000, 2017, 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.

mysql> show databases;
+-------------+
| Database    |
+-------------+
| sharding_db |
+-------------+
1 row in set (0.00 sec)

mysql> show tables;
ERROR 1046 (3D000): No database selected
mysql> use sharding_db;
Database changed

mysql> show tables;
+---------------------+
| Tables_in_demo_ds_0 |
+---------------------+
| t_order             |
+---------------------+
1 row in set (0.04 sec)

mysql> select * from t_order;
Empty set (0.05 sec)

mysql> select * from t_order;
+----------+---------+--------+
| order_id | user_id | status |
+----------+---------+--------+
|        1 |       1 | FAIL   |
|        1 |       2 | OK     |
|        1 |       1 | FAIL   |
|        1 |       2 | OK     |
+----------+---------+--------+
4 rows in set (0.01 sec)

6.插入4条数据查看日志打印

insert into t_order (user_id,status) values(1,'FAIL');
insert into t_order (user_id,status) values(2,'OK');
insert into t_order  (user_id,status) values(1,'FAIL');
insert into t_order  (user_id,status) values(2,'OK');

你可能感兴趣的:(mysql,数据库)