Following MySQL
Reference, rendered from docs/chloedb/mysql.md.
These docs follow main, which can be ahead of the latest release, 0.3.11 (its docs): a change made after it says so where it is described.
chloedb-server follow-mysql keeps ChloeDB in step with a MySQL database by its binary log, as a replica reads it: it copies the tables it follows as of one snapshot, then applies every insert, update, delete, TRUNCATE and new column a moment after MySQL commits it, so ChloeDB serves search over the rows MySQL holds without a Debezium and Kafka pipeline in between. It speaks MySQL's replication protocol itself (MySQL 8.0 and later), and keeps what it applied in ChloeDB's _sources, in the same writes as the rows, so a follower stopped and started again goes on from there.
The same follower runs inside the server as a mysql connector, started and stopped on Data > Connectors, with its password kept as the connector's secret.
Set up MySQL#
The follower reads row events of whole rows, which are MySQL's defaults since 8.0 (log_bin on, binlog_format = ROW, binlog_row_image = FULL); it says which is off when one is. It signs in as a user that may read the binlog and the tables:
CREATE USER 'chloedb'@'%' IDENTIFIED BY '...'; GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'chloedb'@'%'; GRANT SELECT ON shop.* TO 'chloedb'@'%';
caching_sha2_password (MySQL's default) and mysql_native_password sign-ins both work, over TLS or, on a plain connection, with the server's RSA key. On managed MySQL the binlog is a setting: RDS and Aurora keep it while backups are on (set binlog_format to ROW in the parameter group, and CALL mysql.rds_set_configuration('binlog retention hours', 24) so a stopped follower finds its position); Cloud SQL writes it with point-in-time recovery on.
Each followed table needs a primary key, by which the follower updates and deletes its rows; a table without one is refused with the statement that adds one. A key of several columns becomes one KEYWORD column _key in ChloeDB, their values joined by : (42:en), beside the columns themselves.
Run it#
MYSQL_PWD=... chloedb-server follow-mysql \ --source 'mysql://chloedb@db.example:3306/shop?ssl-mode=verify_identity' \ --target http://localhost:7700 --name shop
| option | |
|---|---|
--source |
the database's URL (or CHLOEDB_MYSQL_URL), naming the database whose tables it follows; MYSQL_PWD gives the password (a password in the URL works too, but stays in the process list); ssl-mode is disabled, required, verify_ca or verify_identity; without one, required for a database on another machine and disabled on a loopback address, and --source-ca a CA certificate (PEM) to check the server's by |
--tables |
the tables it follows, joined by commas: orders, shop.orders or shop.* (default every table of the URL's database). A table of another database is named <database>_<table> in ChloeDB |
--target |
ChloeDB's HTTP URL: a server, or a cluster's coordinator (--target-token or CHLOEDB_CLUSTER_TOKEN, --target-ca) |
--name |
the name ChloeDB knows the database by, which its marks in _sources name |
--server-id |
the server id it reads the binlog as, which must differ from every replica's of that MySQL (default a number made from --name) |
--batch |
the most rows of one write (1,000) |
--copy |
copy everything again, as of a new snapshot |
--release |
release the tables it followed (they take other writes again) and exit |
The first run notes the binlog's position (SHOW BINARY LOG STATUS, or SHOW MASTER STATUS before 8.4), opens a consistent snapshot and copies each table as of it while MySQL goes on taking writes, then reads the binlog from the position noted. A ChloeDB table it lacks is made from the MySQL table; one that exists keeps its schema, so make it first to choose the types, analyzers or an embedded vector, as for Postgres.
Stop it whenever: started again it reads on from the last transaction ChloeDB holds. Unlike a Postgres slot, MySQL keeps nothing for a stopped follower: it purges its binlog files on its own schedule (binlog_expire_logs_seconds, 30 days by default), and a follower whose position is in a file purged since copies its tables again, as of a new snapshot, and says so in its log. A copy made again (that one, or --copy) writes over the rows ChloeDB holds, which stay readable, and then deletes those whose keys MySQL no longer has.
What it applies#
- Each change at least once, in order. The changes between the position noted and the snapshot are applied again over the copy, and after a restart those since the last transaction ChloeDB holds; since an insert or update is an upsert of the whole row and a delete is by key, the rows end as MySQL has them, a moment behind while it catches up. Changes are written in batches of up to
--batchrows, each batch that ends a transaction with the position after it, so a reader can see part of a large transaction, as it can see part of any write across a cluster's shards. - Inserts and updates are upserts of the whole row by key; an update that changes the key deletes the old row. Deletes are by key.
TRUNCATEdeletes every row of the table. - Columns added in MySQL (
ALTER TABLE ... ADD COLUMN) are added to the ChloeDB table. A column dropped stays in ChloeDB,NULLin rows written since. - Tables created that
--tablesmatches are followed from their first row; a table dropped is no longer followed and its ChloeDB rows stay. - ChloeDB's tables the follower writes take no other writes, as a Postgres follower's (what it applies): an
INSERT,UPDATE,DELETE,POST /v1/write,DROP TABLEor a column dropped or renamed is refused (409,followed_table). Write the change in MySQL.--release, or deleting the connector, lets them take writes again.
How MySQL columns become fields#
| MySQL | ChloeDB |
|---|---|
tinyint(1), boolean |
BOOL |
tinyint, smallint, mediumint, int, bigint, year, bit(n) |
INT |
decimal, float, double |
FLOAT |
char, varchar, text types |
TEXT (KEYWORD as the key) |
enum |
KEYWORD, the member's name |
set |
KEYWORD[], the members' names |
date, datetime, timestamp |
TIMESTAMP (UTC; a zero date is NULL) |
time |
KEYWORD, as HH:MM:SS |
json |
JSON (a value that is not an object becomes {"value": ...}) |
binary, varbinary, blob types, spatial types |
left out |
The follower's session is in UTC (time_zone = '+00:00'), so a timestamp is read as the instant MySQL holds; a datetime, which has no zone, is taken as UTC.
How far behind it is#
Every 2 seconds the follower tells ChloeDB how it stands, as a Postgres follower does (how far behind): Data > Connectors lists it among the followers, /metrics has chloedb_follow_lag_seconds, chloedb_follow_behind_bytes and chloedb_follow_up with kind="mysql", and the built-in alert follow-behind reads them. Its status has state (copying, following, retrying, failed, stopped), applied_position (the binlog file and position of the last transaction written), mysql_position (MySQL's own), behind_bytes (the binlog between the two, across files) and lag_ms (while it is behind, since MySQL wrote the last change it applied).
How a restart resumes#
What the follower applied is in ChloeDB's _sources:
mysql/<name>/copyis when the copy it follows from began; a copy made again names the other marks afresh, undermysql/<name>/<copy>/.mysql/<name>/<copy>is the end of the last MySQL transaction applied, as the binlog file's number times 2³² plus the position (binlog.000042at 1234 is42 << 32 | 1234)..../copiedsays the copy finished, and.../t/<id>/<table>which MySQL table each ChloeDB table follows (odd while it follows, even once released).
Run one follower per --name and one per --server-id on a MySQL: MySQL drops a replica's connection when another signs in with its id. Under systemd or in a container, run it as the Postgres follower is run, with MYSQL_PWD in its environment file.