How to Fix MySQL Error 1671 (ER_BINLOG_UNSAFE_AUTOINC_COLUMNS)

Published
Length
3 min read

Luis Felipe Dias

All posts

You get mysql error 1671 when a query calls a trigger that writes to an auto-increment column. You can fix it by switching your binary log format to row mode or updating your trigger logic. MySQL uses the symbol ER_BINLOG_UNSAFE_AUTOINC_COLUMNS and SQLSTATE HY000 for this warning or failure. The engine flags the statement as unsafe because statement-based logging cannot keep replica keys in sync. When a stored function or trigger adds rows with generated numbers, the order can change on replica

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 1671 when a query calls a trigger that writes to an auto-increment column. You can fix it by switching your binary log format to row mode or updating your trigger logic.

MySQL uses the symbol ER_BINLOG_UNSAFE_AUTOINC_COLUMNS and SQLSTATE HY000 for this warning or failure. The engine flags the statement as unsafe because statement-based logging cannot keep replica keys in sync. When a stored function or trigger adds rows with generated numbers, the order can change on replicas. That difference causes data drift across your database cluster.

What causes mysql error 1671 in your database

MySQL relies on the binary log to send data changes from the primary node to replicas. In statement mode, the server writes the exact SQL query text into the log file. Replicas then read that text and run the exact same query on their own data copies.

Problems start when a single statement fires a trigger or a stored function. If that hidden routine inserts into a table with an auto-increment column, key order gets tricky. The primary server might assign one set of numbers based on its current memory state. A replica might run the trigger with slightly different timing or lock order. Now the two servers have different primary keys for the exact same data row.

To prevent silent corruption, the engine blocks or warns on the query with code 1671. The exact message states that the statement is unsafe because it invokes a trigger or function. Specifically, it flags routines that insert into an auto-increment column during statement logging.

How to diagnose the affected triggers and functions

First, check your database error logs and warning output to find the blocked query. Look at the tables touched by that main query. Next, inspect the schema to list all triggers attached to those specific tables. You also need to check any stored functions called inside the query text.

Read the body of each trigger and function to find insert statements. Check the target tables of those inserts for auto-increment columns. When you find a match, you have found the exact source of the binary log warning.

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

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Do not give developers broad superuser roles on production just to inspect these routines. Instead, use read-only metadata views to check trigger logic safely. This keeps your production data secure while your team finds the root cause.

How to fix the binary log error safely

You have two clean ways to fix this issue without turning off safety checks. The best fix for most teams is changing the binary log format to row mode. In row mode, the primary server logs the actual changed row values, not the query text. The log records the exact auto-increment number generated on the primary node. Replicas simply write that exact number, so keys never drift apart.

You can also use mixed mode if you need statement logging for most queries. Mixed mode switches to row logging on its own when it spots an unsafe trigger.

If you cannot change the log format, you must rewrite your app and database logic. Move the secondary insert out of the trigger or stored function. Have your app run the two inserts as separate steps inside one transaction. When the app runs direct inserts, statement logging records the generated key safely every time.

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

Does mysql error 1671 stop my query from running?

In standard setups, it logs a warning and lets the query finish. Still, if you use strict binary log settings or specific replication modes, it blocks the write.

Why is statement-based logging unsafe for auto-increment triggers?

Triggers and functions can process rows in different orders on the primary and replica servers. This gives different rows different auto-increment IDs on each server, breaking data consistency.

Will switching to row-based logging fix code 1671 right away?

Yes, row-based logging records the exact values written to each column, including generated keys. Replicas copy the final values instead of running the trigger again, which removes the risk.

The query runs. The emails stay hidden.