Common SQL Errors

MySQL Error 1663: ER_BINLOG_UNSAFE_AND_STMT_ENGINE - How to Fix and Prevent

Galaxy Team
August 7, 2025

<p>The error appears when an unsafe statement targets a statement-only storage engine while the server runs in MIXED binary log mode.</p>

Sign up for the latest in common SQL errors from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

What is MySQL error 1663 (ER_BINLOG_UNSAFE_AND_STMT_ENGINE)?

<p>MySQL Error 1663: ER_BINLOG_UNSAFE_AND_STMT_ENGINE occurs when an unsafe statement tries to write to a statement-only storage engine while BINLOG_FORMAT is MIXED. Switch to ROW logging or move the table to InnoDB to resolve the issue.</p>

Error Highlights

Typical Error Message

Cannot execute statement: impossible to write to binary

Error Type

Replication/Binary Logging Error

Language

MySQL

Symbol

ER_BINLOG_UNSAFE_AND_STMT_ENGINE

Error Code

1663

SQL State

HY000

Explanation

Table of Contents

What is MySQL error 1663 (ER_BINLOG_UNSAFE_AND_STMT_ENGINE)?

MySQL error 1663 appears with message 'Cannot execute statement: impossible to write to binary log since statement is unsafe, storage engine is limited to statement-based logging, and BINLOG_FORMAT = MIXED'. The server blocks the statement to protect replication integrity.

The conflict happens when an unsafe, non-deterministic statement targets a storage engine that only supports statement-based logging while the server is running in MIXED mode. Because the engine forbids row-logging, MySQL aborts the request instead of corrupting replicas.

What Causes This Error?

Using MEMORY, BLACKHOLE, or custom statement-only engines in combination with unsafe statements triggers the condition.

Queries containing non-deterministic functions, LIMIT without ORDER BY, subqueries altering selected rows, or updates to tables used in triggers are flagged as unsafe for statement-based replication.

BINLOG_FORMAT=MIXED directs MySQL to switch automatically to row format for unsafe statements, but a statement-only engine blocks the switch and forces the error.

How to Fix MySQL Error 1663

Set binlog_format to ROW at the session or global level so that the statement can be logged row by row even when the engine allows it.

If changing the format is impossible, move the affected table to an engine that supports row-based logging such as InnoDB or MyISAM.

Alternatively, rewrite the statement to become deterministic and safe for statement logging, removing non-deterministic functions or adding explicit ORDER BY.

Common Scenarios and Solutions

INSERT INTO memory_tbl SELECT * FROM source_tbl WHERE updated_at > NOW() fails because NOW() is non-deterministic and MEMORY is statement-only. Converting memory_tbl to InnoDB resolves it.

UPDATE blackhole_tbl SET val = val + 1 ORDER BY RAND() LIMIT 10 fails for the same reason; switching binlog_format to ROW before executing works.

Best Practices to Avoid This Error

Standardize on ROW binlog format in replicated environments to eliminate most unsafe statement problems.

Avoid using statement-only engines for tables that need writes in replication setups.

Review queries for non-deterministic functions and add deterministic clauses or rewrite them.

Related Errors and Solutions

Error 1592 'Statement violates row-based logging' appears when the server is in STATEMENT mode; switch to MIXED or ROW.

Error 1677 'Statement violates MIXED mode' indicates unsafe statement but engine supports row; enable ROW or rewrite query.

Error 1785 'Statement unsafe because it uses system variables' is resolved by removing the variable or using row logging.

Common Causes

Non-deterministic functions in UPDATE or INSERT

Using NOW(), RAND(), UUID(), or subqueries makes the statement unsafe for statement-based replication.

Statement-only storage engine

Engines such as MEMORY, BLACKHOLE, or custom plugins do not support row-based logging, forcing MySQL to stay in STATEMENT mode.

BINLOG_FORMAT set to MIXED

MIXED mode tries to switch formats automatically; the combination with a statement-only engine causes conflict.

Triggers or stored functions touching other tables

These constructs are marked unsafe because they may affect rows unpredictably when replayed on replicas.

Related Errors

MySQL Error 1592: ER_BINLOG_FORMAT_MIXED_USED

Raised when a statement requires row logging but server is in STATEMENT mode.

MySQL Error 1677: ER_BINLOG_UNSAFE_AUTOINC_COLUMNS

Occurs when unsafe AUTOINCREMENT updates are detected in MIXED format.

MySQL Error 1785: ER_BINLOG_UNSAFE_SYSTEM_VARIABLE

Thrown when an unsafe system variable is used inside the statement during mixed logging.

FAQs

Can I disable binary logging to bypass the error?

Disabling binary logging avoids the check but breaks replication and point-in-time recovery. Use it only on standalone, non-critical servers.

Does changing MEMORY tables to InnoDB impact performance?

InnoDB writes to disk, so read/write latency increases, but you gain durability and replication safety. For pure in-memory speed, consider cache layers.

Is ROW logging slower than MIXED?

Row logging writes more data to the binary log but reduces replica replay time and avoids full-table scans on replicas, often balancing the overhead.

How does Galaxy help?

Galaxy highlights unsafe statements in its editor and recommends changing binlog_format or engine before execution, reducing replication incidents.

Start Querying with the Modern SQL Editor Today!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Check out some other errors

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo