How to Fix MySQL Error 1663 (ER_BINLOG_UNSAFE_AND_STMT_ENGINE)

Published
Length
3 min read

Luis Felipe Dias

All posts

You see mysql error 1663 when an unsafe query runs on a statement-only table. You fix it by rewriting the query or changing the table engine. The official symbol for this failure is ER_BINLOG_UNSAFE_AND_STMT_ENGINE with SQLSTATE HY000. The database blocks the query to keep your primary and replica servers in sync. When a query cannot log in row format, the engine halts the write. What causes mysql error 1663 in the database engine MySQL uses the binary log to send data changes to replica ser

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 1663 when an unsafe query runs on a statement-only table. You fix it by rewriting the query or changing the table engine.

The official symbol for this failure is ER_BINLOG_UNSAFE_AND_STMT_ENGINE with SQLSTATE HY000. The database blocks the query to keep your primary and replica servers in sync. When a query cannot log in row format, the engine halts the write.

What causes mysql error 1663 in the database engine

MySQL uses the binary log to send data changes to replica servers. It logs changes as whole SQL statements, changed rows, or a mix of both. Some queries produce different results on the replica than on the primary server. The engine marks these non-deterministic queries as unsafe for statement logging.

When the binary log uses mixed mode, the engine switches to row mode for unsafe queries. Still, some specialized storage engines only support statement logging. They cannot write row events to the binary log at all. When an unsafe query hits a statement-only table engine, MySQL returns code 1663.

Common triggers include queries that use system variables, random numbers, or user-defined functions. Queries that combine row limits with updates also trigger this safety check. Because the table engine refuses row logging, the database stops the query right away. This stop prevents silent data drift across your database cluster.

How to find the failing query and table engine

Start by checking your database error logs and app logs for the exact query text. Look at every table that the failing transaction reads or updates. You need to find which table uses a storage engine with logging limits.

Check the table definitions in your schema metadata. Most modern apps use standard transactional engines that support row logging. Legacy tables or special cluster engines might still force statement logging. Also check if your session or global log format forces statement mode.

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

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Review the query logic to see why the engine calls it unsafe. Look for functions that depend on local server state, system time, or row order. Even reading from a system variable inside an update can trigger this error. Finding both the unsafe function and the restricted table gives you a clear fix path.

How to fix the error in production

The safest fix is to rewrite the SQL query so it becomes deterministic. Remove random functions, local variables, and unsorted limit clauses from your write statements. If you need a dynamic value, calculate it in your app code first. Then pass that fixed value into the database as a plain parameter.

If you cannot change the app query, look at the table storage engine. Moving the table to a standard engine that supports row logging solves the issue. Test this engine change in a staging setup before you run it in production. Make sure the new engine fits your speed and storage needs.

Never turn off binary logging in production just to bypass this error. Disabling the binary log breaks replication and ruins your point-in-time recovery plan. Also avoid granting super privileges to app roles to force local session changes. Keep your database roles tight and fix the root query or table design instead.

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

What does ER_BINLOG_UNSAFE_AND_STMT_ENGINE mean in plain words?

It means your query could create different data on replicas, and your table engine cannot log row changes. MySQL blocks the write so your servers do not fall out of sync.

Can I fix mysql error 1663 by changing the binary log format?

Changing the log format to row or mixed mode only works if the table engine supports row logging. If the storage engine only allows statement logging, you must rewrite the query or change the engine.

Is it safe to ignore binary log safety warnings in MySQL?

No, ignoring unsafe statement warnings can cause silent data corruption on your replica servers. Plus, error 1663 is a hard stop, so the database will not run the query until you fix it.

The query runs. The emails stay hidden.