scisharp - SciSharp.MySQL.Replication 1.0.0-beta.4.72

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.

PM> Install-Package SciSharp.MySQL.Replication -Version 1.0.0-beta.4.72 -Source https://www.myget.org/F/scisharp/api/v3/index.json

Copy to clipboard

> nuget.exe install SciSharp.MySQL.Replication -Version 1.0.0-beta.4.72 -Source https://www.myget.org/F/scisharp/api/v3/index.json

Copy to clipboard

> dotnet add package SciSharp.MySQL.Replication --version 1.0.0-beta.4.72 --source https://www.myget.org/F/scisharp/api/v3/index.json

Copy to clipboard
<PackageReference Include="SciSharp.MySQL.Replication" Version="1.0.0-beta.4.72" />
Copy to clipboard
source https://www.myget.org/F/scisharp/api/v3/index.json

nuget SciSharp.MySQL.Replication  ~> 1.0.0-beta.4.72
Copy to clipboard

> choco install SciSharp.MySQL.Replication --version 1.0.0-beta.4.72 --source https://www.myget.org/F/scisharp/api/v2

Copy to clipboard
Import-Module PowerShellGet
Register-PSRepository -Name "scisharp" -SourceLocation "https://www.myget.org/F/scisharp/api/v2"
Install-Module -Name "SciSharp.MySQL.Replication" -RequiredVersion "1.0.0-beta.4.72" -Repository "scisharp" -AllowPreRelease
Copy to clipboard

Browse the sources in this package using Visual Studio or WinDbg by configuring the following symbol server URL: https://www.myget.org/F/scisharp/api/v2/symbolpackage/


dotnet-mysql-replication

build MyGet Version NuGet Version

A C# Implementation of MySQL replication protocol client

This library allows you to receive events like insert, update, delete with their data and raw SQL queries from MySQL.

Features

  • Connect to MySQL server as a replica
  • Parse and process binary log events in real-time
  • Support for all MySQL data types including JSON, BLOB, TEXT, etc.
  • Handle various events including:
    • Query events (raw SQL statements)
    • Table maps
    • Row events (insert, update, delete)
    • Format description events
    • Rotate events
    • XID events (transaction identifiers)
  • Checksum verification support
  • Built-in support for MySQL binary format parsing
  • Async/await first design
  • Track and save binary log position
  • Start replication from a specific binary log position

Requirements

  • .NET 6.0+ or .NET Core 3.1+
  • MySQL server with binary logging enabled
  • MySQL user with replication privileges

Installation

dotnet add package SciSharp.MySQL.Replication

Basic Usage

using SciSharp.MySQL.Replication;

var serverHost = "localhost";
var username = "root";
var password = "scisharp";
var serverId = 1; // replication server id

var client = new ReplicationClient();
var result = await client.ConnectAsync(serverHost, username, password, serverId);

if (!result.Result)
{
    Console.WriteLine($"Failed to connect: {result.Message}.");
    return;
}

client.PackageHandler += (s, p) =>
{
    Console.WriteLine(p.ToString());
    Console.WriteLine();
}

client.StartReceive();

// Keep your application running to receive events
// ...

await client.CloseAsync();

Using Async Stream API

You can use the modern C# async stream pattern to process MySQL events using GetEventLogStream():

using SciSharp.MySQL.Replication;
using SciSharp.MySQL.Replication.Events;

var client = new ReplicationClient();
var result = await client.ConnectAsync("localhost", "root", "password", 1);

if (!result.Result)
{
    Console.WriteLine($"Failed to connect: {result.Message}.");
    return;
}

// Process events as they arrive using await foreach
await foreach (var logEvent in client.GetEventLogStream())
{
    switch (logEvent)
    {
        case WriteRowsEvent writeEvent:
            Console.WriteLine($"INSERT on table: {writeEvent.TableId}");
            break;
            
        case UpdateRowsEvent updateEvent:
            Console.WriteLine($"UPDATE on table: {updateEvent.TableId}");
            break;
            
        case QueryEvent queryEvent:
            Console.WriteLine($"SQL Query: {queryEvent.Query}");
            break;
            
        // Handle other event types as needed
    }
}

await client.CloseAsync();

This approach is useful for:

  • Modern C# applications using .NET Core 3.0+
  • Processing events sequentially in a more fluent, readable way
  • Easier integration with async/await patterns
  • Avoiding event handler callback complexity

