How to Fix MySQL Error 1723 (ER_BINLOG_UNSAFE_CREATE_SELECT_AUTOINC)

Published
Length
3 min read

Luis Felipe Dias

All posts

You trigger mysql error 1723 when you create and populate an auto-increment table in one step. You fix it by splitting table creation and data insertion into two separate steps, or by using row-based logging. This failure protects your database replicas from drifting out of sync. When a single query builds a table and inserts rows, the database cannot predict the exact row order. Replicas may assign different ID numbers to the same records. This breaks data integrity across your cluster. Why

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 1723 when you create and populate an auto-increment table in one step. You fix it by splitting table creation and data insertion into two separate steps, or by using row-based logging.

This failure protects your database replicas from drifting out of sync. When a single query builds a table and inserts rows, the database cannot predict the exact row order. Replicas may assign different ID numbers to the same records. This breaks data integrity across your cluster.

Why mysql error 1723 happens in database replication

The database engine links this issue to symbol ER_BINLOG_UNSAFE_CREATE_SELECT_AUTOINC and SQLSTATE HY000. The engine blocks or warns you because the row retrieval order is not fixed. When you run a combined create and select query, the storage engine reads rows as it finds them.

If your cluster uses statement-based binary logging, the primary server sends the raw query text to replicas. Each replica then runs that same query on its own local storage. Because disk layouts and memory caches differ between servers, a replica might read the source rows in another order. The auto-increment column then assigns different numbers to those rows on the replica.

Strict binary log safety rules will stop the query right away. Even if the server only logs a warning, your primary and replica tables will hold mismatched primary keys. Downstream apps that read from replicas will then return wrong records to users.

How to diagnose and fix the failure in production

First, check your database logs and slow query records to find which app or script sent the combined query. Look for migration jobs, reporting scripts, or analytics tasks that build temporary summary tables. Do not turn off binary logging or relax safety checks to bypass the error. Disabling safety rules puts your entire replication setup at risk.

The safest fix happens at the query level. Change the workflow so it runs two separate statements instead of one. First, run a plain statement that creates the empty table with its auto-increment column and keys. Second, run an insert statement that selects the source data and includes an explicit order clause. Sorting the source rows makes the insert order predictable on every server.

MySQL Access Governance + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

You can also check how your database records changes in the binary log. Statement-based logging copies the query text, which causes this ordering problem. Row-based logging writes the exact changed data rows into the log after the primary server builds them. When the primary server logs actual row values, replicas copy the exact ID numbers instead of guessing the order.

How to prevent unsafe table creation in your workflow

Database errors often reach production when teams run ad-hoc scripts without review. A developer might test a quick table build on a single local database where binary logging is off. When that same script runs on a replicated production cluster, it fails at once.

Set up your staging database with the exact same binary log rules as production. This catches unsafe queries during testing before they touch live data. You should also review database access so engineers do not run unverified table creation scripts by hand.

When teams use structured approval checks for schema changes, reviewers can spot combined create and select queries early. Pair these checks with data masking for sensitive columns like emails, phone numbers, card numbers, and national IDs. Masking rewrites matched fields in query results while keeping schema checks strict and safe.

Check MySQL queries and results on the wire

MySQL grants control who logs in. They do not check single queries or mask sensitive columns once a session starts. With hoop.dev, you run an open-source proxy, a sidecar for your MySQL, inline in the MySQL wire protocol. On every query an AI agent or human runs against production MySQL, RDS, or Aurora, hoop.dev decides to pass, mask, block, or hold it for review.

Multi-statement batches and prepared queries are split and checked one by one by hoop.dev guardrails, which refuse banned queries with a native MySQL error or hold risky ones for approval. On the response path, hoop.dev data masking rewrites string values in returned rows in memory, and every decision is logged. See the MIT-licensed project on GitHub.

Frequently asked questions

What does ER_BINLOG_UNSAFE_CREATE_SELECT_AUTOINC mean in plain terms?

It means you tried to build a table with an auto-increment column and fill it from another table at once. The database warns you because replicas might number the new rows in a different order.

Can I fix mysql error 1723 by adding an order clause to the original query?

No, adding a sort clause to a combined create and select statement does not clear the safety flag. You must split the operation into a separate table creation step and an ordered insert step.

Does row-based binary logging stop replica drift for this error?

Yes, row-based logging records the exact row values generated on the primary server. Replicas apply those exact rows and ID numbers rather than running the unsafe query text again.

The query runs. The emails stay hidden.