数据分片

资源操作

  1. ADD RESOURCE ds_0 (
  2. HOST=127.0.0.1,
  3. PORT=3306,
  4. DB=ds_1,
  5. USER=root,
  6. PASSWORD=root
  7. );
  8. ADD RESOURCE ds_1 (
  9. HOST=127.0.0.1,
  10. PORT=3306,
  11. DB=ds_2,
  12. USER=root,
  13. PASSWORD=root
  14. );

规则操作

  • 创建分片规则
  1. CREATE SHARDING TABLE RULE t_order(
  2. RESOURCES(ds_0,ds_1),
  3. SHARDING_COLUMN=order_id,
  4. TYPE(NAME=hash_mod,PROPERTIES("sharding-count"=4)),
  5. GENERATED_KEY(COLUMN=order_id,TYPE(NAME=snowflake,PROPERTIES("worker-id"=123)))
  6. );
  • 创建切分表
  1. CREATE TABLE `t_order` (
  2. `order_id` int NOT NULL,
  3. `user_id` int NOT NULL,
  4. `status` varchar(45) DEFAULT NULL,
  5. PRIMARY KEY (`order_id`)
  6. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
  • 删除切分表
  1. DROP TABLE t_order;
  • 删除分片规则
  1. DROP SHARDING TABLE RULE t_order;
  • 删除数据源
  1. DROP RESOURCE ds_0, ds_1;
  • 删除分布式数据库
  1. DROP DATABASE foo_db;