- deployer組件負責解析binlog日誌
- adapter負責適配各種存儲
實際使用過程中的例子:
使用canal+sharding-jdbc 進行分表, 並對之前的數據進行遷移
1. 操作步驟:
1. 使用mysqldump導出數據
mysqldump -t --databases test -uroot -pxxxxxxxxxx --skip-add-drop-table --single-transaction --master-data=2 --tables goods>D:\goods.sql
-- 注:
-- 1.全表備份where條件可刪除
-- 2.使用single-transaction master-data=2 防止數據庫鎖表
-- 3.導出後的日誌中有相關的binlog開始文件和位置
查看goods.sql文件
-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000093', MASTER_LOG_POS=455856543;
2. 創建分庫分表的sql, 由於需要創建32張表這裏使用腳本生成。*.groovy
// 必須先定義i,否則程序會報錯
def i = 0
def sql="""
drop table if exists user_${-> i};
CREATE TABLE `user_${-> i}` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`user_id` int(11) NOT NULL COMMENT '用戶ID',
`name` varchar(30) DEFAULT NULL COMMENT '名字',
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
"""
def file = new File("1.sql")
file.write("","utf-8")
for (j in 0..7){
i=j
file.append(sql,"utf-8")
}
println "write 1.sql finished! "
3. sql文件通過sharding-jdbc導入mysql
2. 配置
2.1. canal deployer配置
#instance.properties
#canal.instance.filter.regex=.*\\..*
canal.instance.filter.regex=test.goods
# table black regex
canal.instance.filter.black.regex=
2.2. canal adapter配置
# server.yml
srcDataSources:
defaultDS:
url: jdbc:mysql://127.0.0.1:3306/test?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password: 123456
canalAdapters:
- instance: example # canal instance Name or mq topic name
groups:
- groupId: g1
outerAdapters:
- name: logger
- name: rdb
key: mysql1
properties:
jdbc.driverClassName: com.mysql.jdbc.Driver
jdbc.url: jdbc:mysql://127.0.0.1:3307/sharding_db?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
jdbc.username: root
jdbc.password: root
dataSourceKey: defaultDS
destination: example #canal instance Name or mq topic name
groupId: g1
outerAdapterKey: mysql1
concurrent: true
dbMapping:
database: test
table: user
targetTable: sharding_db.user
targetPk:#可配置多個key
id: id
name:name
mapAll: true #不能忽略,必須配置
2.3 sharding-proxy配置
# vi conf/server.yaml authentication: users: root: password: root #代理密碼 props: executor.size: 16 sql.show: true
# vi conf/config-sharding.yaml
schemaName: sharding_db
dataSources:
test:
url: jdbc:mysql://127.0.0.1:3308/test?serverTimezone=UTC&useSSL=false # 目標數據源
username: root #用戶名
password: 123456 #密碼
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
shardingRule:# 分表規則
tables:
user: # 表名
actualDataNodes: test.user_${0..32} #表range
tableStrategy:
inline:
shardingColumn: id #分表字段
algorithmExpression: user_${id % 32} #分表規則
keyGenerator: #key生成算法
type: SNOWFLAKE
column: id
bindingTables:
- user
defaultDatabaseStrategy:
none:
defaultTableStrategy:
none: