Sharding
Usage
Pre-work
- Start the MySQL service
- Create MySQL database (refer to ShardingSphere-Proxy data source configuration rules)
- Create a role or user with creation permission for ShardingSphere-Proxy
- Start Zookeeper service (for persistent configuration)
Start ShardingSphere-Proxy
- Add
mode
andauthentication
configurations toserver.yaml
(please refer to the example of ShardingSphere-Proxy) - Start ShardingSphere-Proxy (Related introduction)
Create a distributed database and sharding tables
- Connect to ShardingSphere-Proxy
- Create a distributed database
CREATE DATABASE sharding_db;
- Use newly created database
USE sharding_db;
- Configure data source information
ADD RESOURCE ds_0 (
HOST=127.0.0.1,
PORT=3306,
DB=ds_1,
USER=root,
PASSWORD=root
);
ADD RESOURCE ds_1 (
HOST=127.0.0.1,
PORT=3306,
DB=ds_2,
USER=root,
PASSWORD=root
);
- Create sharding rule
CREATE SHARDING TABLE RULE t_order(
RESOURCES(ds_0,ds_1),
SHARDING_COLUMN=order_id,
TYPE(NAME=hash_mod,PROPERTIES("sharding-count"=4)),
GENERATED_KEY(COLUMN=order_id,TYPE(NAME=snowflake,PROPERTIES("worker-id"=123)))
);
- Create sharding table
CREATE TABLE `t_order` (
`order_id` int NOT NULL,
`user_id` int NOT NULL,
`status` varchar(45) DEFAULT NULL,
PRIMARY KEY (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
- Drop sharding table
DROP TABLE t_order;
- Drop sharding rule
DROP SHARDING TABLE RULE t_order;
- Drop resource
DROP RESOURCE ds_0, ds_1;
- Drop distributed database
DROP DATABASE sharding_db;
Notice
- Currently,
DROP DATABASE
will only remove thelogical distributed database
, not the user’s actual database. DROP TABLE
will delete all logical fragmented tables and actual tables in the database.CREATE DATABASE
will only create alogical distributed database
, so users need to create actual databases in advance.- The
Auto Sharding Algorithm
will continue to increase to cover the user’s various sharding scenarios.
当前内容版权归 ShardingSphere 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 ShardingSphere .