Position Tracking and Custom Starting Position

You can track the current binary log position and start from a specific position:

using SciSharp.MySQL.Replication;

var client = new ReplicationClient();

// Track position changes
client.PositionChanged += (sender, position) =>
{
    Console.WriteLine($"Current position: {position}");
    // Save position to a file, database, etc.
    File.WriteAllText("binlog-position.txt", $"{position.Filename}:{position.Position}");
};

// Start from a specific position
var startPosition = new BinlogPosition("mysql-bin.000001", 4);
var result = await client.ConnectAsync("localhost", "root", "password", 1, startPosition);

// Get current position at any time
var currentPosition = client.CurrentPosition;
Console.WriteLine($"Current log file: {currentPosition.Filename}, position: {currentPosition.Position}");

Advanced Usage

Working with Specific Events

using SciSharp.MySQL.Replication;
using SciSharp.MySQL.Replication.Events;

var client = new ReplicationClient();
// ... connect to MySQL

client.PackageHandler += (s, e) =>
{
    switch (e)
    {
        case WriteRowsEvent writeEvent:
            Console.WriteLine($"INSERT on table: {writeEvent.TableId}");
            foreach (var row in writeEvent.Rows)
            {
                // Process inserted rows
                foreach (var cell in row.Cells)
                {
                    Console.WriteLine($"  Column: {cell.ColumnIndex}, Value: {cell.Value}");
                }
            }
            break;
            
        case UpdateRowsEvent updateEvent:
            Console.WriteLine($"UPDATE on table: {updateEvent.TableId}");
            foreach (var row in updateEvent.Rows)
            {
                // Process before/after values for updated rows
                Console.WriteLine("  Before update:");
                foreach (var cell in row.BeforeUpdate)
                {
                    Console.WriteLine($"    Column: {cell.ColumnIndex}, Value: {cell.Value}");
                }
                Console.WriteLine("  After update:");
                foreach (var cell in row.AfterUpdate)
                {
                    Console.WriteLine($"    Column: {cell.ColumnIndex}, Value: {cell.Value}");
                }
            }
            break;
            
        case DeleteRowsEvent deleteEvent:
            Console.WriteLine($"DELETE on table: {deleteEvent.TableId}");
            foreach (var row in deleteEvent.Rows)
            {
                // Process deleted rows
                foreach (var cell in row.Cells)
                {
                    Console.WriteLine($"  Column: {cell.ColumnIndex}, Value: {cell.Value}");
                }
            }
            break;
            
        case QueryEvent queryEvent:
            Console.WriteLine($"SQL Query: {queryEvent.Query}");
            Console.WriteLine($"Database: {queryEvent.Schema}");
            break;

        case RotateEvent rotateEvent:
            Console.WriteLine($"Rotating to new binary log: {rotateEvent.NextBinlogFileName}");
            Console.WriteLine($"New position: {rotateEvent.RotatePosition}");
            break;
    }
};

client.StartReceive();

Setting Up MySQL for Replication

  1. Enable binary logging in your MySQL server's my.cnf or my.ini:
[mysqld]
server-id=1
log-bin=mysql-bin
binlog_format=ROW
  1. Create a user with replication privileges:
CREATE USER 'replication_user'@'%' IDENTIFIED BY 'password';
GRANT REPLICATION SLAVE ON *.* TO 'replication_user'@'%';
FLUSH PRIVILEGES;

Logging

You can provide a logger for detailed diagnostics:

using Microsoft.Extensions.Logging;

// Create a logger factory
var loggerFactory = LoggerFactory.Create(builder =>
{
    builder.AddConsole();
    builder.AddDebug();
});

var client = new ReplicationClient();
client.Logger = loggerFactory.CreateLogger<ReplicationClient>();

License

This project is licensed under the MIT License - see the LICENSE file for details.

  • .NETFramework 6.0
    • Microsoft.Extensions.Logging (>= 8.0.0)
    • MySql.Data (>= 8.4.0)
    • SuperSocket.Client (>= 2.0.0)
  • .NETFramework 7.0
    • Microsoft.Extensions.Logging (>= 8.0.0)
    • MySql.Data (>= 8.4.0)
    • SuperSocket.Client (>= 2.0.0)
  • .NETFramework 8.0
    • Microsoft.Extensions.Logging (>= 8.0.0)
    • MySql.Data (>= 8.4.0)
    • SuperSocket.Client (>= 2.0.0)
  • .NETFramework 9.0
    • Microsoft.Extensions.Logging (>= 8.0.0)
    • MySql.Data (>= 8.4.0)
    • SuperSocket.Client (>= 2.0.0)
  • .NETFramework 6.0: 6.0.0.0
  • .NETFramework 7.0: 7.0.0.0
  • .NETFramework 8.0: 8.0.0.0
  • .NETFramework 9.0: 9.0.0.0

