How to Fix MySQL Error 1722 (ER_BINLOG_UNSAFE_WRITE_AUTOINC_SELECT)
You trigger mysql error 1722 when a query writes to an auto-increment table after reading another table. You fix this issue by switching to row-based binary logging or ordering your query results. This failure uses the symbol ER_BINLOG_UNSAFE_WRITE_AUTOINC_SELECT and returns SQLSTATE HY000. The database engine flags this action to stop data drift across your primary and replica servers. When you use statement-based logging, the replica runs the exact same query text as the primary. If the read
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 trigger mysql error 1722 when a query writes to an auto-increment table after reading another table. You fix this issue by switching to row-based binary logging or ordering your query results.
This failure uses the symbol ER_BINLOG_UNSAFE_WRITE_AUTOINC_SELECT and returns SQLSTATE HY000. The database engine flags this action to stop data drift across your primary and replica servers. When you use statement-based logging, the replica runs the exact same query text as the primary. If the read order changes on the replica, the auto-increment numbers will not match the primary.
Root causes of mysql error 1722 in the database engine
The MySQL engine tracks every data change in the binary log for replication and point-in-time recovery. In statement-based mode, the engine writes the raw query string into the log file. This approach works well for simple updates that target specific primary keys. It fails when a single transaction reads from one table and inserts into another auto-increment table.
Storage engines do not return rows in a fixed order unless you add a sort clause. A primary server might read rows from memory or a fast index cache during peak load. The replica server might read those same rows from disk in a different order. As the insert runs, the database assigns new auto-increment values to each incoming row in turn.
If the row order shifts, the primary and replica assign different keys to the exact same records. Your child tables and foreign keys then point to the wrong records on the replica node. To prevent silent data corruption, the engine blocks or warns on this unsafe statement pattern.
How to diagnose and fix the failure in production
First, check your active binary log mode and your global transaction identifier rules on the server. When you turn on strict GTID consistency, the database turns replication warnings into hard errors. You can check the database error logs to find the exact app query that failed.
The safest fix at the database layer is to switch your binary log mode to row-based. In row-based mode, the primary logs the actual data changes and the exact auto-increment numbers assigned. The replica applies those exact row values instead of running the select query again. You can also use mixed mode, which switches to row logging only when an unsafe query runs.
MySQL Access Governance + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
If you must keep statement-based logging, you need to change how your app writes the data. Add an explicit sort clause based on a unique key to every select query that feeds an insert. Better yet, split the work into two separate steps in your app code. Read the source rows first, sort them in memory, and then run explicit insert statements.
Preventing unsafe binary log writes across your team
Ad-hoc queries run by engineers during live incidents often trigger replication errors like this one. An engineer might copy records into a backup table or backfill missing rows using a single query. Without a review step, that quick fix can halt replication or reject valid production writes.
You should never turn off binary log safety checks or grant broad admin roles to bypass errors. Instead, enforce safe query patterns before statements reach the primary database engine. Set up staging databases with strict GTID rules so developers catch unsafe queries during testing. Combine this setup with clear query review policies and PII masking for sensitive fields like emails or phone numbers.
Inspect every MySQL query and response in flight
Static grants and network rules block bad logins. They cannot pause a risky query for review or mask emails in a result set. By design, hoop.dev sits inline in the MySQL protocol as a sidecar for your MySQL. For every plain query, prepared statement, or cursor an agent or human runs in production, hoop.dev decides to pass, mask, block, or hold it for review.
First, hoop.dev guardrails block banned actions before MySQL runs them, and an AI Analyzer you configure can score intent or escalate for approval. Next, hoop.dev data masking rewrites sensitive string columns in results in memory, and every verdict goes to the audit trail. Find the open-source binary on GitHub.
Frequently asked questions
Why did mysql error 1722 start appearing after a server upgrade?
Newer database versions and strict GTID modes enforce binary log safety rules by default. What used to log a quiet warning in older setups now blocks the query to protect replicas.
Does adding an order by clause always fix this error?
Adding a sort clause on a unique key makes the row order predictable across servers. Still, the database parser may still flag complex compound statements unless you use row-based logging.
Is it safe to change the binary log format while the database is running?
Yes, you can change the log mode at the session or global level without a full restart. Active sessions keep their old mode until they close, while new sessions pick up the row setting.
Related guides
In this post