MySQL to Studio Sanitiser
Make a MySQL export of a WordPress database importable into a WordPress Studio
local site (SQLite through the SQLite Database Integration plugin). This skill
writes a cleaned copy beside the original and reports every change.
It packages the fixes for real failed imports:
- 2026-10-13: Studio rejected a GTID replication statement, then raw
_binary literals in WP All Import's wp_pmxi_hash table.
- 2026-10-14: a phpMyAdmin export from MariaDB 13 failed on
`DEFAULT x AS 0000... ` in ten Wordfence tables. A hand-made native
SQLite conversion of the same dump then failed on its first line,
PRAGMA foreign_keys=OFF.
When to use this skill
- A Studio import fails and
~/Library/Logs/Studio/shows
Error during import of ... Could not execute statement: ....
- Before importing a live or staging export into Studio, to catch the known
problems first.
Rules
- The output must stay MySQL syntax. Studio's importer (and the
phpMyAdmin bundled with Studio) send every statement through the SQLite
Database Integration plugin, which parses MySQL and translates it to SQLite
itself. Never convert a dump to SQLite's own dialect (PRAGMA,
AUTOINCREMENT, WITHOUT ROWID, BEGIN TRANSACTION, INSERT OR ..., replace(...,char(13)),
a sqlite3 .dump), and never write a custom converter: it fails with
Could not execute statement: PRAGMA .... The -sqlite.sql output name
means "ready for Studio", not "SQLite syntax".
- Never modify the original file. Write the cleaned copy beside it as
<name>-sqlite.sql.
- Change only statements SQLite or the Studio importer cannot run. Everything
else stays byte-for-byte identical.
- Treat the dump as bytes. It may contain non-UTF-8 data. On macOS, plain
sed
stops with "illegal byte sequence" and silently leaves a truncated file. Use
LC_ALL=C for sed, grep and diff, or Python in rb/wb mode, and
always compare line counts afterwards.
- Never print data rows, password hashes, email addresses or API keys from the
dump. Refer to tables, lines and counts.
- Ask before changing anything outside the known fixes below.
Procedure
1. Prepare the input and ask the user
- If the export is compressed (
.sql.gz,.zip), extract it to a new file
first (for example gunzip -k export.sql.gz). Never overwrite the archive.
- Note the input's size and line count.
- Confirm which Studio site the export is for (
~/Studio/<site>). The import
replaces that site's database, so the name matters.
- Read the export and the site together:
```bash
node agents/wp-db-migration-agent/lib/sql-tool.cjs site-urls <export.sql> --studio-site ~/Studio/<site>
node agents/wp-db-migration-agent/lib/sql-tool.cjs tables <export.sql>
```
site-urls shows the export's WordPress table prefix next to the site's
$table_prefix, siteurl and home, and the Studio site's own URL (its
custom domain from Studio's site list, such as https://<site>.local) with
a planned URL replacement and how many links it would change. tables lists every table with
its rows, size and group (WordPress core, accounts, Wordfence, plugin or
other).
Then ask these questions before running the sanitiser, one message with
the facts you found, and wait for the answers:
- Table prefix. Say what the export uses and what the Studio site uses.
If they differ (prefix-differs), ask: "Rename the tables to `<site
prefix> (recommended), keep <export prefix> and change $table_prefix`
in the site's wp-config.php, or another prefix?" If they already match,
say so and ask only if the user wants a different prefix. Never guess: an
import under the wrong prefix leaves WordPress unable to see the data.
- Local user accounts. Ask: "Keep the Studio site's own user accounts
(--keep-users), or replace them with the live site's accounts (the
default)?" Explain the trade-off:
- Keeping them means you log in as before. Posts and comments keep the
live site's author IDs, which may match a different local user or none.
- Replacing them brings the live site's accounts and author links, and you
log in with a live-site account.
--keep-users only works when the imported tables end up with the Studio
site's prefix, because local roles are stored under it
(<prefix>capabilities). With --studio-site, the tool refuses a
mismatch; add --prefix <site prefix>.
- Other tables to leave out. Tell the user that Wordfence's tables are
left out by default (name them and their size from tables). Then ask:
"Are there any other tables to leave out?" Suggest likely candidates from
the tables output without deciding for them: large log, statistics or
cache tables (for example bad_behavior), tables of plugins that are
gone or won't run locally, and backup-plugin tables. Never suggest WordPress
core tables. Pass each answer as --exclude <name>; names may omit the
prefix and use * and ? (for example --exclude 'gf_*'). Quote
patterns with wildcards so the shell leaves them alone.
- Site URL replacement. Show the export's URL, the Studio site's custom
URL and the link count from the URL replacement section, then ask:
"After import, replace <export URL> (http or https, with or without
www) with <Studio URL> throughout the database?" Also ask whether any
other hosts should be replaced too, such as a CDN, a media domain or an old
domain still in content. If site-urls showed no Studio URL (the site is
not in Studio's list), get it with
studio status --path ~/Studio/<site> --format json (siteUrl) or ask
the user. Record the confirmed pairs: the replacement runs in step 5,
after import, never on the dump.
2. Run the sanitiser
In a clone of lightspeedwp/.github (Node.js 20+), run:
node agents/wp-db-migration-agent/lib/sql-tool.cjs sanitise <path/to/export.sql>
Add the options that match the user's answers from step 1, and always pass
--studio-site ~/Studio/<site> so the report confirms the prefixes match.
To change the table prefix (the user's answer to question 1), add
--prefix <prefix>. It renames every table and the
keys WordPress looks up by prefix (<prefix>user_roles in options and
<prefix>* keys in usermeta, such as <prefix>capabilities). It refuses an
export with more than one WordPress prefix (multisite) or a rename that would
collide with an existing table. The site URL is replaced afterwards on the
imported site (step 5), never by editing the dump.
The tool applies the known fixes, verifies the result, and prints the edit
table, findings and site URLs. Exit code 0 means done, 1 means there are
findings to review (the output is still written), and 2 means an error (no
output is left behind).
If the tool is not available, apply the known fixes by hand (step 3), then
verify (step 4).
3. Known fixes (what the tool does)
- Delete the GTID replication lines. These only exist on a MySQL server
with replication on:
SET @@GLOBAL.GTID_PURGED=...;- the
-- GTID state at the beginning of the backupcomment
- Delete the binary-log lines:
SET @MYSQLDUMP_TEMP_LOG_BIN = @@SESSION.SQL_LOG_BIN;SET @@SESSION.SQL_LOG_BIN= 0;SET @@SESSION.SQL_LOG_BIN = @MYSQLDUMP_TEMP_LOG_BIN;
- Convert every
_binary '...'literal to a hex literalX'<hex>'.
Decode the MySQL escapes first: \0→0x00, \b→0x08, \n→0x0A,
\r→0x0D, \t→0x09, \Z→0x1A. A backslash followed by any other byte
means that byte, which covers \\, \' and \". Check that each decoded
value's length matches its column (for example binary(16) → 16 bytes) and
that primary-key values stay unique. If any length disagrees, stop and write
nothing: a wrong decode corrupts data silently.
- Rewrite MariaDB's broken binary defaults. MariaDB 11+ dumps a binary
column's default as `DEFAULT x AS <hex> `, which no parser accepts
(Studio, MySQL or MariaDB). Rewrite it as DEFAULT X'<hex>', checking the
byte count matches binary(N). Seen on Wordfence tables (wfblocks7,
wfcrawlers, wffilemods, wflocs and others).
- Add
DROP TABLE IF EXISTSbefore eachCREATE TABLEthat has none.
phpMyAdmin exports often leave it out. Every Studio site already has
WordPress's own tables, so without it the import stops at the first clash
(for example Could not execute statement: CREATE TABLE wp_commentmeta).
mysqldump adds these by default.
Tables left out by default
Wordfence (<prefix>wf* and <prefix>wfls_*, such as wfconfig, wfhits
and wfls_settings): its logs, scan results and blocks are not needed locally,
and we don't run Wordfence on Studio sites. The tool reports each table left
out. Wordfence's settings in the options table stay, so keep the plugin
deactivated locally (`studio wp --path ~/Studio/<site> plugin deactivate
wordfence). Pass --keep-wordfence` only if the user asks for its tables.
The tool refuses (exit 2, nothing written) a file that is already in SQLite's
own dialect. Go back to the original MySQL export and sanitise that.
Leave these alone; the importer handles them: the /*!40101 SET ... */ header
and footer lines, LOCK TABLES, DISABLE KEYS, ENGINE=InnoDB, character
set and collation clauses (including MariaDB-only ones such as
utf8mb4_general1400_as_ci), ALTER TABLE ... ADD PRIMARY KEY / `MODIFY ...
AUTO_INCREMENT blocks from phpMyAdmin, and ON UPDATE CURRENT_TIMESTAMP`.
4. Scan and verify
Report, before changing, anything else SQLite is likely to reject: other
SET @@GLOBAL statements, CREATE DATABASE / USE, DELIMITER,
DEFINER=, triggers, stored procedures or functions, views, events,
FULLTEXT / SPATIAL indexes and generated columns.
Then verify and report:
- a byte-safe diff (
LC_ALL=C diff) between the original and the cleaned file,
showing only the intended lines changed;
- the count of
CREATE TABLEandINSERT INTOstatements in both files (they
must match);
- zero remaining matches for
GTID,SQL_LOG_BIN,@@GLOBAL,_binaryand
DEFAULT x AS;
- the original file's checksum is unchanged (
shasum -a 256before and after).
5. Report
End with a table of every edit:
| # | Edit | Line | Why |
| --- | -------------------------------------- | ---- | ------------------------------------- |
| 1 | Removed SET @@GLOBAL.GTID_PURGED=... | 24 | Replication setting SQLite cannot run |
Then tell the user:
- Import: in the Studio app (site → Import / Export → Import), or
studio import <file> --path ~/Studio/<site>. Importing replaces the site's
database, so confirm the site first.
- Site URL: once the user confirms the import worked, run the