Skip to content

Commit 29c1d60

Browse files
committed
updated readme
1 parent 75be44d commit 29c1d60

File tree

1 file changed

+140
-4
lines changed

1 file changed

+140
-4
lines changed

README.md

Lines changed: 140 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,43 @@
66

77
dotnet-mysql-replication is a C# Implementation of MySQL replication protocol client. This allows you to receive events like insert, update, delete with their data and raw SQL queries from MySQL.
88

9-
## Usage
9+
## Features
1010

11-
```csharp
11+
- Connect to MySQL server as a replica
12+
- Parse and process binary log events in real-time
13+
- Support for all MySQL data types including JSON, BLOB, TEXT, etc.
14+
- Handle various events including:
15+
- Query events (raw SQL statements)
16+
- Table maps
17+
- Row events (insert, update, delete)
18+
- Format description events
19+
- Rotate events
20+
- XID events (transaction identifiers)
21+
- Checksum verification support
22+
- Built-in support for MySQL binary format parsing
23+
- Async/await first design
24+
25+
## Requirements
26+
27+
- .NET 6.0+ or .NET Core 3.1+
28+
- MySQL server with binary logging enabled
29+
- MySQL user with replication privileges
30+
31+
## Installation
32+
33+
```
34+
dotnet add package SciSharp.MySQL.Replication
35+
```
36+
37+
## Basic Usage
1238

39+
```csharp
1340
using SciSharp.MySQL.Replication;
1441

1542
var serverHost = "localhost";
1643
var username = "root";
1744
var password = "scisharp";
18-
var serverId = 1; // replication server id
45+
var serverId = 1; // replication server id
1946
2047
var client = new ReplicationClient();
2148
var result = await client.ConnectAsync(serverHost, username, password, serverId);
@@ -34,8 +61,117 @@ client.PackageHandler += (s, p) =>
3461

3562
client.StartReceive();
3663

37-
//...
64+
// Keep your application running to receive events
65+
// ...
3866
3967
await client.CloseAsync();
68+
```
4069

70+
## Advanced Usage
71+
72+
### Working with Specific Events
73+
74+
```csharp
75+
using SciSharp.MySQL.Replication;
76+
using SciSharp.MySQL.Replication.Events;
77+
78+
var client = new ReplicationClient();
79+
// ... connect to MySQL
80+
81+
client.PackageHandler += (s, e) =>
82+
{
83+
switch (e)
84+
{
85+
case WriteRowsEvent writeEvent:
86+
Console.WriteLine($"INSERT on table: {writeEvent.TableId}");
87+
foreach (var row in writeEvent.Rows)
88+
{
89+
// Process inserted rows
90+
foreach (var cell in row.Cells)
91+
{
92+
Console.WriteLine($" Column: {cell.ColumnIndex}, Value: {cell.Value}");
93+
}
94+
}
95+
break;
96+
97+
case UpdateRowsEvent updateEvent:
98+
Console.WriteLine($"UPDATE on table: {updateEvent.TableId}");
99+
foreach (var row in updateEvent.Rows)
100+
{
101+
// Process before/after values for updated rows
102+
Console.WriteLine(" Before update:");
103+
foreach (var cell in row.BeforeUpdate)
104+
{
105+
Console.WriteLine($" Column: {cell.ColumnIndex}, Value: {cell.Value}");
106+
}
107+
108+
Console.WriteLine(" After update:");
109+
foreach (var cell in row.AfterUpdate)
110+
{
111+
Console.WriteLine($" Column: {cell.ColumnIndex}, Value: {cell.Value}");
112+
}
113+
}
114+
break;
115+
116+
case DeleteRowsEvent deleteEvent:
117+
Console.WriteLine($"DELETE on table: {deleteEvent.TableId}");
118+
foreach (var row in deleteEvent.Rows)
119+
{
120+
// Process deleted rows
121+
foreach (var cell in row.Cells)
122+
{
123+
Console.WriteLine($" Column: {cell.ColumnIndex}, Value: {cell.Value}");
124+
}
125+
}
126+
break;
127+
128+
case QueryEvent queryEvent:
129+
Console.WriteLine($"SQL Query: {queryEvent.Query}");
130+
Console.WriteLine($"Database: {queryEvent.DatabaseName}");
131+
break;
132+
}
133+
};
134+
135+
client.StartReceive();
41136
```
137+
138+
### Setting Up MySQL for Replication
139+
140+
1. Enable binary logging in your MySQL server's `my.cnf` or `my.ini`:
141+
142+
```
143+
[mysqld]
144+
server-id=1
145+
log-bin=mysql-bin
146+
binlog_format=ROW
147+
```
148+
149+
2. Create a user with replication privileges:
150+
151+
```sql
152+
CREATE USER 'replication_user'@'%' IDENTIFIED BY 'password';
153+
GRANT REPLICATION SLAVE ON *.* TO 'replication_user'@'%';
154+
FLUSH PRIVILEGES;
155+
```
156+
157+
### Logging
158+
159+
You can provide a logger for detailed diagnostics:
160+
161+
```csharp
162+
using Microsoft.Extensions.Logging;
163+
164+
// Create a logger factory
165+
var loggerFactory = LoggerFactory.Create(builder =>
166+
{
167+
builder.AddConsole();
168+
builder.AddDebug();
169+
});
170+
171+
var client = new ReplicationClient();
172+
client.Logger = loggerFactory.CreateLogger<ReplicationClient>();
173+
```
174+
175+
## License
176+
177+
This project is licensed under the MIT License - see the LICENSE file for details.

0 commit comments

Comments
 (0)