How to Fix MySQL Error 3022 (ER_WARN_OPEN_TEMP_TABLES_MUST_BE_ZERO)
You see mysql error 3022 when you run a replica command while temporary tables keeps the session active. You fix it by waiting for the replica to close all temporary tables first. This database warning uses the symbol ER_WARN_OPEN_TEMP_TABLES_MUST_BE_ZERO and the HY000 state. The engine tells you that your operation is not safe with open temporary tables. The database keeps those tables open until the server restarts or the session ends. Teams hit this issue during replica maintenance, channel
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 see mysql error 3022 when you run a replica command while temporary tables keeps the session active. You fix it by waiting for the replica to close all temporary tables first.
This database warning uses the symbol ER_WARN_OPEN_TEMP_TABLES_MUST_BE_ZERO and the HY000 state. The engine tells you that your operation is not safe with open temporary tables. The database keeps those tables open until the server restarts or the session ends. Teams hit this issue during replica maintenance, channel changes, or topology updates. Understanding why the replica holds these tables helps you avoid broken data.
What causes mysql error 3022 in replication
Replicas create temporary tables when they replay events from the source database. Statement-based and mixed logging formats often send temporary table commands to replicas. A source session creates a temporary table, uses it, and drops it later. The replica SQL thread opens that table and keeps it in memory. It keeps the session active until the replica reads the matching drop event from the log.
If an admin stops the replica or changes settings mid-stream, the table keeps the session active. The database raises code 3022 to warn you about this state. Certain admin actions require the open temporary table count to sit at zero. If you ignore the warning and restart the replica, the temporary table vanishes. When the replica resumes, later events that need that table will fail right away.
How to diagnose and fix open temporary tables
First, check the replica status metrics to see the count of open temporary tables. Do not grant full root rights to every engineer just to check this metric. Give your team a read-only role that can read server status variables safely. If the count is above zero, the replica is still midway through a workflow.
Let the replica SQL thread run until it processes the drop table events. You can pause new writes on the source app to speed this up. Watch the status metric until the open temporary table count drops to zero. Once the count hits zero, stop the replica thread and run your change.
Zero Trust Architecture + Open Policy Agent (OPA): architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
For a permanent fix, switch your binary log format to row-based logging. Row-based logging writes the final row changes instead of temporary table commands. Replicas will not need to create or track temporary tables during replay. This change prevents code 3022 from blocking future maintenance tasks.
How to prevent unsafe replica operations
Production incidents often happen when engineers run replica commands without checking server state. Shared admin keys make it hard to see who ran a risky command. Your team should restrict direct admin rights on production database replicas.
Set up role-based rules so engineers only run approved maintenance checks. Require peer review before anyone stops a replica thread or edits replication channels. Data masking also helps when engineers inspect replication logs containing emails or card numbers. Masking rewrites matched fields in results so sensitive data stays safe during checks.
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_WARN_OPEN_TEMP_TABLES_MUST_BE_ZERO mean?
It means you ran an admin operation while a replica held open temporary tables. The server warns you that those tables will keeps the session active until a restart.
Can I restart the MySQL server when error 3022 appears?
No, restarting the server deletes the open temporary tables from replica memory. When replication resumes, queries needing those tables will fail and break the replica.
How do I stop replicas from opening temporary tables?
Change your database binary logging format from statement-based to row-based logging. Row-based events do not use temporary tables on the replica during replay.
Related guides
In this post