Repository navigation
Description
Bug summary
When the MySQL server has gtid_mode=ON, mysqldump writes two statements near the top of the dump that wp db export creates:
SET @@SESSION.SQL_LOG_BIN= 0;
SET @@GLOBAL.GTID_PURGED=/*!80000 '+'*/ '52b461b2-e9b7-11f1-83ac-f2cb234d1a9a:1-6014';wp db export does not pass --set-gtid-purged (DB_Command.php L837-L847), so wp db import of that file stops at one of them in two common cases:
- Importing back into the same server, for example to restore a backup or to copy a site into another database there:
ERROR 3546 (HY000) at line 24: @@GLOBAL.GTID_PURGED cannot be changed: the added gtid set must not overlap with @@GLOBAL.GTID_EXECUTED - Importing as a database user without the
SUPER,SYSTEM_VARIABLES_ADMINorSESSION_VARIABLES_ADMINprivilege, as is common on managed hosting, into any server:ERROR 1227 (42000) at line 18: Access denied; you need (at least one of) the SUPER, SYSTEM_VARIABLES_ADMIN or SESSION_VARIABLES_ADMIN privilege(s) for this operation
The import exits with 1 before any table is created, so nothing from the dump is restored. MySQL 9.7.4 and 26.10.0 have gtid_mode=ON as their compiled default, so a stock server hits this; 8.4.13 has it off. mysqldump itself warns during the export: "If you don't want to restore GTIDs, pass --set-gtid-purged=OFF."
Steps to reproduce
On a MySQL 9.7 server with default settings (the mysql:9.7 Docker image), with WordPress installed:
wp db export backup.sql
wp option update blogname Changed
wp db import backup.sql; echo "exit: $?"
wp option get blognameWarning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions, even those that changed suppressed parts of the database. If you don't want to restore GTIDs, pass --set-gtid-purged=OFF. To make a complete dump, pass --all-databases --triggers --routines --events.
Warning: A dump from a server that has GTIDs enabled will by default include the GTIDs of all transactions, even those that were executed during its extraction and might not be represented in the dumped data. This might result in an inconsistent data dump.
In order to ensure a consistent backup of the database, pass --single-transaction or --lock-all-tables or --source-data.
Success: Exported to 'backup.sql'.
Success: Updated 'blogname' option.
ERROR 3546 (HY000) at line 24: @@GLOBAL.GTID_PURGED cannot be changed: the added gtid set must not overlap with @@GLOBAL.GTID_EXECUTED
exit: 1
Changed
Results with the nightly, for a dump made by wp db export:
| Export from | Import into | Import user | Result |
|---|---|---|---|
| MySQL 9.7.4 or 26.10.0 (GTID on) | the same server | root | ERROR 3546 |
| MySQL 9.7.4 or 26.10.0 (GTID on) | the same server | privileges on its database only | ERROR 1227 |
| MySQL 26.10.0 (GTID on) | MySQL 8.4.13 (GTID off) or 9.7.4 (GTID on) | root | imported |
| MySQL 26.10.0 (GTID on) | MySQL 8.4.13 (GTID off) or 9.7.4 (GTID on) | privileges on its database only | ERROR 1227 |
| MySQL 8.4.13 (GTID off) | the same server | root | imported |
With wp db export backup.sql --set-gtid-purged=OFF, the dump has neither statement, and the same-server import works for root and for the limited user (tested on 26.10.0).
Expected
A dump made by wp db export can be restored with wp db import on the server it came from, and by a database user without global privileges.
Environment
- WP-CLI nightly 3.0.0-alpha-45ebe6e (db-command v3.0.3), PHP 8.4, macOS, mysql and mysqldump clients 26.10.0
- Servers: MySQL 26.10.0 (Homebrew), 9.7.4 and 8.4.13 (official Docker images, default settings)
- WP-CLI 2.12.0's export writes the same two statements. Its import could not be compared here, because with this client it fails earlier on
SOURCE(changed in db import: Pass import file via STDIN redirection #341).
Possible solution
This needs a decision on the default, so here are the options I see:
wp db exportadds--set-gtid-purged=OFFby default when the dump client supports it (MySQL's mysqldump does, mariadb-dump 12.3.3 does not), checked the way--column-statisticsalready is (L815), with a way to keep GTIDs for anyone who needs them.- Leave the default and document
--set-gtid-purged=OFFin thewp db exporthelp and examples.
For reference, the wpdb fallback added in #320 already skips privilege errors on SET statements such as these (L3056-L3059), so the limited-user case would differ between the two import paths. I have read that code but not tested it.
I can open a PR for whichever option you prefer.
Activity