数据分库分表配置案例

一. 配置模板

1. 配置分库分表模板

  1. # 配置对外数据库名
  2. schemaName: big_db
  3. # 配置数据源
  4. dataSources:
  5. # ds0 源
  6. ds0:
  7. url: jdbc:mysql://localhost:3315/ds0
  8. username: root
  9. password:
  10. autoCommit: true
  11. connectionTimeout: 30000
  12. idleTimeout: 60000
  13. maxLifetime: 1800000
  14. maximumPoolSize: 65
  15. # ds1 源
  16. ds1:
  17. url: jdbc:mysql://localhost:3306/ds1
  18. username: root
  19. password:
  20. autoCommit: true
  21. connectionTimeout: 30000
  22. idleTimeout: 60000
  23. maxLifetime: 1800000
  24. maximumPoolSize: 65
  25. # 配置分表规则
  26. shardingRule:
  27. # 配置数据表
  28. tables:
  29. # 配置 t_order 表
  30. t_order:
  31. # 配置数据源规则
  32. actualDataNodes: ds${0..1}.t_order${0..1}
  33. # 配置表分表规则
  34. tableStrategy:
  35. # 分表算法
  36. inline:
  37. # 分表字段
  38. shardingColumn: order_id
  39. # 分表定位到 mysql 实体表的路由规则
  40. algorithmExpression: t_order${order_id % 2}
  41. # 分布式 id 生成字段
  42. keyGeneratorColumnName: order_id
  43. # 配置 t_order_item 表
  44. t_order_item:
  45. actualDataNodes: ds${0..1}.t_order_item${0..1}
  46. tableStrategy:
  47. inline:
  48. shardingColumn: order_id
  49. algorithmExpression: t_order_item${order_id % 2}
  50. # 注册到代理的数据表, 多个逗号分隔
  51. bindingTables:
  52. - t_order,t_order_item
  53. # 默认分库策略
  54. defaultDatabaseStrategy:
  55. # 分库算法
  56. inline:
  57. # 分库字段
  58. shardingColumn: user_id
  59. # 分库规则
  60. algorithmExpression: ds${user_id % 2}
  61. # 默认分表策略
  62. defaultTableStrategy:
  63. none:
  64. # 分布式 id 生成算法
  65. defaultKeyGeneratorClassName: io.shardingsphere.core.keygen.DefaultKeyGenerator

2. 配套 SQL

  1. use big_db;
  2. show tables;
  3. DROP TABLE big_db.t_order;
  4. CREATE TABLE IF NOT EXISTS big_db.t_order (
  5. id BIGINT NOT NULL AUTO_INCREMENT,
  6. user_id INT NOT NULL,
  7. order_id INT NOT NULL,
  8. info VARCHAR(50),
  9. PRIMARY KEY (id))
  10. ;
  11. INSERT INTO `t_order` (`user_id`,`order_id`, `info`) VALUES (2, 3, 'info');
  12. SELECT COUNT(*) FROM t_order;

二. 配置案例

1. 配置分库分表

  • 配置 conf/config-sharding_db.yaml 文件
  1. schemaName: big_db
  2. dataSources:
  3. ds_app1:
  4. url: jdbc:mysql://app1:3306/temp_db?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
  5. username: root
  6. password:
  7. autoCommit: true
  8. connectionTimeout: 30000
  9. idleTimeout: 60000
  10. maxLifetime: 1800000
  11. maximumPoolSize: 50
  12. ds_app2:
  13. url: jdbc:mysql://app2:3315/temp_db?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
  14. username: root
  15. password:
  16. autoCommit: true
  17. connectionTimeout: 30000
  18. idleTimeout: 60000
  19. maxLifetime: 1800000
  20. maximumPoolSize: 50
  21. shardingRule:
  22. tables:
  23. # 配置表 t_big_table
  24. t_big_table:
  25. actualDataNodes: ds_${['app2', 'app1']}.t_big_table_${['default']}
  26. # 分表规则
  27. tableStrategy:
  28. # 分表算法
  29. inline:
  30. # 分表字段
  31. shardingColumn: tb_partition_key
  32. # 分表
  33. algorithmExpression: t_big_table_${tb_partition_key}
  34. keyGeneratorColumnName: id
  35. # 注册表
  36. bindingTables:
  37. - t_big_table
  38. # 默认连接数据库
  39. defaultDataSourceName: ds_app2
  40. # 默认分库策略
  41. defaultDatabaseStrategy:
  42. # 分库算法
  43. inline:
  44. # 分库字段
  45. shardingColumn: db_partition_key
  46. # 分库路由规则
  47. algorithmExpression: ds_${db_partition_key}
  48. # 默认分表策略
  49. defaultTableStrategy:
  50. none:
  51. defaultKeyGeneratorClassName: io.shardingsphere.core.keygen.DefaultKeyGenerator

2. 配套 SQL

  1. use big_db;
  2. show tables;
  3. -- 删除 SQL
  4. DROP TABLE big_db.t_big_table;
  5. -- 创建数据表
  6. CREATE TABLE IF NOT EXISTS big_db.t_big_table (
  7. id BIGINT NOT NULL AUTO_INCREMENT,
  8. info varchar(100) NOT NULL DEFAULT 'info' COMMENT 'info',
  9. db_partition_key varchar(100) NOT NULL DEFAULT 'app2' COMMENT 'database 分区',
  10. tb_partition_key varchar(100) NOT NULL DEFAULT 'default' COMMENT 'table 分区',
  11. PRIMARY KEY (id))
  12. ;
  13. -- 插入数据, 会写入到 数据库(app1) t_big_table_(default)
  14. INSERT INTO `t_big_table` (`info`, `db_partition_key`, `tb_partition_key`) VALUES ('info', 'app1', 'default');
  15. -- 插入数据, 会写入到 数据库(app2) t_big_table_(default)
  16. INSERT INTO `t_big_table` (`info`, `db_partition_key`, `tb_partition_key`) VALUES ('info', 'app2', 'default');
  17. -- 插入数据, 会写入到 数据库(app2) t_big_table_(20181211) 中, 如果表不存在需要创建
  18. INSERT INTO `t_big_table` (`info`, `db_partition_key`, `tb_partition_key`) VALUES ('info', 'app2', '20181211');
  19. -- 查询总数据, 会有个 BUG, 需要提前把所有规则填写在 actualDataNodes 配置中, 才能读到数据
  20. SELECT COUNT(*) FROM big_db.t_big_table;
  21. -- 查询指定数据库, 指定数据表数据
  22. SELECT COUNT(*) FROM big_db.t_big_table WHERE db_partition_key='app2' AND tb_partition_key = '20181211';