Canal進(jìn)行MySQL到MySQL數(shù)據(jù)庫(kù)全量+增量同步踩坑指南
背景
最近工作中遇到一個(gè)遷移數(shù)據(jù)庫(kù)的需求,需要將數(shù)據(jù)庫(kù)從A服務(wù)器遷移至B服務(wù)器,為了盡量減少遷移導(dǎo)致的停機(jī)時(shí)間,考慮使用全量遷移+增量同步的方式,最終選擇使用Canal作為遷移工具
準(zhǔn)備工作
1. 數(shù)據(jù)庫(kù)
兩臺(tái)服務(wù)器的數(shù)據(jù)庫(kù)都需要提前準(zhǔn)備好數(shù)據(jù)庫(kù)賬號(hào),用于Canal進(jìn)行數(shù)據(jù)庫(kù)連接,因?yàn)镃anal是偽裝成從庫(kù)讀取源數(shù)據(jù)的,所以需要對(duì)源庫(kù)進(jìn)行一些配置,比如賬號(hào)需要授予從庫(kù)權(quán)限(如果給的是最高權(quán)限賬號(hào),那基本不用擔(dān)心這個(gè))、保證Canal部署的程序與數(shù)據(jù)庫(kù)網(wǎng)絡(luò)互通、以及最重要的!開(kāi)啟ROW格式的Binlog,這個(gè)非常重要!
2.Canal安裝
使用Canal進(jìn)行全量和增量同步需要用到兩個(gè)組件,Canal adapter、Canal deployer,可自行前往github倉(cāng)庫(kù)下載。
Canal配置啟動(dòng)增量同步和全量同步
1.deployer配置
將下載的兩個(gè)組件解壓,首先配置deployer
主要關(guān)注conf目錄
進(jìn)入example目錄,編輯instance.properties文件:
重點(diǎn)關(guān)注這幾個(gè)參數(shù):canal.instance.master.address,canal.instance.dbUsername,canal.instance.dbPassword,canal.instance.filter.regex(重要)
這樣就配置好了deployer,回到deployer的bin目錄,執(zhí)行startup.sh即可啟動(dòng)deployer程序(此時(shí)還未同步)
################################################# ## mysql serverId , v1.0.26+ will autoGen # canal.instance.mysql.slaveId=0 # enable gtid use true/false canal.instance.gtidon=false # position info canal.instance.master.address=這里填寫(xiě)源數(shù)據(jù)庫(kù)的ip:端口 canal.instance.master.journal.name= canal.instance.master.position= canal.instance.master.timestamp= canal.instance.master.gtid= # rds oss binlog canal.instance.rds.accesskey= canal.instance.rds.secretkey= canal.instance.rds.instanceId= # table meta tsdb info canal.instance.tsdb.enable=true #canal.instance.tsdb.url=jdbc:mysql://127.0.0.1:3306/canal_tsdb #canal.instance.tsdb.dbUsername=canal #canal.instance.tsdb.dbPassword=canal #canal.instance.standby.address = #canal.instance.standby.journal.name = #canal.instance.standby.position = #canal.instance.standby.timestamp = #canal.instance.standby.gtid= # username/password canal.instance.dbUsername=填寫(xiě)源數(shù)據(jù)庫(kù)用戶名,例:root canal.instance.dbPassword=填寫(xiě)源數(shù)據(jù)庫(kù)密碼,例:root canal.instance.connectionCharset = UTF-8 # enable druid Decrypt database password canal.instance.enableDruid=false #canal.instance.pwdPublicKey=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALK4BUxdDltRRE5/zXpVEVPUgunvscYFtEip3pmLlhrWpacX7y7GCMo2/JM6LeHmiiNdH1FWgGCpUfircSwlWKUCAwEAAQ== # table regex canal.instance.filter.regex=這里填寫(xiě)需要進(jìn)行同步的數(shù)據(jù)庫(kù)表名,格式:數(shù)據(jù)庫(kù)名.表名,多個(gè)表用逗號(hào)分分隔,例如:testdb.user,testdb.role,testdb.menu # table black regex canal.instance.filter.black.regex=mysql\\.slave_.* # table field filter(format: schema1.tableName1:field1/field2,schema2.tableName2:field1/field2) #canal.instance.filter.field=test1.t_product:id/subject/keywords,test2.t_company:id/name/contact/ch # table field black filter(format: schema1.tableName1:field1/field2,schema2.tableName2:field1/field2) #canal.instance.filter.black.field=test1.t_product:subject/product_image,test2.t_company:id/name/contact/ch # table field black filter(format: schema1.tableName1:field1/field2,schema2.tableName2:field1/field2) #canal.instance.filter.black.field=test1.t_product:subject/product_image,test2.t_company:id/name/contact/ch # mq config canal.mq.topic=example # dynamic topic route by schema or table regex #canal.mq.dynamicTopic=mytest1.user,topic2:mytest2\\..*,.*\\..* canal.mq.partition=0 # hash partition config #canal.mq.enableDynamicQueuePartition=false #canal.mq.partitionsNum=3 #canal.mq.dynamicTopicPartitionNum=test.*:4,mycanal:6 #canal.mq.partitionHash=test.table:id^name,.*\\..* #################################################
2.adapter配置
同樣,進(jìn)入conf目錄
首先編輯bootstrap.yml文件,將里面的配置全部注釋?zhuān)驗(yàn)槲覀儧](méi)有用上admin的服務(wù),不需要連Canal自身的數(shù)據(jù)庫(kù)
#canal: # manager: # jdbc: # url: jdbc:mysql://127.0.0.1:3306/canal_manager?useUnicode=true&characterEncoding=UTF-8 # username: canal # password: canal
接著編輯application.yml文件
主要關(guān)注canal.conf.srcDataSources和canal.conf.canalAdapters下的配置
srcDataSources:
defaultDS:
url: 這里填源數(shù)據(jù)庫(kù)的jdbc連接信息,例:jdbc:mysql://127.0.0.1:3306/testdb
username: 數(shù)據(jù)庫(kù)賬號(hào),例:root
password: 數(shù)據(jù)庫(kù)密碼,例:root
maxActive: 100 #額外增加這一行,默認(rèn)的連接數(shù)只有3,會(huì)導(dǎo)致全量同步出現(xiàn)異常,導(dǎo)致全量同步數(shù)據(jù)缺失,最好改大一點(diǎn)
canalAdapters:
- instance: example #這里就保留叫example,只是個(gè)命名
groups:
- groupId: g1
outerAdapters:
- name: logger
- name: rdb
key: mysql1 # 這里保持默認(rèn)即可
properties:
jdbc.driverClassName: com.mysql.jdbc.Driver #數(shù)據(jù)庫(kù)驅(qū)動(dòng)就保持這個(gè)驅(qū)動(dòng),不要修改為帶cj的那個(gè)驅(qū)動(dòng),如果這個(gè)驅(qū)動(dòng)有問(wèn)題,再考慮更換驅(qū)動(dòng)
jdbc.url: 這里填寫(xiě)需要同步接收方的數(shù)據(jù)庫(kù)鏈接,例:jdbc:mysql://192.168.1.2:3309/testdb?useUnicode=true
jdbc.username: root
jdbc.password: 123456
druid.stat.enable: false
druid.stat.slowSqlMillis: 1000修改好文件后保存
增量同步配置
由于本文場(chǎng)景是從mysql同步到另一個(gè)mysql,如果需要同步到es或者其他系統(tǒng),請(qǐng)參考github內(nèi)的教程
同步到mysql(或是其他關(guān)系型數(shù)據(jù)庫(kù))則接著進(jìn)入到rdb目錄
該文件夾內(nèi),里面已經(jīng)有一個(gè)示例yml文件,一個(gè)yml文件一般配置一個(gè)同步表,如果有多個(gè)表需要同步,則復(fù)制多個(gè)yml副本,將文件名修改為庫(kù)名_表名.yml,方便歸類(lèi),本文由于只有一個(gè)庫(kù),就直接命名為表名.yml
以本文為例,復(fù)制默認(rèn)yml文件3份,分別命名為user.yml、role.yml、menu.yml,代表三個(gè)表,接下來(lái)以u(píng)ser.yml配置為例
dataSourceKey: defaultDS #無(wú)需修改
destination: example #這個(gè)對(duì)應(yīng)application.yml文件中的
groupId: g1
outerAdapterKey: mysql1
concurrent: true
dbMapping:
database: epidemic #需要同步的數(shù)據(jù)庫(kù)名,兩臺(tái)服務(wù)器的數(shù)據(jù)庫(kù)名最好命名成一樣的,不然可能有各種意外情況發(fā)生
table: user #需要同步的表名
targetTable: user #同步到目標(biāo)服務(wù)器對(duì)應(yīng)的表名
targetPk:
id: id # 主鍵,格式為, 源表主鍵名:目標(biāo)表主鍵名
mapAll: true # 映射所有字段,這里改為true的話,下面的targetColumns就可以不用改了,除非兩邊的數(shù)據(jù)結(jié)構(gòu)不一樣
# targetColumns:
# id:
# name:
# role_id:
# c_time:
# test1:
# etlCondition: "where c_time>={}"
commitBatch: 3000 # 批量提交的大小,保持不動(dòng)即可其他幾個(gè)表的yml文件填寫(xiě)方式類(lèi)似
修改好配置文件后,回到adapter的bin目錄內(nèi),執(zhí)行startup.sh,啟動(dòng)adapter,此時(shí)可以查看adapter/logs/adapter/adapter.log,如果里面開(kāi)始有輸出DML語(yǔ)句則代表增量同步已經(jīng)開(kāi)始了
3.全量同步
使用命令行執(zhí)行如下命令,即可啟動(dòng)全量同步:curl http://canal部署的ip:8081/etl/rdb/mysql1/rdb目錄內(nèi)對(duì)應(yīng)表的文件名.yml -X POST例如:curl http://127.0.0.1:8081/etl/rdb/mysql1/user.yml -X POST依然查看adapter/logs/adapter/adapter.log日志即可看到全量同步的輸出
完成
至此,增量同步和全量同步都完成了
到此這篇關(guān)于Canal進(jìn)行MySQL到MySQL數(shù)據(jù)庫(kù)全量+增量同步踩坑指南的文章就介紹到這了,更多相關(guān)Canal進(jìn)行MySQL到MySQL數(shù)據(jù)庫(kù)全量+增量同步踩坑指南內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Redis和數(shù)據(jù)庫(kù)的一致性(Canal+MQ) 的實(shí)現(xiàn)
- 使用Canal實(shí)現(xiàn)MySQL數(shù)據(jù)同步的完整指南
- canal實(shí)現(xiàn)mysql數(shù)據(jù)同步的詳細(xì)過(guò)程
- 兩個(gè)windows服務(wù)器使用canal實(shí)現(xiàn)mysql實(shí)時(shí)同步
- Canal實(shí)現(xiàn)MYSQL實(shí)時(shí)數(shù)據(jù)同步的示例代碼
- 基于Docker結(jié)合Canal實(shí)現(xiàn)MySQL實(shí)時(shí)增量數(shù)據(jù)傳輸功能
- MySQL數(shù)據(jù)實(shí)時(shí)同步Redis的方案全解析
- 保證MySQL與Redis數(shù)據(jù)一致性的6種實(shí)現(xiàn)方案
- Redis與MySQL數(shù)據(jù)一致性問(wèn)題的策略模式及解決方案
- 詳解讓MySQL和Redis數(shù)據(jù)保持一致的四種策略
- Java使用Canal同步MySQL數(shù)據(jù)到Redis
- Linux寶塔面板使用Canal實(shí)現(xiàn)Mysql和Redis數(shù)據(jù)同步(圖文教程)
相關(guān)文章
解說(shuō)mysql之binlog日志以及利用binlog日志恢復(fù)數(shù)據(jù)的方法
下面小編就為大家?guī)?lái)一篇解說(shuō)mysql之binlog日志以及利用binlog日志恢復(fù)數(shù)據(jù)的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03
MySQL 5.6 中的 TIMESTAMP 和 explicit_defaults_for_timestamp 參數(shù)
這篇文章主要介紹了MySQL 5.6 中的 TIMESTAMP 和 explicit_defaults_for_timestamp 參數(shù),需要的朋友可以參考下2015-08-08
MySQL?從0到1打開(kāi)數(shù)據(jù)庫(kù)管理操作方法
數(shù)據(jù)庫(kù)管理系統(tǒng)(DataBase?Management?System)是用來(lái)創(chuàng)建數(shù)據(jù)庫(kù)和管理數(shù)據(jù)庫(kù)數(shù)據(jù)的一個(gè)管理軟件,我們口頭說(shuō)的MySQL數(shù)據(jù)庫(kù)就是這個(gè)管理系統(tǒng),這篇文章主要介紹了MySQL從0到1打開(kāi)數(shù)據(jù)庫(kù)管理,需要的朋友可以參考下2023-06-06
MySQL里Create Index 能否創(chuàng)建主鍵 Primary Key
MySQL里Create Index 能否創(chuàng)建主鍵 Primary Key2009-07-07
MySQL延遲關(guān)聯(lián)性能優(yōu)化方法
這篇文章主要介紹了MySQL延遲關(guān)聯(lián)性能優(yōu)化方法,本文講解了延遲關(guān)聯(lián)的背景、延遲關(guān)聯(lián)的分析、延遲關(guān)聯(lián)的解決等內(nèi)容,需要的朋友可以參考下2015-05-05
SQL創(chuàng)建視圖的注意事項(xiàng)及說(shuō)明
這篇文章主要介紹了SQL創(chuàng)建視圖的注意事項(xiàng)及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
MySQL8.0.3 RC版即將發(fā)布 先來(lái)看看有哪些變化
MySQL8.0.3 RC版即將發(fā)布,這篇文章主要介紹了MySQL8.0.3 RC版的一些新變化,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09

