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

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

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

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

啟動(dòng)Canal

首先,設(shè)置Canal服務(wù)器里,目標(biāo)節(jié)點(diǎn)(即監(jiān)測(cè)的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ù)庫(kù)配置寫從節(jié)點(diǎn)的,且賬戶應(yīng)該有數(shù)據(jù)讀寫權(quán)限;

server:
  port: 8080

# 1.數(shù)據(jù)源的配置
spring:
  # 數(shù)據(jù)庫(kù)配置
  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ù)庫(kù)的表內(nèi)容,目前是一致的,即使不一致,在你想要進(jìn)行同步前,也應(yīng)該手動(dòng)導(dǎo)出/導(dǎo)入數(shù)據(jù),使其初始狀態(tài)數(shù)據(jù)保持一致。

在這里插入圖片描述

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

在這里插入圖片描述

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

在這里插入圖片描述

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

在這里插入圖片描述

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

在這里插入圖片描述

到這里,使用Canal實(shí)現(xiàn)MySQL主從同步已完成;

另外

另外,我有個(gè)想法,能不能把這個(gè)項(xiàng)目package,打成一個(gè)jar包,當(dāng)遇到短期的數(shù)據(jù)庫(kù)同步場(chǎng)景時(shí),直接運(yùn)行這個(gè)jar包就可以了。

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

總結(jié)

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

相關(guān)文章

  • MySQL雙主配置的項(xiàng)目實(shí)踐

    MySQL雙主配置的項(xiàng)目實(shí)踐

    本文詳細(xì)介紹了配置兩臺(tái)MySQL服務(wù)器之間的主從復(fù)制,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-12-12
  • MySQL啟動(dòng)失敗報(bào)錯(cuò):mysqld.service failed to run ‘start-pre‘ task的問題分析與解決方案

    MySQL啟動(dòng)失敗報(bào)錯(cuò):mysqld.service failed to run 

    在日常運(yùn)維中,MySQL 作為廣泛應(yīng)用的關(guān)系型數(shù)據(jù)庫(kù),其穩(wěn)定性和可用性至關(guān)重要,然而,有時(shí)系統(tǒng)升級(jí)或配置變更后,MySQL 服務(wù)可能會(huì)出現(xiàn)無法啟動(dòng)的問題,本文針對(duì)某次實(shí)際案例進(jìn)行深入分析和處理,需要的朋友可以參考下
    2024-12-12
  • 刪除MySQL表中重復(fù)數(shù)據(jù)詳解

    刪除MySQL表中重復(fù)數(shù)據(jù)詳解

    這篇文章主要為大家介紹了刪除MySQL表中重復(fù)數(shù)據(jù)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • VS2013連接MySQL5.6成功案例一枚

    VS2013連接MySQL5.6成功案例一枚

    這篇文章主要為大家分享了VS2013連接MySQL5.6成功案例一枚,很有實(shí)用性,感興趣的小伙伴們可以參考一下
    2016-05-05
  • navicat連接mysql報(bào)錯(cuò)10060的解決辦法

    navicat連接mysql報(bào)錯(cuò)10060的解決辦法

    最近在學(xué)習(xí)中遇到了個(gè)小問題,現(xiàn)在將解決的辦法分享給同樣遇到這個(gè)問題的同學(xué),這篇文章主要給大家介紹了關(guān)于navicat連接mysql報(bào)錯(cuò)10060的解決辦法,需要的朋友可以參考下
    2023-03-03
  • MySQL 使用 SSL 連接配置詳解

    MySQL 使用 SSL 連接配置詳解

    本文給大家分享的是如何配置MySQL支持SSL連接方式的方法以及在docker中配置的具體案例,有需要的小伙伴可以參考下
    2016-12-12
  • 解析mysql數(shù)據(jù)庫(kù)還原錯(cuò)誤:(mysql Error Code: 1005 errno 121)

    解析mysql數(shù)據(jù)庫(kù)還原錯(cuò)誤:(mysql Error Code: 1005 errno 121)

    本篇文章是對(duì)mysql數(shù)據(jù)庫(kù)還原錯(cuò)誤:(mysql Error Code: 1005 errno 121)的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • MySQL 啟動(dòng)成功但未監(jiān)聽端口的解決方法

    MySQL 啟動(dòng)成功但未監(jiān)聽端口的解決方法

    這篇文章主要給大家介紹了關(guān)于MySQL 啟動(dòng)成功但未監(jiān)聽端口的解決方法,文中通過圖文給大家介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。
    2017-06-06
  • python中的mysql數(shù)據(jù)庫(kù)LIKE操作符詳解

    python中的mysql數(shù)據(jù)庫(kù)LIKE操作符詳解

    LIKE操作符用于在WHERE子句中搜索列中的指定模式,like操作符的語法在文章開頭也給大家提到,通過兩種示例代碼給大家介紹python中的mysql數(shù)據(jù)庫(kù)LIKE操作符知識(shí),感興趣的朋友跟隨小編一起看看吧
    2021-07-07
  • MySQL刪除表數(shù)據(jù)、清空表命令詳解(truncate、drop、delete區(qū)別)

    MySQL刪除表數(shù)據(jù)、清空表命令詳解(truncate、drop、delete區(qū)別)

    介紹了MySQL中清空或刪除表數(shù)據(jù)的三種方法:truncate、delete和drop,以及它們的特點(diǎn)、使用場(chǎng)景和注意事項(xiàng),Truncate用于快速刪除表中所有數(shù)據(jù)并釋放空間,但不保留表結(jié)構(gòu);delete用于刪除表中特定行或所有數(shù)據(jù),保留表結(jié)構(gòu)且操作可回滾
    2024-10-10

最新評(píng)論