使用Canal實(shí)現(xiàn)MySQL主從同步的流程步驟
說明:本文介紹如何使用Canal實(shí)現(xiàn)MySQL主從同步的效果
啟動(dòng)Canal
首先,設(shè)置Canal服務(wù)器里,目標(biāo)節(jié)點(diǎn)(即監(jiān)測的MySQL節(jié)點(diǎn))的配置,啟動(dòng)Canal服務(wù);

啟動(dòng)Canal服務(wù)器,Windows操作系統(tǒng)下,直接雙擊startup.bat文件即可;

創(chuàng)建項(xiàng)目
創(chuàng)建一個(gè)Spring Boot項(xiàng)目,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ū)動(dòng)-->
<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é)點(diǎn)的,且賬戶應(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é)點(diǎn)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
實(shí)體類對(duì)象
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());
}
}
對(duì)應(yīng)寫三個(gè)針對(duì)User表(User實(shí)體類對(duì)應(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);
}
啟動(dòng)程序前,看下兩個(gè)數(shù)據(jù)庫的表內(nèi)容,目前是一致的,即使不一致,在你想要進(jìn)行同步前,也應(yīng)該手動(dòng)導(dǎo)出/導(dǎo)入數(shù)據(jù),使其初始狀態(tài)數(shù)據(jù)保持一致。

啟動(dòng)程序,數(shù)據(jù)庫開始同步,查看控制臺(tái),在實(shí)時(shí)打印檢測的信息;

此時(shí),在主節(jié)點(diǎn)i_user表內(nèi)修改一條數(shù)據(jù),查看控制臺(tái),從數(shù)據(jù)庫內(nèi)容;

可以看到這次操作被canal監(jiān)測到了,并通過代碼更新到了從庫,即代碼中配置的數(shù)據(jù)庫;

查看從庫i_user表內(nèi)容,從庫數(shù)據(jù)成功同步;

到這里,使用Canal實(shí)現(xiàn)MySQL主從同步已完成;
另外
另外,我有個(gè)想法,能不能把這個(gè)項(xiàng)目package,打成一個(gè)jar包,當(dāng)遇到短期的數(shù)據(jù)庫同步場景時(shí),直接運(yùn)行這個(gè)jar包就可以了。
比如日常開發(fā)時(shí),我們想讓自己的本地庫與測試環(huán)境的庫保持同步,直接去修改測試庫配置,搭建主從可能比較麻煩,就可以用這種方式。甚至可以寫個(gè)bat腳本,配個(gè)環(huán)境變量,直接敲CMD命令就能實(shí)現(xiàn)兩個(gè)數(shù)據(jù)庫之間的同步了,非常方便。
總結(jié)
以上就是使用Canal實(shí)現(xiàn)MySQL主從同步效果的詳細(xì)內(nèi)容,更多關(guān)于Canal MySQL主從同步的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- 關(guān)于mysql主備切換canal出現(xiàn)的問題解決
- 詳解監(jiān)聽MySQL的binlog日志工具分析:Canal
- Canal實(shí)現(xiàn)MYSQL實(shí)時(shí)數(shù)據(jù)同步的示例代碼
- 基于Docker結(jié)合Canal實(shí)現(xiàn)MySQL實(shí)時(shí)增量數(shù)據(jù)傳輸功能
- 使用canal監(jiān)控mysql數(shù)據(jù)庫實(shí)現(xiàn)elasticsearch索引實(shí)時(shí)更新問題
- mysql使用Canal監(jiān)聽binlog日志,同步數(shù)據(jù)到MySQL、ElasticSearch、Redis、Kafka、RocketMQ和es
相關(guān)文章
MySQL中無GROUP BY情況下直接使用HAVING語句的問題探究
這篇文章主要介紹了MySQL中無GROUP BY情況下直接使用HAVING語句的問題探究,同時(shí)探究了該情況下MAX與MIN功能的使用情況,需要的朋友可以參考下2015-05-05
MYSQL出現(xiàn)" Client does not support authentication "的
MYSQL出現(xiàn)" Client does not support authentication "的解決方法...2007-06-06
MySQL實(shí)現(xiàn)創(chuàng)建存儲(chǔ)過程并循環(huán)添加記錄的方法
這篇文章主要介紹了MySQL實(shí)現(xiàn)創(chuàng)建存儲(chǔ)過程并循環(huán)添加記錄的方法,涉及基本的mysql存儲(chǔ)過程創(chuàng)建、調(diào)用相關(guān)操作技巧,需要的朋友可以參考下2017-05-05
mysql修改數(shù)據(jù)庫引擎的幾種方法總結(jié)
這篇文章主要給大家介紹了關(guān)于mysql修改數(shù)據(jù)庫引擎的相關(guān)資料,包括使用ALTERTABLE語句、更改默認(rèn)存儲(chǔ)引擎、使用MySQLWorkbench、導(dǎo)出和導(dǎo)入數(shù)據(jù)以及編寫腳本批量修改,每種方法都有其優(yōu)缺點(diǎn)和適用場景,需要的朋友可以參考下2024-11-11
Django+mysql配置與簡單操作數(shù)據(jù)庫實(shí)例代碼
這篇文章主要介紹了Django+mysql配置與簡單操作數(shù)據(jù)庫實(shí)例,需要的朋友可以參考下2017-07-07
5招帶你輕松優(yōu)化MySQL count(*)查詢性能
最近在公司優(yōu)化了幾個(gè)慢查詢接口的性能,總結(jié)了一些心得體會(huì)拿出來跟大家一起分享一下,文中的示例代碼講解詳細(xì),希望對(duì)大家會(huì)有所幫助2022-11-11
Mysql ERROR 1067: Invalid default v
這篇文章主要介紹了Mysql ERROR 1067: Invalid default value for字段問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
MYSQL Left Join優(yōu)化(10秒優(yōu)化到20毫秒內(nèi))
在實(shí)際開發(fā)中,相信大多數(shù)人都會(huì)用到j(luò)oin進(jìn)行連表查詢,但是有些人發(fā)現(xiàn),用join好像效率很低,而且驅(qū)動(dòng)表不同,執(zhí)行時(shí)間也不同。那么join到底是如何執(zhí)行的呢,本文就詳細(xì)的介紹一下2021-12-12

