文章目錄
簡介
定位爲透明化的數據庫代理端,提供封裝了數據庫二進制協議的服務端版本,用於完成對異構語言的支持。 目前先提供MySQL/PostgreSQL版本,它可以使用任何兼容MySQL/PostgreSQL協議的訪問客戶端(如:MySQL Command Client, MySQL Workbench, Navicat等)操作數據,對DBA更加友好。
嚮應用程序完全透明,可直接當做MySQL/PostgreSQL使用。
適用於任何兼容MySQL/PostgreSQL協議的的客戶端。
1 進入conf目錄,修改server.yaml
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
query.with.cipher.column: true
sql.show: false
2 進入conf目錄,修改config-sharding.yaml
2.1 拷貝mysql驅動到Sharding-Proxy的lib目錄
將mysql 驅動 mysql-connector-java-8.0.15.jar 複製到Sharding-Proxy的lib目錄中
2.2 修改 config-sharding.yaml 文件
schemaName: sharding_db
dataSources:
ds_0:
url: jdbc:mysql://127.0.0.1:3306/edu1?serverTimezone=UTC&useSSL=false
username: root
password: chengwen
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
shardingRule:
tables:
t_order:
actualDataNodes: ds_${0}.t_order_${0..1}
tableStrategy:
inline:
shardingColumn: order_id
algorithmExpression: t_order_${order_id % 2}
keyGenerator:
type: SNOWFLAKE
column: order_id
bindingTables:
- t_order
defaultDatabaseStrategy:
inline:
shardingColumn: user_id
algorithmExpression: ds_${0}
defaultTableStrategy:
none:
2.3 本地創建一個數據庫
CREATE SCHEMA `edu1` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ;
3 啓動Sharding-Proxy服務
進入bin目錄,執行命令啓動Sharding-Proxy
chengwen@chengwen 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
chengwen@chengwen bin %
查看日誌,出現端口號3307(默認),ACTIVE表示成功
[INFO ] 00:27:18.350 [nioEventLoopGroup-2-1] i.n.handler.logging.LoggingHandler - [id: 0x7014d160] REGISTERED
[INFO ] 00:27:18.352 [nioEventLoopGroup-2-1] i.n.handler.logging.LoggingHandler - [id: 0x7014d160] BIND: 0.0.0.0/0.0.0.0:3307
[INFO ] 00:27:18.355 [nioEventLoopGroup-2-1] i.n.handler.logging.LoggingHandler - [id: 0x7014d160, L:/0.0.0.0:3307] ACTIVE
4 通過Sharding-Proxy啓動端口進行連接
4.1 打開Navicat for Mysql 15 連接sharding-proxy
4.2 進行sql命令操作看到只有一個庫
4.3 在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.4 向表中添加一條記錄
USE sharding_db;
show tables;
select * from t_order;
insert into t_order(order_id,user_id,status) values (11,1,'init');
select * from t_order;
5 回到本地3306端口數據庫中,看到已經創建好了表和添加了數據
本地庫edu1需要自己創建,數據表和數據記錄會由sharding-proxy自動創建