How to Fix MySQL Error 1668 (ER_BINLOG_UNSAFE_LIMIT)

Published
Length
3 min read

Luis Felipe Dias

All posts

You see mysql error 1668 when a write query uses a limit clause without a safe sort order. You can fix it by adding a unique sort key or switching to row-based logging. This warning carries the symbol ER_BINLOG_UNSAFE_LIMIT and uses the general state code HY000. The database engine flags the query because it cannot predict which rows will change. When you replicate data across servers, unpredictable queries cause your replica tables to drift apart. You must fix the query pattern or adjust your

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 1668 when a write query uses a limit clause without a safe sort order. You can fix it by adding a unique sort key or switching to row-based logging.

This warning carries the symbol ER_BINLOG_UNSAFE_LIMIT and uses the general state code HY000. The database engine flags the query because it cannot predict which rows will change. When you replicate data across servers, unpredictable queries cause your replica tables to drift apart. You must fix the query pattern or adjust your logging mode to keep servers in sync.

Why mysql error 1668 happens in replication

MySQL uses a binary log to send data changes from a primary server to replicas. In statement-based logging, the primary server writes the exact query text into the log. The replica reads that log and runs the exact same query on its own tables.

Problems start when a query deletes or updates rows with a limit clause alone. Relational tables do not store rows in a fixed order on disk. The primary server might pick the first ten rows it finds in memory. The replica might store those same rows in a different physical order on its disk.

When the replica runs the query, it updates or deletes a different set of rows. Even if you add a sort clause, the column might contain duplicate values. If the sort column is not unique, the engine still guesses which duplicate row comes first. To protect your data, MySQL throws code 1668 to warn you about this risk.

How to diagnose and fix unsafe limit queries

First, find the exact query that triggers the warning in your database error logs. Look for update, delete, or insert queries that cap the number of changed rows. Check if the query lacks a sort clause or sorts by a non-unique column.

The safest fix happens in your app code. Update the query to sort by the primary key before applying the row limit. Primary keys are always unique, so every server picks the exact same rows in order. This makes the query safe and deterministic across all replicas.

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 cannot change the app query, you can update your binary logging format. Ask your database team to switch the logging mode from statement-based to row-based or mixed. In row-based mode, the primary server logs the actual changed rows instead of the query text. Replicas then apply the exact row changes without guessing which records match the limit.

Preventing unsafe queries in production

Do not silence warnings or grant broad admin roles just to push a quick fix. Running ad-hoc fixes directly on production databases creates new risks for your team. Instead, review write queries during testing before they reach your live servers.

You should also track who runs manual data fixes during an outage. If an engineer runs a cleanup script with a row limit, check the sort order first. Good access controls let you review risky commands and mask sensitive fields like emails or card numbers. Masking rewrites matched fields in results while keeping your routine database work safe.

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 1668 stop my query from running?

By default, code 1668 acts as a warning while the query still runs. Still, if strict SQL modes are active, the engine can block the query from finishing.

Why does adding an order by clause still trigger the warning?

If you sort by a column that allows duplicate values, the order remains uncertain. You must sort by a primary key or a unique index to stop the warning.

Does mixed binary logging fix this issue?

Yes, mixed logging solves this problem. The engine uses statement logging by default, but switches to row logging when it spots an unsafe limit.

The query runs. The emails stay hidden.