欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Canal實現(xiàn)MySQL主從同步的流程步驟

 更新時間:2024年04月29日 09:20:25   作者:何中應(yīng)  
這篇文章主要介紹了如何使用Canal實現(xiàn)MySQL主從同步效果,文中通過代碼示例和圖文結(jié)合的方式給大家講解的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下

說明:本文介紹如何使用Canal實現(xiàn)MySQL主從同步的效果

啟動Canal

首先,設(shè)置Canal服務(wù)器里,目標(biāo)節(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)容,目前是一致的,即使不一致,在你想要進(jìn)行同步前,也應(yīng)該手動導(dǎo)出/導(dǎo)入數(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包,當(dāng)遇到短期的數(shù)據(jù)庫同步場景時,直接運行這個jar包就可以了。

比如日常開發(fā)時,我們想讓自己的本地庫與測試環(huán)境的庫保持同步,直接去修改測試庫配置,搭建主從可能比較麻煩,就可以用這種方式。甚至可以寫個bat腳本,配個環(huán)境變量,直接敲CMD命令就能實現(xiàn)兩個數(shù)據(jù)庫之間的同步了,非常方便。

總結(jié)

以上就是使用Canal實現(xiàn)MySQL主從同步效果的詳細(xì)內(nèi)容,更多關(guān)于Canal MySQL主從同步的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • mysql中GROUP_CONCAT的使用方法實例分析

    mysql中GROUP_CONCAT的使用方法實例分析

    這篇文章主要介紹了mysql中GROUP_CONCAT的使用方法,結(jié)合實例形式分析了MySQL中GROUP_CONCAT合并查詢結(jié)果的相關(guān)操作技巧,需要的朋友可以參考下
    2020-02-02
  • 詳解MySQL分組鏈接的使用技巧

    詳解MySQL分組鏈接的使用技巧

    本篇文章主要針對MYSQL中分組以及4種鏈接做了詳細(xì)的分析,有助于大家對這2項MYSQL功能有深入的理解,參考學(xué)習(xí)下吧。
    2017-12-12
  • MySQL分組排序取每組第一條數(shù)據(jù)的實現(xiàn)

    MySQL分組排序取每組第一條數(shù)據(jù)的實現(xiàn)

    最近有個需求MySQL根據(jù)某一個字段分組,然后組內(nèi)排序,最后每組取排序后的第一條數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-08-08
  • win2008下mysql8.0.11升級mysql8.0.17版本詳細(xì)步驟

    win2008下mysql8.0.11升級mysql8.0.17版本詳細(xì)步驟

    這篇文章主要為大家詳細(xì)介紹了win2008下mysql8.0.11升級mysql8.0.17版本詳細(xì)步驟,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • 關(guān)于Mysql自增id的這些你可能還不知道

    關(guān)于Mysql自增id的這些你可能還不知道

    這篇文章主要給大家介紹了關(guān)于Mysql自增id的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Mysql具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • MySQL安裝服務(wù)時提示:Install/Remove?of?the?Service?Denied解決

    MySQL安裝服務(wù)時提示:Install/Remove?of?the?Service?Denied解決

    今天給新電腦安裝了mysql,本來好好的,卻報了個bug,就記錄下吧,這篇文章主要給大家介紹了關(guān)于MySQL安裝服務(wù)時提示:Install/Remove?of?the?Service?Denied的解決辦法,需要的朋友可以參考下
    2023-03-03
  • mysql存儲過程基礎(chǔ)之遍歷多表記錄后插入第三方表中詳解

    mysql存儲過程基礎(chǔ)之遍歷多表記錄后插入第三方表中詳解

    這篇文章主要給大家介紹了關(guān)于mysql存儲過程教程之遍歷多表記錄后插入第三方表中的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧
    2018-07-07
  • mysql二進(jìn)制日志文件恢復(fù)數(shù)據(jù)庫

    mysql二進(jìn)制日志文件恢復(fù)數(shù)據(jù)庫

    喜歡的在服務(wù)器或者數(shù)據(jù)庫上直接操作的兄弟們你值得收藏下!不然你就悲劇了。-----(當(dāng)然我也是在網(wǎng)上搜索的資料!不過自己測試通過了的!)
    2014-08-08
  • MySQL中的物理存儲結(jié)構(gòu)詳解

    MySQL中的物理存儲結(jié)構(gòu)詳解

    這篇文章主要介紹了MySQL中的物理存儲結(jié)構(gòu)用法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • mysql 5.6.37(zip)下載安裝配置圖文教程

    mysql 5.6.37(zip)下載安裝配置圖文教程

    這篇文章主要為大家詳細(xì)介紹了mysql 5.6.37(zip)下載安裝配置圖文教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-08-08

最新評論