Use Canal to implement the process steps of MySQL master/slave synchronization
This article describes how to use Canal to achieve the effect of MySQL master/slave synchronization
Start Canal
First, set the configuration of the target node (i.e. the monitored MySQL node) in the Canal server to start the Canal service.
Start the Canal server. In Windows, double-click the startup.bat file.
Create project
Create a Spring Boot project, pom.xml file as follows:
<? 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> 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> <! --> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> </dependency> <! --canal client --> <dependency> <groupId>top.javatool</groupId> <artifactId> Ccanal spring-boot-starter</artifactId> <version>1.2.1-RELEASE</version> </dependency> </dependencies> </project>
The application.yml file is as follows, where the database is configured to write to the slave node, and the account should have data read and write permission.
spring: # Database configuration datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql:// Secondary node MySQLIP:3306/test? useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: 123456 # 2.mybatis Configure mybatis: Configuration: # show SQL log configuration log - impl: org. Apache. Ibatis. Logging. Stdout. # StdOutImpl hump named configuration map - the underscore - to - camel - case: true # 3.canal Configure canal: # ip server: 127.0.0.1:11111 destination: example
Entity class object
import lombok.Data; import java.io.Serializable; @Data public class User implements Serializable { private String id; private String username; private String password; }
canal processing class
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(" New user: "+ user); userMapper.insertUser(user); } @Override public void update(User before, User after) {System.out.println(" Update user: "+ before +" -> "+ after);} @override public void update(user before, user after) {system.out.println (" Update user:" + before + "->" + after); userMapper.updateUserById(after); } @Override public void delete(User user) {System.out.println(" Delete user: "+ user);} @override public void delete(user user) {system.out.println (" Delete user:" + user); userMapper.deleteUserById(user.getId()); }}
Corresponding to write three Mapper methods for the User table (the table corresponding to the User entity class, that is, the i_user table) operations;
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); }
Before starting the program, look at the table content of the two databases, the current is consistent, even if it is not consistent, before you want to synchronize, you should manually export/import data, so that its initial state data is consistent.
Start the program, the database begins to synchronize, view the console, and print the detection information in real time;
In this case, modify a piece of data in the i_user table of the primary node to view the console and secondary database contents.
You can see that this operation is detected by canal and updated via code to the slave library, the database configured in the code;
The contents of the i_user table in the secondary library are displayed. Data in the secondary library is synchronized successfully.
At this point, the MySQL master-slave synchronization using Canal is complete;
In addition
In addition, I have an idea, can this project package, into a jar package, when encountered short-term database synchronization scenarios, directly run this jar package can be.
For example, in daily development, we want to keep our local library synchronized with the library of the test environment, and directly modify the configuration of the test library, which may be troublesome to build the master and slave, we can use this way. You can even write a bat script, set an environment variable, and directly hit the CMD command to achieve synchronization between the two databases, which is very convenient.
Sum up
These are the details of implementing the master-slave synchronization effect of MySQL using Canal. For more information about Canal MySQL master-slave synchronization, please pay attention to other related articles of Script Home!
- Use Canal to synchronize real-time data from your PHP application to the MySQL database
- Canal listens to MySQL implementation steps
- MySQL high performance implementation of Canal data synchronization artifact
- Use canal to monitor the mysql database for real-time elasticsearch index update issues
- mysql-canal-rabbitmq installation and deployment super detailed tutorial
Related article
-
Examples of DDL, DML, and DQL statements for learning MYSQL single table operations
DML, DDL, DCL and DQL are four commonly used languages in the database, respectively, for data operation, data definition, data control and data query, the following article mainly introduces the DDL, DML and DQL statements related information about MYSQL single table operation learning, the need of friends can refer to the next2024-03-03 -
Explain MySQL data grouping in detail
This article mainly introduces the related information of MySQL data grouping, to help you better understand and use MySQL, interested friends can understand2020-12-12 -
The difference between datatime and timestamp in mysql
This article mainly introduces the difference between datatime and timestamp in mysql, which has a good reference value and hopes to help you. Let's take a look2021-02-02 -
MySQL requires an implementation method that sorts in a specific order
In MySQL, we can control the order of the data in the query results by specifying the order sort, this sort is very useful, this article to introduce, interested can understand2023-11-11 -
MySQL Database connection Exception Summary (worth collecting)
This article mainly introduces the summary of MySQL database connection exceptions, to help you better understand and learn mysql, interested friends can understand2020-08-08 -
mysql stored procedure multi-layer cursor loop nested writing sharing
This article mainly introduces the mysql stored procedure multi-layer cursor loop nesting writing, has a good reference value, I hope to help you. If there are mistakes or incomplete areas, please feel free to comment2023-07-07 -
MYSQL table optimization method summary is very comprehensive
A MYSQL table optimization article, said very good, using mysql friends can refer to the next.2010-05-05 -
Several ways for MySQL to view memory usage
This article mainly introduces several methods of MySQL to view the use of memory, mainly introduces 5 kinds of viewing methods, the article introduces very detailed through the example code, for everyone's study or work has a certain reference learning value, the need for friends to study together with Xiaobian2024-05-05 -
Teach you to use SQL to get the year, month, day of the week, day and hour
Time processing is often encountered in our daily development needs, the following article mainly introduces how to use SQL to obtain the year, month, week, day, time of the relevant information, the text through the graphic introduction is very detailed, need friends can refer to the next2022-12-12
Latest comments