Skip to content

Commit e27d597

Browse files
authored
Expand MySQL documentation (#210)
* Expanded MySQL Connection documentation Added section for MySQL schema change handling * Moved MySQL standard connection guide to an accordion * Link to the required binlog configuration in the MySQL Aurora connection setup * Added MySQL to SQLite type mappings * Further tweaks to MySQL docs * Updated note for binary types in MySQL Changed MySQL Alpha -> Beta
1 parent 98838f8 commit e27d597

File tree

10 files changed

+233
-71
lines changed

10 files changed

+233
-71
lines changed

architecture/powersync-protocol.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,4 @@ Write checkpoints are used to ensure clients have synced their own changes back
6767

6868
Creating a write checkpoint is a separate operation, which is performed by the client after all data has been uploaded. It is important that this happens after the data has been written to the backend source database.
6969

70-
The server then keeps track of the current CDC stream position on the database (LSN in Postgres, resume token in MongoDB, or binlog position in MySQL), and notifies the client when the data has been replicated, as part of checkpoint data in the normal data stream.
70+
The server then keeps track of the current CDC stream position on the database (LSN in Postgres, resume token in MongoDB, or GTID + Binlog Position in MySQL), and notifies the client when the data has been replicated, as part of checkpoint data in the normal data stream.
269 KB
Loading
158 KB
Loading

installation/database-connection.mdx

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,50 @@ Also see:
162162
- [MongoDB Atlas Device Sync Migration Guide](/migration-guides/mongodb-atlas)
163163
- [MongoDB Setup](/installation/database-setup#mongodb)
164164

165-
## <Icon icon="dolphin" iconType="solid" size={32}/> MySQL (Alpha) Specifics
165+
## <Icon icon="dolphin" iconType="solid" size={32}/> MySQL (Beta) Specifics
166166

167-
1. Fill in your connection details from MySQL:
168-
1. "**Name**" can be any name for the connection.
169-
2. "**Host**" and "**Database name**" is the database to replicate.
170-
3. "**Username**" and "**Password**" maps to your database user.
171-
2. Click **"Test Connection"** and fix any errors. If have any issues connecting, reach out to our support engineers on our [Discord server](https://discord.gg/powersync) or otherwise [contact us](/resources/contact-us).
172-
1. Make sure that your database allows access to PowerSync's IPs — see [Security and IP Filtering](/installation/database-setup/security-and-ip-filtering)
173-
3. Click **"Save".**
167+
Select your MySQL hosting provider for steps to connect your newly-created PowerSync instance to your MySQL database:
168+
<AccordionGroup>
169+
<Accordion title="AWS Aurora">
170+
To enable binary logging and GTID replication in AWS Aurora, you need to create a [DB Parameter Group](https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/AuroraMySQL.Reference.ParameterGroups.html)
171+
and configure it with the necessary parameters. Follow these steps:
172+
173+
1. Navigate to [Amazon RDS console](https://console.aws.amazon.com/rds/)
174+
In the navigation pane, choose Parameter groups and click on `Create Parameter Group`.
175+
<Frame>
176+
<img src="/images/installation/aurora-create-parameter-group.png" />
177+
</Frame>
178+
2. Add all the required [binlog configuration](/installation/database-setup#binlog-configuration) parameters. For example:
179+
<Frame>
180+
<img src="/images/installation/aurora-set-gtid-flags.png" />
181+
</Frame>
182+
3. Associate your newly created parameter group with your Aurora cluster:
183+
1. In the navigation pane, choose Databases.
184+
2. Select your Aurora cluster.
185+
3. Choose Modify.
186+
4. In the DB Parameter Group section, select the parameter group you created.
187+
5. Click Continue and then Apply immediately.
188+
4. Whitelist PowerSync's IPs in your Aurora cluster's security group to allow access. See [Security and IP Filtering](/installation/database-setup/security-and-ip-filtering) for more details.
189+
5.
190+
</Accordion>
174191

175-
PowerSync deploys and configures an isolated cloud environment for you, which can take a few minutes to complete.
192+
For other providers and self-hosted databases:
193+
<Accordion title="Other / Self-hosted">
194+
Fill in your MySQL connection details:
195+
1. "**Name**", "**Host**", "**Port**", "**Database name**", "**Username**", "**Password**" are required.
196+
2. "**Name**" can be any name for the connection.
197+
3. "**Host**" the endpoint for your database.
198+
4. "**Database name**" is the default database to replicate. Additional databases are derived by qualifying the tables in the sync rules.
199+
5. "**Username**" and "**Password**" maps to your database user.
200+
6. If you want to query your source database via the PowerSync Dashboard, enable "**Allow querying data from the dashboard?**".
201+
7. Click **"Test Connection"** and fix any errors.
202+
8. Click **"Save".**
203+
204+
PowerSync deploys and configures an isolated cloud environment for you, which can take a few minutes to complete.
205+
</Accordion>
206+
</AccordionGroup>
207+
208+
<Info>
209+
Make sure that your MySQL database allows access to PowerSync's IPs — see [Security and IP Filtering](/installation/database-setup/security-and-ip-filtering)
210+
</Info>
176211

installation/database-setup.mdx

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: "Configure your backend database for PowerSync, including permissio
44
sidebarTitle: "Overview"
55
---
66

7-
Jump to: [Postgres](#postgres) | [MongoDB](#mongodb) | [MySQL](#mysql-alpha)
7+
Jump to: [Postgres](#postgres) | [MongoDB](#mongodb) | [MySQL](#mysql-beta)
88

99
import PostgresPowerSyncUser from '/snippets/postgres-powersync-user.mdx';
1010
import PostgresPowerSyncPublication from '/snippets/postgres-powersync-publication.mdx';
@@ -307,40 +307,77 @@ If you need to use private endpoints with MongoDB Atlas, see [Private Endpoints]
307307
308308
For more information on migrating from Atlas Device Sync to PowerSync, see our [migration guide](/migration-guides/mongodb-atlas).
309309
310-
## <Icon icon="dolphin" iconType="solid" size={32} /> MySQL (Alpha)
311-
312-
<Info>
313-
This section is a work in progress. More details for MySQL connections are coming soon. In the meantime, ask on our [Discord server](https://discord.gg/powersync) if you have any questions.
314-
</Info>
310+
## <Icon icon="dolphin" iconType="solid" size={32} /> MySQL (Beta)
315311
316312
<Check>
317313
**Version compatibility**: PowerSync requires MySQL version 5.7 or greater.
318314
</Check>
319315
320-
MySQL connections use the [binary log](https://dev.mysql.com/doc/refman/8.4/en/binary-log.html) to replicate changes.
316+
PowerSync reads from the MySQL [binary log](https://dev.mysql.com/doc/refman/8.4/en/binary-log.html) to replicate changes. We use a modified version of the [Zongji MySQL](https://github.com/powersync-ja/powersync-mysql-zongji) binlog listener to achieve this.
321317
322-
Generally, this requires the following config:
318+
### Binlog Configuration
323319
324-
- `gtid_mode` : `ON`
325-
- `enforce_gtid_consistency` : `ON`
326-
- `binlog_format` : `ROW`
320+
To ensure that PowerSync can read the binary log, you need to configure your MySQL server to enable binary logging and configure it with the following server command options:
321+
- [server_id](https://dev.mysql.com/doc/refman/8.4/en/replication-options.html#sysvar_server_id): Uniquely identifies the MySQL server instance in the replication topology. Default value is **1**.
322+
- [log_bin](https://dev.mysql.com/doc/refman/8.4/en/replication-options-binary-log.html#sysvar_log_bin): **ON**. Enables binary logging. Default is **ON** for MySQL 8.0 and later, but **OFF** for MySQL 5.7.
323+
- [enforce_gtid_consistency](https://dev.mysql.com/doc/refman/8.4/en/replication-options-gtids.html#sysvar_enforce_gtid_consistency): **ON**. Enforces GTID consistency. Default is **OFF**.
324+
- [gtid_mode](https://dev.mysql.com/doc/refman/8.4/en/replication-options-gtids.html#sysvar_gtid_mode): **ON**. Enables GTID based logging. Default is **OFF**.
325+
- [binlog_format](https://dev.mysql.com/doc/refman/8.4/en/replication-options-binary-log.html#sysvar_binlog_format): **ROW**. Sets the binary log format to row-based replication. This is required for PowerSync to correctly replicate changes. Default is **ROW**.
327326
328-
PowerSync also requires a user with replication permissions on the database. An example:
327+
These can be specified in a MySQL [option file](https://dev.mysql.com/doc/refman/8.4/en/option-files.html):
328+
```
329+
server_id=<Unique Integer Value>
330+
log_bin=ON
331+
enforce_gtid_consistency=ON
332+
gtid_mode=ON
333+
binlog_format=ROW
334+
```
335+
336+
### Database User Configuration
337+
PowerSync also requires a MySQL user with **REPLICATION** and **SELECT** permission on the source databases. These can be added by running the following SQL commands:
329338
330339
```sql
331340
-- Create a user with necessary privileges
332-
CREATE USER 'repl_user'@'%' IDENTIFIED BY 'good_password';
341+
CREATE USER 'repl_user'@'%' IDENTIFIED BY '<password>';
333342

334343
-- Grant replication client privilege
335344
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'repl_user'@'%';
336345

337-
-- Grant access to the specific database
338-
GRANT ALL PRIVILEGES ON powersync.* TO 'repl_user'@'%';
346+
-- Grant select access to the specific database
347+
GRANT SELECT ON <source_database>.* TO 'repl_user'@'%';
339348

340349
-- Apply changes
341350
FLUSH PRIVILEGES;
342351
```
343352
353+
It is possible to constrain the MySQL user further and limit access to specific tables. Care should be taken to ensure that all the tables in the sync rules are included in the grants.
354+
```sql
355+
-- Grant select to the users and the invoices tables in the source database
356+
GRANT SELECT ON <source_database>.users TO 'repl_user'@'%';
357+
GRANT SELECT ON <source_database>.invoices TO 'repl_user'@'%';
358+
359+
-- Apply changes
360+
FLUSH PRIVILEGES;
361+
```
362+
363+
### Additional Configuration (optional)
364+
#### Binlog
365+
366+
The binlog can be configured to limit logging to specific databases. By default, events for tables in all the databases on the MySQL server will be logged.
367+
- [binlog-do-db](https://dev.mysql.com/doc/refman/8.4/en/replication-options-binary-log.html#option_mysqld_binlog-do-db): Only updates for tables in the specified database will be logged.
368+
- [binlog-ignore-db](https://dev.mysql.com/doc/refman/8.4/en/replication-options-binary-log.html#option_mysqld_binlog-ignore-db): No updates for tables in the specified database will be logged.
369+
370+
Examples:
371+
```
372+
# Only row events for tables in the user_db and invoices_db databases will appear in the binlog.
373+
binlog-do-db=user_db
374+
binlog-do-db=invoices_db
375+
```
376+
```
377+
# Row events for tables in the user_db will be ignored. Events for any other database will be logged.
378+
binlog-ignore-db=user_db
379+
```
380+
344381
## Next Step
345382
346383
Next, connect PowerSync to your database:

installation/quickstart-guide.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: "Quickstart Guide / Installation Overview"
33
sidebarTitle: "Quickstart / Overview"
44
---
55

6-
PowerSync is designed to be stack agnostic, and currently supports [Postgres](/installation/database-setup#postgres), [MongoDB](/installation/database-setup#mongodb) and [MySQL](/installation/database-setup#mysql-alpha) (alpha) as the backend source database, and has the following official client-side SDKs available:
6+
PowerSync is designed to be stack agnostic, and currently supports [Postgres](/installation/database-setup#postgres), [MongoDB](/installation/database-setup#mongodb) and [MySQL](/installation/database-setup#mysql-beta) (Beta) as the backend source database, and has the following official client-side SDKs available:
77
- [**Flutter**](/client-sdk-references/flutter) (mobile and [web](/client-sdk-references/flutter/flutter-web-support))
88
- [**React Native**](/client-sdk-references/react-native-and-expo) (mobile and [web](/client-sdk-references/react-native-and-expo/react-native-web-support))
99
- [**JavaScript Web**](/client-sdk-references/javascript-web) (including integrations for React & Vue)

intro/powersync-overview.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ PowerSync is designed to be backend database agnostic, and currently supports:
2222

2323
<Card title="MongoDB" icon="leaf" href="/installation/database-setup/#mongodb" />
2424

25-
<Card title="MySQL (Alpha)" icon="dolphin" href="/installation/database-setup/#mysql-alpha" />
25+
<Card title="MySQL (Beta)" icon="dolphin" href="/installation/database-setup/#mysql-beta" />
2626
</CardGroup>
2727

2828
### Supported Client SDKs

resources/feature-status.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Below is a summary of the current main PowerSync features and their release stat
4545
| **Category / Item** | **Status** |
4646
| -------------------------------------------------- | -------------- |
4747
| **Database Connectors** | |
48-
| MySQL | Alpha |
48+
| MySQL | Beta |
4949
| MongoDB | V1 |
5050
| Postgres | V1 |
5151
| | |

usage/lifecycle-maintenance/implementing-schema-changes.mdx

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,75 @@ Renaming an unsynced collection to a name that is included in the Sync Rules tri
149149
Circular renames (e.g., renaming `todos``todos_old``todos`) are not directly supported. To reprocess the database after such changes, a Sync Rules update must be deployed.
150150

151151

152-
## <Icon icon="dolphin" iconType="solid" size={32} /> MySQL (Alpha) Specifics
152+
## <Icon icon="dolphin" iconType="solid" size={32} /> MySQL (Beta) Specifics
153+
PowerSync keeps the [sync buckets](/usage/sync-rules/organize-data-into-buckets) up to date with any incremental data changes as recorded in the MySQL [binary log](https://dev.mysql.com/doc/refman/8.4/en/binary-log.html).
154+
The binary log also provides DDL (Data Definition Language) query updates, which include:
153155

154-
<Info>
155-
This section is a work in progress. More details for MySQL connections are coming soon. In the meantime, ask on our [Discord server](https://discord.gg/powersync) if you have any questions.
156-
</Info>
156+
1. Creating, dropping or renaming tables.
157+
158+
2. Truncating tables. (Not technically a schema change, but they appear in the query updates regardless.)
159+
160+
3. Changing replica identity of a table. (Creation, deletion or modification of primary keys, unique indexes, etc.)
161+
162+
4. Adding, dropping, renaming or changing the types of columns.
163+
164+
For MySQL, PowerSync detects schema changes by parsing the DDL queries in the binary log. It may not always be possible to parse the DDL queries correctly, especially if they are complex or use non-standard syntax.
165+
In such cases, PowerSync will ignore the schema change, but will log a warning with the schema change query. If required, the schema change would then need to be manually
166+
handled by redeploying the sync rules. This triggers a re-replication.
167+
168+
### MySQL schema changes affecting Sync Rules
169+
170+
#### DROP table
171+
172+
PowerSync will detect when a table is dropped, and automatically remove the data from the sync buckets.
173+
174+
#### CREATE table
175+
176+
Table creation is detected and handled the first time row events for the new table appear on the binary log.
177+
178+
#### TRUNCATE table
179+
180+
PowerSync will detect truncate statements in the binary log, and consequently remove all data from the sync buckets for that table.
181+
182+
#### RENAME table
183+
184+
A renamed table is handled similarly to dropping the old table, and then creating a new table with existing data under the new name.
185+
This may be a slow operation if the table is large, since the "new" table has to be re-replicated. Replication will be blocked until the new table is replicated.
186+
187+
#### Change REPLICA IDENTITY
188+
189+
The replica identity of a table is considered to be changed if either:
190+
191+
1. The type of replica identity changes (`DEFAULT`, `INDEX`, `FULL`, `NOTHING`).
192+
193+
2. The name or type of columns which form part of the replica identity changes.
194+
195+
The latter can happen if:
196+
197+
1. Using `REPLICA IDENTITY FULL`, and any column is added, removed, renamed, or the type changed.
198+
199+
2. Using `REPLICA IDENTITY DEFAULT`, and the type of any column in the primary key is changed.
200+
201+
3. Using `REPLICA IDENTITY INDEX`, and the type of any column in the replica index is changed.
202+
203+
4. The primary key or replica index is removed or changed.
204+
205+
When the replication identity changes, the entire table is replicated again. This may be a slow operation if the table is large, and all other replication will be blocked until the table is replicated again.
206+
207+
Sync rules affected by schema changes will fail "soft" — an alert would be generated, but the system will continue processing changes.
208+
209+
#### Column changes
210+
211+
Column changes such as adding, dropping, renaming columns, or changing column types, are detected by PowerSync but will generally not result in re-replication. (Unless the replica identity was affected as described above).
212+
213+
Adding a column with a `NULL` default value will generally not cause issues. Existing records will have a missing value instead of `NULL` value, but those are generally treated the same on the client.
214+
215+
Adding a column with a different default value, whether it's a static or computed value, will not have this default automatically replicated for existing rows. To propagate this value, make an update to every existing row.
216+
217+
Removing a column will not have the values automatically removed for existing rows on PowerSync. To propagate the change, make an update to every existing row.
218+
219+
Changing a column type, and/or changing the default value of a column using an `ALTER TABLE` statement, will not be automatically replicated to PowerSync.
220+
In some cases, the change will have no effect on PowerSync (for example, changing between `VARCHAR` and `TEXT` types). When the values are expected to change, make an update to every existing row to propagate the changes.
157221

158222
## See Also
159223

0 commit comments

Comments
 (0)