Skip to main content

MySQL to PostgreSQL replica and migration

Project description

https://img.shields.io/github/issues/the4thdoctor/pg_chameleon.svg https://img.shields.io/github/forks/the4thdoctor/pg_chameleon.svg https://img.shields.io/github/stars/the4thdoctor/pg_chameleon.svg https://img.shields.io/badge/license-BSD-blue.svg https://img.shields.io/github/release/the4thdoctor/pg_chameleon https://img.shields.io/pypi/dm/pg_chameleon.svg

pg_chameleon is a MySQL to PostgreSQL replica system written in Python 3. The system use the library mysql-replication to pull the row images from MySQL which are stored into PostgreSQL as JSONB. A pl/pgsql function decodes the jsonb values and replays the changes against the PostgreSQL database.

pg_chameleon 2.0 is available on pypi

The documentation is available on pgchameleon.org

Please submit your bug reports on GitHub.

Requirements

Replica host

Operating system: Linux, FreeBSD, OpenBSD Python: CPython 3.5+

Optionals for building documentation

Origin database

MySQL 5.5+

Aurora MySQL 5.7+

Destination database

PostgreSQL 9.5+

Example scenarios

  • Analytics

  • Migrations

  • Data aggregation from multiple MySQL databases

Features

  • Read from multiple MySQL schemas and restore them it into a target PostgreSQL database. The source and target schema names can be different.

  • Setup PostgreSQL to act as a MySQL slave.

  • Support for enumerated and binary data types.

  • Basic DDL Support (CREATE/DROP/ALTER TABLE, DROP PRIMARY KEY/TRUNCATE, RENAME).

  • Discard of rubbish data coming from the replica.

  • Conservative approach to the replica. Tables which generate errors are automatically excluded from the replica.

  • Possibilty to refresh single tables or single schemas.

  • Basic replica monitoring.

  • Detach replica from MySQL for migration support.

  • Data type override (e.g. tinyint(1) to boolean)

  • Daemonised init_replica process.

  • Daemonised replica process with two separated subprocess, one for the read and one for the replay.

  • Rollbar integration

  • Support for geometrical data. Requires PostGIS on the target database.

  • Minimal locking during init_replica for transactional engines (e.g. innodb)

Caveats

The replica requires the tables to have a primary or unique key. Tables without primary/unique key are initialised during the init_replica process but not replicated.

The copy_max_memory is just an estimate. The average rows size is extracted from mysql’s informations schema and can be outdated. If the copy process fails for memory error check the failing table’s row length and the number of rows for each slice.

Python 3 is supported only from version 3.5 as required by mysql-replication .

The lag is determined using the last received event timestamp and the postgresql timestamp. If the mysql is read only the lag will increase because no replica event is coming in.

The detach replica process resets the sequences in postgres to let the database work standalone. The foreign keys from the source MySQL schema are extracted and created initially as NOT VALID. The foreign keys are created without the ON DELETE or ON UPDATE clauses. A second run tries to validate the foreign keys. If an error occurs it gets logged out according to the source configuration.

Setup

RPM PGDG

pg_chameleon is included in the PGDG RMP repository thanks to Devrim.

Please follow the instructions on https://www.postgresql.org/download/linux/redhat/

openSUSE Build Service

pg_chameleon is available on the openSUSE build Service

Currently all releases are supported except SLE_12_SP5 because of unresolved dependencies.

Virtual env setup

  • Create a virtual environment (e.g. python3 -m venv venv)

  • Activate the virtual environment (e.g. source venv/bin/activate)

  • Upgrade pip with pip install pip –upgrade

  • Install pg_chameleon with pip install pg_chameleon.

  • Create a user on mysql for the replica (e.g. usr_replica)

  • Grant access to usr on the replicated database (e.g. GRANT ALL ON sakila.* TO ‘usr_replica’;)

  • Grant RELOAD privilege to the user (e.g. GRANT RELOAD ON *.* to ‘usr_replica’;)

  • Grant REPLICATION CLIENT privilege to the user (e.g. GRANT REPLICATION CLIENT ON *.* to ‘usr_replica’;)

  • Grant REPLICATION SLAVE privilege to the user (e.g. GRANT REPLICATION SLAVE ON *.* to ‘usr_replica’;)

Configuration directory

The system wide install is now supported correctly.

The configuration is set with the command chameleon set_configuration_files in $HOME/.pg_chameleon . Inside the directory there are three subdirectories.

  • configuration is where the configuration files are stored.

  • pid is where the replica pid file is created. it can be changed in the configuration file

  • logs is where the replica logs are saved if log_dest is file. It can be changed in the configuration file

You should use config-example.yaml as template for the other configuration files. Check the configuration file reference for an overview.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pg_chameleon-2.0.18.tar.gz (344.8 kB view details)

Uploaded Source

Built Distribution

pg_chameleon-2.0.18-py3-none-any.whl (106.6 kB view details)

Uploaded Python 3

File details

Details for the file pg_chameleon-2.0.18.tar.gz.

File metadata

  • Download URL: pg_chameleon-2.0.18.tar.gz
  • Upload date:
  • Size: 344.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.11

File hashes

Hashes for pg_chameleon-2.0.18.tar.gz
Algorithm Hash digest
SHA256 46c2646438a16d3447dda27deb2197b691411d15e9f643038f7fcf20a6143d67
MD5 89c3be036ad6465432513b0e45a4eaf9
BLAKE2b-256 2630d24427dde1d9b98d3705889fadb7f7cdda0c0d545c8464c1f700fbe35844

See more details on using hashes here.

File details

Details for the file pg_chameleon-2.0.18-py3-none-any.whl.

File metadata

  • Download URL: pg_chameleon-2.0.18-py3-none-any.whl
  • Upload date:
  • Size: 106.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.11

File hashes

Hashes for pg_chameleon-2.0.18-py3-none-any.whl
Algorithm Hash digest
SHA256 b9298bb960185cb7ced49fd959244642c09172973b9f359b3f953ff23990a36a
MD5 98813b250c2fb13c8d5d6a1d8c256c70
BLAKE2b-256 8c41301844b8c377f851caef144f71c576083779b8a07c68e8ccd0ffa70daa82

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page