{"id":7438,"library":"mysql-replication","title":"MySQL Replication Client","description":"mysql-replication is a pure Python implementation of the MySQL replication protocol, built on top of PyMYSQL. It enables real-time streaming of binlog events from a MySQL server, facilitating tasks such as change data capture (CDC), data synchronization, and auditing. The current version is 1.0.15, and the project maintains an active release cadence, addressing bugs and adding support for newer Python versions.","status":"active","version":"1.0.15","language":"python","source_language":"en","source_url":"https://github.com/julien-duponchelle/python-mysql-replication","tags":["database","mysql","replication","binlog","cdc","realtime"],"install":[{"cmd":"pip install mysql-replication","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Core database driver used for establishing the connection to the MySQL server.","package":"PyMySQL","optional":false}],"imports":[{"wrong":"from mysql.replication import BinLogStreamReader","symbol":"BinLogStreamReader","correct":"from pymysqlreplication import BinLogStreamReader"}],"quickstart":{"code":"import os\nfrom mysql.replication import BinLogStreamReader\nfrom mysql.replication.row_event import UpdateRowsEvent, DeleteRowsEvent, WriteRowsEvent\n\n# Configure your MySQL connection details. Use environment variables for production.\nMYSQL_HOST = os.environ.get('MYSQL_HOST', 'localhost')\nMYSQL_PORT = int(os.environ.get('MYSQL_PORT', 3306))\nMYSQL_USER = os.environ.get('MYSQL_USER', 'replication_user')\nMYSQL_PASSWORD = os.environ.get('MYSQL_PASSWORD', 'password') # Ensure user has REPLICATION SLAVE/CLIENT\n\ntry:\n    stream = BinLogStreamReader(\n        connection_settings={\n            \"host\": MYSQL_HOST,\n            \"port\": MYSQL_PORT,\n            \"user\": MYSQL_USER,\n            \"passwd\": MYSQL_PASSWORD,\n            \"charset\": \"utf8\"\n        },\n        server_id=101, # IMPORTANT: Must be a unique ID > 0 within your replication topology\n        blocking=True, # Wait for new events if no more are available\n        resume_stream=True, # Resume from last position if possible\n        log_file=None, # Start from current position\n        log_pos=None   # Start from current position\n    )\n\n    print(f\"Connected to MySQL binlog stream on {MYSQL_HOST}:{MYSQL_PORT}. Listening for events...\")\n\n    for event in stream:\n        if isinstance(event, (UpdateRowsEvent, DeleteRowsEvent, WriteRowsEvent)):\n            print(f\"\\n---\")\n            print(f\"[{type(event).__name__}] Database: {event.schema}, Table: {event.table}\")\n            for row in event.rows:\n                if isinstance(event, WriteRowsEvent):\n                    print(f\"  Inserted: {row['values']}\")\n                elif isinstance(event, UpdateRowsEvent):\n                    print(f\"  Before: {row['before_values']}\")\n                    print(f\"  After:  {row['after_values']}\")\n                elif isinstance(event, DeleteRowsEvent):\n                    print(f\"  Deleted: {row['values']}\")\n        # Add more event types (e.g., QueryEvent, RotateEvent, XidEvent) as needed\n\nexcept Exception as e:\n    print(f\"An error occurred: {e}\")\nfinally:\n    if 'stream' in locals() and stream:\n        stream.close()\n        print(\"\\nBinLogStreamReader closed.\")","lang":"python","description":"This quickstart demonstrates how to connect to a MySQL server's binlog stream and process various row-based replication events (INSERT, UPDATE, DELETE). Ensure your MySQL server is configured for replication, and the provided user has sufficient privileges (`REPLICATION SLAVE`, `REPLICATION CLIENT`). Remember to set a unique `server_id`."},"warnings":[{"fix":"Upgrade to version `1.0.15` or newer. Always review your logging configuration in production environments to prevent exposure of sensitive data.","message":"Older versions (<=1.0.14) of `mysql-replication` could potentially log sensitive connection information (e.g., passwords) if logging was not carefully configured, especially in verbose modes.","severity":"gotcha","affected_versions":"<=1.0.14"},{"fix":"Ensure `server_id` is unique for each client connecting to the binlog. A common practice is to pick a high, unused number like 101 or 1000+.","message":"The `server_id` parameter in `BinLogStreamReader` must be a unique integer greater than 0 within your entire MySQL replication topology. Using a duplicate `server_id` can lead to unexpected behavior or replication conflicts.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Configure `my.cnf` with `log_bin`, `server-id`, `binlog_format=ROW`. Grant necessary permissions: `GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'replication_user'@'%' IDENTIFIED BY 'password';` then `FLUSH PRIVILEGES;`.","message":"Your MySQL server must be explicitly configured for binlog replication. This includes enabling `log_bin`, setting a unique `server-id` in `my.cnf`, and typically `binlog_format = ROW` to get detailed row changes. The replication user also needs `REPLICATION SLAVE` and `REPLICATION CLIENT` privileges.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"search_vec":"'1.0.15':51 'activ':57 'ad':63 'address':60 'audit':46 'binlog':29,72 'bug':61 'built':17 'cadenc':59 'captur':41 'cdc':42,73 'chang':39 'client':3 'current':48 'data':40,43 'databas':69 'enabl':23 'event':30 'facilit':35 'implement':11 'maintain':55 'mysql':1,5,14,33,70 'mysql-repl':4 'newer':66 'project':54 'protocol':16 'pure':9 'pymysql':21 'python':10,67 'real':25 'real-tim':24 'realtim':74 'releas':58 'replic':2,6,15,71 'server':34 'stream':27 'support':64 'synchron':44 'task':36 'time':26 'top':19 'version':49,68","created_at":"2026-04-16T13:58:18.797043+00:00","updated_at":"2026-04-16T13:58:18.797043+00:00","problems":null,"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"1.0.15","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/julien-duponchelle/python-mysql-replication","docs":null,"changelog":null,"pypi":"https://pypi.org/project/mysql-replication/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["database"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-30","last_verified":"2026-06-30","next_check":"2026-07-30","install_tag":null}}