使用Canal實現(xiàn)MySQL主從同步的流程步驟
說明:本文介紹如何使用Canal實現(xiàn)MySQL主從同步的效果
啟動Canal
首先,設(shè)置Canal服務(wù)器里,目標節(jié)點(即監(jiān)測的MySQL節(jié)點)的配置,啟動Canal服務(wù);
啟動Canal服務(wù)器,Windows操作系統(tǒng)下,直接雙擊startup.bat
文件即可;
創(chuàng)建項目
創(chuàng)建一個Spring Boot項目,pom.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.12</version> <relativePath/> </parent> <groupId>com.hezy</groupId> <artifactId>canal_demo</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!--druid--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.8</version> </dependency> <!--mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.2</version> </dependency> <!--mysql驅(qū)動--> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> </dependency> <!--canal客戶端--> <dependency> <groupId>top.javatool</groupId> <artifactId>canal-spring-boot-starter</artifactId> <version>1.2.1-RELEASE</version> </dependency> </dependencies> </project>
application.yml文件如下,這里的數(shù)據(jù)庫配置寫從節(jié)點的,且賬戶應(yīng)該有數(shù)據(jù)讀寫權(quán)限;
server: port: 8080 # 1.數(shù)據(jù)源的配置 spring: # 數(shù)據(jù)庫配置 datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://從節(jié)點MySQLIP:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: 123456 # 2.mybatis配置 mybatis: configuration: # 顯示SQL日志配置 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 駝峰命名配置 map-underscore-to-camel-case: true # 3.canal配置 canal: # canal服務(wù)端的ip server: 127.0.0.1:11111 destination: example
實體類對象
import lombok.Data; import java.io.Serializable; @Data public class User implements Serializable { private String id; private String username; private String password; }
canal處理類
import com.hezy.mapper.UserMapper; import com.hezy.pojo.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import top.javatool.canal.client.annotation.CanalTable; import top.javatool.canal.client.handler.EntryHandler; @Component @CanalTable("i_user") public class UserHandler implements EntryHandler<User> { @Autowired private UserMapper userMapper; @Override public void insert(User user) { System.out.println("新增用戶:" + user); userMapper.insertUser(user); } @Override public void update(User before, User after) { System.out.println("更新用戶:" + before + " -> " + after); userMapper.updateUserById(after); } @Override public void delete(User user) { System.out.println("刪除用戶:" + user); userMapper.deleteUserById(user.getId()); } }
對應(yīng)寫三個針對User表(User實體類對應(yīng)的表,即i_user表)操作的Mapper方法;
import com.hezy.pojo.User; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Update; @Mapper public interface UserMapper { @Insert("insert into i_user values (#{id}, #{username}, #{password})") void insertUser(User uesr); @Delete("delete from i_user where id = #{id}") void deleteUserById(String id); @Update("update i_user set username=#{username}, password=#{password} where id=#{id}") void updateUserById(User user); }
啟動程序前,看下兩個數(shù)據(jù)庫的表內(nèi)容,目前是一致的,即使不一致,在你想要進行同步前,也應(yīng)該手動導出/導入數(shù)據(jù),使其初始狀態(tài)數(shù)據(jù)保持一致。
啟動程序,數(shù)據(jù)庫開始同步,查看控制臺,在實時打印檢測的信息;
此時,在主節(jié)點i_user表內(nèi)修改一條數(shù)據(jù),查看控制臺,從數(shù)據(jù)庫內(nèi)容;
可以看到這次操作被canal監(jiān)測到了,并通過代碼更新到了從庫,即代碼中配置的數(shù)據(jù)庫;
查看從庫i_user表內(nèi)容,從庫數(shù)據(jù)成功同步;
到這里,使用Canal實現(xiàn)MySQL主從同步已完成;
另外
另外,我有個想法,能不能把這個項目package,打成一個jar包,當遇到短期的數(shù)據(jù)庫同步場景時,直接運行這個jar包就可以了。
比如日常開發(fā)時,我們想讓自己的本地庫與測試環(huán)境的庫保持同步,直接去修改測試庫配置,搭建主從可能比較麻煩,就可以用這種方式。甚至可以寫個bat腳本,配個環(huán)境變量,直接敲CMD命令就能實現(xiàn)兩個數(shù)據(jù)庫之間的同步了,非常方便。
總結(jié)
以上就是使用Canal實現(xiàn)MySQL主從同步效果的詳細內(nèi)容,更多關(guān)于Canal MySQL主從同步的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
MySQL啟動失敗報錯:mysqld.service failed to run 
在日常運維中,MySQL 作為廣泛應(yīng)用的關(guān)系型數(shù)據(jù)庫,其穩(wěn)定性和可用性至關(guān)重要,然而,有時系統(tǒng)升級或配置變更后,MySQL 服務(wù)可能會出現(xiàn)無法啟動的問題,本文針對某次實際案例進行深入分析和處理,需要的朋友可以參考下2024-12-12解析mysql數(shù)據(jù)庫還原錯誤:(mysql Error Code: 1005 errno 121)
本篇文章是對mysql數(shù)據(jù)庫還原錯誤:(mysql Error Code: 1005 errno 121)的解決方法進行了詳細的分析介紹,需要的朋友參考下2013-06-06python中的mysql數(shù)據(jù)庫LIKE操作符詳解
LIKE操作符用于在WHERE子句中搜索列中的指定模式,like操作符的語法在文章開頭也給大家提到,通過兩種示例代碼給大家介紹python中的mysql數(shù)據(jù)庫LIKE操作符知識,感興趣的朋友跟隨小編一起看看吧2021-07-07MySQL刪除表數(shù)據(jù)、清空表命令詳解(truncate、drop、delete區(qū)別)
介紹了MySQL中清空或刪除表數(shù)據(jù)的三種方法:truncate、delete和drop,以及它們的特點、使用場景和注意事項,Truncate用于快速刪除表中所有數(shù)據(jù)并釋放空間,但不保留表結(jié)構(gòu);delete用于刪除表中特定行或所有數(shù)據(jù),保留表結(jié)構(gòu)且操作可回滾2024-10-10