最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用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)測的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各種查詢方式詳解

    MySql各種查詢方式詳解

    如果是做數(shù)據(jù)分析,MySQL里面最重要、最常用的就是數(shù)據(jù)查詢,數(shù)據(jù)查詢不只是簡單查詢數(shù)據(jù)庫中存儲(chǔ)的數(shù)據(jù),還要根據(jù)需求對(duì)數(shù)據(jù)進(jìn)行篩選、聚合,以及確定數(shù)據(jù)以什么樣的格式進(jìn)行顯示。MySQL提供了強(qiáng)大、靈活的語句和函數(shù)來實(shí)現(xiàn)查詢的操作
    2022-07-07
  • MySQL中無GROUP BY情況下直接使用HAVING語句的問題探究

    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 "的

    MYSQL出現(xiàn)" Client does not support authentication "的解決方法...
    2007-06-06
  • MySQL主鍵作用與使用方法詳解

    MySQL主鍵作用與使用方法詳解

    主鍵是數(shù)據(jù)庫表中的一個(gè)或多個(gè)字段(列),它的值用于唯一地標(biāo)識(shí)表中的某一條記錄,這篇文章主要介紹了MySQL主鍵作用與使用方法的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2026-01-01
  • MySQL實(shí)現(xiàn)創(chuàng)建存儲(chǔ)過程并循環(huán)添加記錄的方法

    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é)

    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í)例代碼

    這篇文章主要介紹了Django+mysql配置與簡單操作數(shù)據(jù)庫實(shí)例,需要的朋友可以參考下
    2017-07-07
  • 5招帶你輕松優(yōu)化MySQL count(*)查詢性能

    5招帶你輕松優(yōu)化MySQL count(*)查詢性能

    最近在公司優(yōu)化了幾個(gè)慢查詢接口的性能,總結(jié)了一些心得體會(huì)拿出來跟大家一起分享一下,文中的示例代碼講解詳細(xì),希望對(duì)大家會(huì)有所幫助
    2022-11-11
  • Mysql ERROR 1067: Invalid default value for字段問題

    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))

    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

最新評(píng)論

元江| 卓资县| 上蔡县| 会同县| 海门市| 吴堡县| 建阳市| 沁源县| 松滋市| 阿尔山市| 扶余县| 永平县| 东辽县| 绩溪县| 新龙县| 盐山县| 广昌县| 正蓝旗| 大埔区| 唐海县| 乌拉特前旗| 迁安市| 兖州市| 韶山市| 马龙县| 中宁县| 玛纳斯县| 延寿县| 克什克腾旗| 政和县| 平定县| 原平市| 伊宁市| 广西| 麟游县| 白朗县| 五华县| 鄂州市| 淮安市| 云南省| 西平县|