{"id":28639384,"url":"https://github.com/scisharp/dotnet-mysql-replication","last_synced_at":"2025-06-12T19:40:23.545Z","repository":{"id":46789103,"uuid":"210571917","full_name":"SciSharp/dotnet-mysql-replication","owner":"SciSharp","description":"C# Implementation of MySQL replication client","archived":false,"fork":false,"pushed_at":"2025-05-03T04:22:18.000Z","size":336,"stargazers_count":52,"open_issues_count":2,"forks_count":17,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-06-11T19:53:14.239Z","etag":null,"topics":["client","csharp","mysql","replication"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SciSharp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2019-09-24T10:08:17.000Z","updated_at":"2025-06-04T02:19:35.000Z","dependencies_parsed_at":"2024-01-13T17:47:04.512Z","dependency_job_id":"5f82de88-9f5f-4d43-acdc-7b82eefef544","html_url":"https://github.com/SciSharp/dotnet-mysql-replication","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/SciSharp/dotnet-mysql-replication","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SciSharp%2Fdotnet-mysql-replication","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SciSharp%2Fdotnet-mysql-replication/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SciSharp%2Fdotnet-mysql-replication/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SciSharp%2Fdotnet-mysql-replication/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SciSharp","download_url":"https://codeload.github.com/SciSharp/dotnet-mysql-replication/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SciSharp%2Fdotnet-mysql-replication/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259520285,"owners_count":22870415,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["client","csharp","mysql","replication"],"created_at":"2025-06-12T19:40:22.820Z","updated_at":"2025-06-12T19:40:23.526Z","avatar_url":"https://github.com/SciSharp.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# dotnet-mysql-replication\n\n[![build](https://github.com/SciSharp/dotnet-mysql-replication/actions/workflows/build.yaml/badge.svg)](https://github.com/SciSharp/dotnet-mysql-replication/actions/workflows/build.yaml)\n[![MyGet Version](https://img.shields.io/myget/scisharp/vpre/SciSharp.MySQL.Replication)](https://www.myget.org/feed/scisharp/package/nuget/SciSharp.MySQL.Replication)\n[![NuGet Version](https://img.shields.io/nuget/v/SciSharp.MySQL.Replication.svg?style=flat)](https://www.nuget.org/packages/SciSharp.MySQL.Replication/)\n\nA C# Implementation of MySQL replication protocol client\n\nThis library allows you to receive events like insert, update, delete with their data and raw SQL queries from MySQL.\n\n## Features\n\n- Connect to MySQL server as a replica\n- Parse and process binary log events in real-time\n- Support for all MySQL data types including JSON, BLOB, TEXT, etc.\n- Handle various events including:\n  - Query events (raw SQL statements)\n  - Table maps\n  - Row events (insert, update, delete)\n  - Format description events\n  - Rotate events\n  - XID events (transaction identifiers)\n- Checksum verification support\n- Built-in support for MySQL binary format parsing\n- Async/await first design\n- Track and save binary log position \n- Start replication from a specific binary log position\n\n## Requirements\n\n- .NET 6.0+ or .NET Core 3.1+\n- MySQL server with binary logging enabled\n- MySQL user with replication privileges\n\n## Installation\n\n```\ndotnet add package SciSharp.MySQL.Replication\n```\n\n## Basic Usage\n\n```csharp\nusing SciSharp.MySQL.Replication;\n\nvar serverHost = \"localhost\";\nvar username = \"root\";\nvar password = \"scisharp\";\nvar serverId = 1; // replication server id\n\nvar client = new ReplicationClient();\nvar result = await client.ConnectAsync(serverHost, username, password, serverId);\n\nif (!result.Result)\n{\n    Console.WriteLine($\"Failed to connect: {result.Message}.\");\n    return;\n}\n\nclient.PackageHandler += (s, p) =\u003e\n{\n    Console.WriteLine(p.ToString());\n    Console.WriteLine();\n}\n\nclient.StartReceive();\n\n// Keep your application running to receive events\n// ...\n\nawait client.CloseAsync();\n```\n\n## Using Async Stream API\n\nYou can use the modern C# async stream pattern to process MySQL events using `GetEventLogStream()`:\n\n```csharp\nusing SciSharp.MySQL.Replication;\nusing SciSharp.MySQL.Replication.Events;\n\nvar client = new ReplicationClient();\nvar result = await client.ConnectAsync(\"localhost\", \"root\", \"password\", 1);\n\nif (!result.Result)\n{\n    Console.WriteLine($\"Failed to connect: {result.Message}.\");\n    return;\n}\n\n// Process events as they arrive using await foreach\nawait foreach (var logEvent in client.GetEventLogStream())\n{\n    switch (logEvent)\n    {\n        case WriteRowsEvent writeEvent:\n            Console.WriteLine($\"INSERT on table: {writeEvent.TableId}\");\n            break;\n            \n        case UpdateRowsEvent updateEvent:\n            Console.WriteLine($\"UPDATE on table: {updateEvent.TableId}\");\n            break;\n            \n        case QueryEvent queryEvent:\n            Console.WriteLine($\"SQL Query: {queryEvent.Query}\");\n            break;\n            \n        // Handle other event types as needed\n    }\n}\n\nawait client.CloseAsync();\n```\n\nThis approach is useful for:\n- Modern C# applications using .NET Core 3.0+\n- Processing events sequentially in a more fluent, readable way\n- Easier integration with async/await patterns\n- Avoiding event handler callback complexity\n\n## Position Tracking and Custom Starting Position\n\nYou can track the current binary log position and start from a specific position:\n\n```csharp\nusing SciSharp.MySQL.Replication;\n\nvar client = new ReplicationClient();\n\n// Track position changes\nclient.PositionChanged += (sender, position) =\u003e\n{\n    Console.WriteLine($\"Current position: {position}\");\n    // Save position to a file, database, etc.\n    File.WriteAllText(\"binlog-position.txt\", $\"{position.Filename}:{position.Position}\");\n};\n\n// Start from a specific position\nvar startPosition = new BinlogPosition(\"mysql-bin.000001\", 4);\nvar result = await client.ConnectAsync(\"localhost\", \"root\", \"password\", 1, startPosition);\n\n// Get current position at any time\nvar currentPosition = client.CurrentPosition;\nConsole.WriteLine($\"Current log file: {currentPosition.Filename}, position: {currentPosition.Position}\");\n```\n\n## Advanced Usage\n\n### Working with Specific Events\n\n```csharp\nusing SciSharp.MySQL.Replication;\nusing SciSharp.MySQL.Replication.Events;\n\nvar client = new ReplicationClient();\n// ... connect to MySQL\n\nclient.PackageHandler += (s, e) =\u003e\n{\n    switch (e)\n    {\n        case WriteRowsEvent writeEvent:\n            Console.WriteLine($\"INSERT on table: {writeEvent.TableId}\");\n            foreach (var row in writeEvent.Rows)\n            {\n                // Process inserted rows\n                foreach (var cell in row.Cells)\n                {\n                    Console.WriteLine($\"  Column: {cell.ColumnIndex}, Value: {cell.Value}\");\n                }\n            }\n            break;\n            \n        case UpdateRowsEvent updateEvent:\n            Console.WriteLine($\"UPDATE on table: {updateEvent.TableId}\");\n            foreach (var row in updateEvent.Rows)\n            {\n                // Process before/after values for updated rows\n                Console.WriteLine(\"  Before update:\");\n                foreach (var cell in row.BeforeUpdate)\n                {\n                    Console.WriteLine($\"    Column: {cell.ColumnIndex}, Value: {cell.Value}\");\n                }\n                Console.WriteLine(\"  After update:\");\n                foreach (var cell in row.AfterUpdate)\n                {\n                    Console.WriteLine($\"    Column: {cell.ColumnIndex}, Value: {cell.Value}\");\n                }\n            }\n            break;\n            \n        case DeleteRowsEvent deleteEvent:\n            Console.WriteLine($\"DELETE on table: {deleteEvent.TableId}\");\n            foreach (var row in deleteEvent.Rows)\n            {\n                // Process deleted rows\n                foreach (var cell in row.Cells)\n                {\n                    Console.WriteLine($\"  Column: {cell.ColumnIndex}, Value: {cell.Value}\");\n                }\n            }\n            break;\n            \n        case QueryEvent queryEvent:\n            Console.WriteLine($\"SQL Query: {queryEvent.Query}\");\n            Console.WriteLine($\"Database: {queryEvent.Schema}\");\n            break;\n\n        case RotateEvent rotateEvent:\n            Console.WriteLine($\"Rotating to new binary log: {rotateEvent.NextBinlogFileName}\");\n            Console.WriteLine($\"New position: {rotateEvent.RotatePosition}\");\n            break;\n    }\n};\n\nclient.StartReceive();\n```\n\n### Setting Up MySQL for Replication\n\n1. Enable binary logging in your MySQL server's `my.cnf` or `my.ini`:\n\n```\n[mysqld]\nserver-id=1\nlog-bin=mysql-bin\nbinlog_format=ROW\n```\n\n2. Create a user with replication privileges:\n\n```sql\nCREATE USER 'replication_user'@'%' IDENTIFIED BY 'password';\nGRANT REPLICATION SLAVE ON *.* TO 'replication_user'@'%';\nFLUSH PRIVILEGES;\n```\n\n### Logging\n\nYou can provide a logger for detailed diagnostics:\n\n```csharp\nusing Microsoft.Extensions.Logging;\n\n// Create a logger factory\nvar loggerFactory = LoggerFactory.Create(builder =\u003e\n{\n    builder.AddConsole();\n    builder.AddDebug();\n});\n\nvar client = new ReplicationClient();\nclient.Logger = loggerFactory.CreateLogger\u003cReplicationClient\u003e();\n```\n\n## License\n\nThis project is licensed under the MIT License - see the LICENSE file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscisharp%2Fdotnet-mysql-replication","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fscisharp%2Fdotnet-mysql-replication","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscisharp%2Fdotnet-mysql-replication/lists"}