Skip to content

Commit 4f0e42f

Browse files
committed
add java with binlog demo
0 parents  commit 4f0e42f

File tree

8 files changed

+199
-0
lines changed

8 files changed

+199
-0
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
db
2+
db2
3+
.settings
4+
.classpath
5+
.project
6+
target
7+
dependency-reduced-pom.xml

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# mysql master-slave-demo
2+
3+
## config
4+
5+
* grant
6+
7+
```code
8+
GRANT replication slave ON *.* TO'root'@'%' identified by 'root';
9+
```
10+
11+
* flush privaliges
12+
13+
```code
14+
15+
```
16+
17+
* view master info
18+
19+
```code
20+
show master status
21+
```
22+
* config slave
23+
24+
```code
25+
change master to master_host='mysql',master_user='root',master_port=3306,master_password='root',master_log_file='mysql-bin.000003',master_log_pos=1027
26+
```
27+
28+
* start slave
29+
30+
```code
31+
start slave
32+
```
33+
34+
* view slave status
35+
36+
```code
37+
show slave status
38+
```
39+
40+
## java mysql binlog connector demo
41+
42+
* build
43+
44+
```code
45+
cd java-binlog
46+
mvn clean package
47+
```
48+
49+
* running
50+
51+
```code
52+
java -jar target/mysql-binglog-app.jar
53+
```

docker-compose.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
version: "3"
2+
services:
3+
mysql:
4+
image: mysql:5.7.16
5+
environment:
6+
- "MYSQL_ROOT_PASSWORD=root"
7+
- "MYSQL_DATABASE=demo"
8+
- "MYSQL_USER=demo"
9+
- "MYSQL_PASSWORD=demo"
10+
command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
11+
volumes:
12+
- "./db:/var/lib/mysql"
13+
- "./master.cnf:/etc/mysql/my.cnf"
14+
ports:
15+
- "3306:3306"
16+
mysql2:
17+
image: mysql:5.7.16
18+
environment:
19+
- "MYSQL_ROOT_PASSWORD=root"
20+
- "MYSQL_DATABASE=demo"
21+
- "MYSQL_USER=demo"
22+
- "MYSQL_PASSWORD=demo"
23+
command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
24+
volumes:
25+
- "./db2:/var/lib/mysql"
26+
- "./slave.cnf:/etc/mysql/my.cnf"
27+
ports:
28+
- "3307:3306"

init.sql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- database
2+
3+
create database demo;
4+
5+
--- table sql
6+
CREATE TABLE `appdemos` (
7+
`id` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
8+
`appname` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL
9+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
10+
11+

java-binlog/pom.xml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.dalong</groupId>
8+
<artifactId>mysql-binlog-app</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<properties>
11+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12+
<encoding>UTF-8</encoding>
13+
<java.version>1.8</java.version>
14+
<maven.compiler.source>1.8</maven.compiler.source>
15+
<maven.compiler.target>1.8</maven.compiler.target>
16+
</properties>
17+
<dependencies>
18+
<dependency>
19+
<groupId>com.github.shyiko</groupId>
20+
<artifactId>mysql-binlog-connector-java</artifactId>
21+
<version>0.18.1</version>
22+
</dependency>
23+
</dependencies>
24+
<build>
25+
<finalName>mysql-binlog-app</finalName>
26+
<plugins>
27+
<plugin>
28+
<groupId>org.apache.maven.plugins</groupId>
29+
<artifactId>maven-shade-plugin</artifactId>
30+
<version>2.3</version>
31+
<executions>
32+
<execution>
33+
<phase>package</phase>
34+
<goals>
35+
<goal>shade</goal>
36+
</goals>
37+
<configuration>
38+
<transformers>
39+
<!-- add Main-Class to manifest file -->
40+
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
41+
<mainClass>com.dalong.Application</mainClass>
42+
</transformer>
43+
</transformers>
44+
</configuration>
45+
</execution>
46+
</executions>
47+
</plugin>
48+
</plugins>
49+
50+
</build>
51+
52+
</project>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.dalong;
2+
3+
import com.github.shyiko.mysql.binlog.BinaryLogClient;
4+
import com.github.shyiko.mysql.binlog.BinaryLogClient.EventListener;
5+
import com.github.shyiko.mysql.binlog.event.Event;
6+
import com.github.shyiko.mysql.binlog.event.deserialization.EventDeserializer;
7+
8+
import java.io.IOException;
9+
10+
/**
11+
* application for mysql binlog demo
12+
*/
13+
public class Application {
14+
public static void main(String[] args) throws IOException {
15+
BinaryLogClient client = new BinaryLogClient("127.0.0.1", 3306, "root", "root");
16+
EventDeserializer eventDeserializer = new EventDeserializer();
17+
eventDeserializer.setCompatibilityMode(
18+
EventDeserializer.CompatibilityMode.DATE_AND_TIME_AS_LONG,
19+
EventDeserializer.CompatibilityMode.CHAR_AND_BINARY_AS_BYTE_ARRAY
20+
);
21+
client.setEventDeserializer(eventDeserializer);
22+
client.registerEventListener(new EventListener() {
23+
@Override
24+
public void onEvent(Event event) {
25+
System.out.println(event.toString());
26+
}
27+
});
28+
client.connect();
29+
}
30+
}

master.cnf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[mysqld]
2+
log-bin=mysql-bin
3+
log-bin-index=mysql-bin.index
4+
server-id=1
5+
sync_binlog=1
6+
binlog_format=mixed
7+
binlog-do-db=demo
8+
binlog-ignore-db=mysql
9+
binlog-ignore-db=performance_schema
10+
binlog-ignore-db=information_schema

slave.cnf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[mysqld]
2+
server-id=2
3+
log-bin=mysql-bin
4+
relay-log-index=slave-relay-bin.index
5+
relay-log=slave-relay-bin
6+
sync_master_info=1
7+
sync_relay_log=1
8+
sync_relay_log_info=1

0 commit comments

Comments
 (0)