Owners

Kerry Jiang Haiping Chen

Authors

Kerry Jiang and other contributors

Project URL

https://github.com/SciSharp/dotnet-mysql-replication

License

Unknown

Tags

MySQL replication C# client

Info

368 total downloads
11 downloads for version 1.0.0-beta.4.72
Download (180.98 KB)
Download symbols (84.62 KB)
Found on the current feed only

Package history

Version Size Last updated Downloads Mirrored?
1.0.0-beta.4.72 180.98 KB Sat, 03 May 2025 04:23:10 GMT 11
1.0.0-beta.4.70 179.78 KB Sat, 03 May 2025 03:57:39 GMT 9
1.0.0-beta.4.68 165.71 KB Sat, 03 May 2025 03:46:20 GMT 9
1.0.0-beta.3.67 165.71 KB Thu, 01 May 2025 03:40:25 GMT 16
1.0.0-beta.3.66 165.71 KB Thu, 01 May 2025 03:26:43 GMT 7
1.0.0-beta.3.64 165.72 KB Thu, 01 May 2025 02:55:18 GMT 10
1.0.0-beta.3 166.77 KB Thu, 01 May 2025 03:40:25 GMT 14
1.0.0-beta.2.63 165.72 KB Thu, 01 May 2025 02:51:17 GMT 6
1.0.0-beta.2.62 165.66 KB Thu, 01 May 2025 02:40:38 GMT 11
1.0.0-beta.2.61 165.66 KB Thu, 01 May 2025 02:34:54 GMT 11
1.0.0-beta.2.60 165.66 KB Thu, 01 May 2025 02:27:20 GMT 10
1.0.0-beta.2.59 162.98 KB Thu, 01 May 2025 02:07:42 GMT 11
1.0.0-beta.2.58 160.45 KB Wed, 30 Apr 2025 05:40:48 GMT 7
1.0.0-beta.2.57 160.34 KB Mon, 28 Apr 2025 00:49:27 GMT 11
1.0.0-beta.2.39 130.42 KB Tue, 22 Apr 2025 00:45:03 GMT 11
1.0.0-beta.2.38 130.09 KB Sat, 19 Apr 2025 23:41:25 GMT 12
1.0.0-beta.2.37 89.11 KB Sat, 19 Apr 2025 23:38:28 GMT 10
1.0.0-beta.2.35 89.1 KB Sat, 19 Apr 2025 23:30:38 GMT 14
1.0.0-beta.2.32 85.25 KB Sat, 19 Apr 2025 23:14:04 GMT 11
1.0.0-beta.2.31 84.72 KB Sat, 19 Apr 2025 22:52:03 GMT 11
1.0.0-beta.2.30 84.72 KB Sat, 19 Apr 2025 22:15:25 GMT 8
1.0.0-beta.2.28 84.72 KB Sat, 25 May 2024 22:41:27 GMT 13
1.0.0-beta.2.27 84.69 KB Wed, 15 May 2024 03:32:04 GMT 17
1.0.0-beta.2.26 83.72 KB Wed, 15 May 2024 03:28:27 GMT 13
1.0.0-beta.2.25 83.19 KB Wed, 15 May 2024 03:16:41 GMT 11
1.0.0-beta.2 84.67 KB Wed, 15 May 2024 03:39:35 GMT 16
1.0.0-beta.1.23 62.61 KB Sun, 12 Mar 2023 16:18:15 GMT 18
1.0.0-beta.1.22 62.6 KB Sat, 11 Mar 2023 19:44:28 GMT 13
1.0.0-beta.1.21 62.6 KB Sat, 11 Mar 2023 19:29:06 GMT 13
1.0.0-beta.1.20 62.59 KB Sat, 11 Mar 2023 19:21:08 GMT 12
1.0.0-beta.1 62.59 KB Sat, 11 Mar 2023 19:30:00 GMT 22