How to Fix MySQL Error 1841 (ER_CANT_SET_GTID_PURGED_WHEN_OWNED_GTIDS_IS_NOT_EMPTY)
You get mysql error 1841 when you try to update the global GTID purged value while transactions are still running. To fix it, you must stop all active client transactions and wait for owned GTIDs to clear before you run the change. This issue often appears during data restores or replica setups. The database blocks the change to protect your replication state from data loss or split history. Once the server has zero active transactions across all sessions, the update will succeed. What causes
Your coding agents run with your access.
Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.
You get mysql error 1841 when you try to update the global GTID purged value while transactions are still running. To fix it, you must stop all active client transactions and wait for owned GTIDs to clear before you run the change.
This issue often appears during data restores or replica setups. The database blocks the change to protect your replication state from data loss or split history. Once the server has zero active transactions across all sessions, the update will succeed.
What causes mysql error 1841 in your database
MySQL uses Global Transaction Identifiers, or GTIDs, to track every change across a cluster. The engine records which transactions it has applied and which ones it has purged from old binary logs. When you restore a backup, you must update the global GTID purged variable to match the dump file.
According to the official MySQL spec, this error maps to symbol ER_CANT_SET_GTID_PURGED_WHEN_OWNED_GTIDS_IS_NOT_EMPTY and SQLSTATE HY000. The engine states that the global GTID purged value can only be set when there are no ongoing transactions, not even in other clients. If any session holds an open transaction with an assigned GTID, the server rejects your change.
Even a single background worker or monitoring app can trigger this lock. When a client starts a transaction or acquires a GTID, the database adds that identifier to the global owned GTIDs set. As long as that set is not empty, the database refuses to modify the purged history.
How to diagnose and fix the failure in production
First, you need to check which sessions are running transactions on the target database. Inspect the active process list and current transaction tables to find open work. Look for app servers, cron jobs, or admin tools that stay connected to the instance.
Next, check the global status for owned GTIDs on the server. If this status returns any values, a session is holding an active transaction right now. Do not grant broad superuser roles to app accounts or turn off GTID checks to bypass this rule.
MySQL Access Governance + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Instead, pause the apps or background jobs that write to this database node. Commit or roll back any open transactions in your own client session as well. If a stale session is stuck in an open state, close that specific client session safely.
Verify that the owned GTIDs list is completely empty across the whole server. Then run your GTID purged update in a clean session before you turn app traffic back on. The command will finish right away, and your replica will keep a clean transaction log.
How to prevent transaction conflicts during restores
Most teams hit this error when restoring a backup onto a live database server. If apps can still reach the host during a restore, they will open new transactions and block the GTID update. You should isolate the target node from app traffic before starting a restore.
Route your app traffic away from the maintenance node at the load balancer or proxy layer. Keep admin sessions limited so only one operator runs the restore script at a time. When you control who can write to the database during maintenance, you avoid race conditions and half-finished restores.
Add wire guardrails and masking beside MySQL
Database grants and TLS setup decide who can reach MySQL. Even so, an open session can run broad updates or read raw personal data from allowed tables. In practice, hoop.dev sits inline in the wire protocol beside the database as a sidecar for your MySQL. On every production query an agent or engineer sends, hoop.dev decides to pass, mask, block, or escalate it for human review.
With hoop.dev MySQL support, guardrails refuse bad queries before they run or hold risky calls for approval, while data masking rewrites matched sensitive values in result rows in memory. Furthermore, hoop.dev holds SOC 2 Type II and generates evidence for GDPR, HIPAA, and PCI DSS programs. Read the docs and source on GitHub.
Frequently asked questions
What does ER_CANT_SET_GTID_PURGED_WHEN_OWNED_GTIDS_IS_NOT_EMPTY mean?
It means you tried to change the global GTID purged list while at least one session held an active transaction. MySQL requires zero ongoing transactions across all connected clients before it allows this update.
Can read-only queries trigger mysql error 1841?
Simple read queries usually do not assign an owned GTID, so they rarely block the update on their own. Still, an open transaction block that mixes reads and writes or holds a GTID will trigger the error.
Why do I see this error when importing a backup file?
Backup tools often add a step to set the global GTID purged value so replicas know which transactions are in the dump. If another client is writing to the target database during the import, that step fails with error 1841.
Related guides
In this post