How to Fix MySQL Error 1673 (ER_BINLOG_UNSAFE_SYSTEM_VARIABLE)

Published
Length
3 min read

Luis Felipe Dias

All posts

You get mysql error 1673 when a write query reads a local system variable. You fix it by passing explicit values or switching to row-based binary logging. MySQL maps this issue to ER_BINLOG_UNSAFE_SYSTEM_VARIABLE under SQLSTATE HY000. The engine states that the statement is unsafe because it uses a system variable. That variable may have a different value on the replica server. When replicas hold different variable values, statement-based replication produces mismatched data across nodes. Wha

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 1673 when a write query reads a local system variable. You fix it by passing explicit values or switching to row-based binary logging.

MySQL maps this issue to ER_BINLOG_UNSAFE_SYSTEM_VARIABLE under SQLSTATE HY000. The engine states that the statement is unsafe because it uses a system variable. That variable may have a different value on the replica server. When replicas hold different variable values, statement-based replication produces mismatched data across nodes.

What causes mysql error 1673 in database clusters

Database clusters use binary logs to copy changes from primary servers to replicas. In statement-based logging mode, the primary server sends the exact query text. The replica reads this log and runs the same query on its own data.

Problems start when a write query depends on a server system variable. A developer might insert the server host name or character set into a table. The primary server reads its own local setting and writes that value. The replica then runs the same query using its own local setting instead.

Because the two servers have different settings, they write different rows. Now the replica data does not match the primary data. To prevent silent data drift, the engine flags ER_BINLOG_UNSAFE_SYSTEM_VARIABLE with SQLSTATE HY000. Depending on your strictness rules, the engine logs a warning or blocks the write.

How to diagnose and fix the failure in production

Start by checking your database error logs and slow query logs. Find the exact write statements that trigger the 1673 code. Look for queries that insert or update rows using system variables. Common culprits include queries that record server IDs, paths, or local time zones.

The safest fix is to change how the app builds the query. Read the needed value in the app layer before you run the write. Then pass that value as a plain literal string or number in the query. Because the query now holds a fixed value, every replica writes the exact same data.

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 code right away, check your binary log format. Statement-based logging copies the query text, which causes the variable mismatch. Row-based logging records the actual changed table rows instead of the query text. Mixed logging uses statement mode by default but switches to row mode for unsafe queries.

Switching to row-based or mixed logging resolves the replication hazard at the engine level. The primary server resolves the system variable once and logs the resulting row data. Replicas copy the final row values without reading their own local system variables. Never disable binary logging or ignore replication warnings to bypass this error.

How to prevent unsafe queries during database maintenance

Ad-hoc maintenance scripts often cause this warning during manual production updates. Engineers log in and run quick write queries that reference local session or system variables. Standardizing how your team runs production queries stops these issues before they hit the log.

Review all migration scripts in a staging setup that mirrors your production replication mode. Keep statement-based logging active in test setups if you want to catch unsafe queries early. Require peer approval for manual write sessions on primary database nodes. Clear query habits keep primary and replica data in sync across your whole fleet.

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_SYSTEM_VARIABLE mean in MySQL?

It means a write query uses a system variable that can differ on a replica. In statement-based replication, this causes the primary and replica to write different data.

Does mysql error 1673 stop my query from running?

Often it acts as a warning in the log, and the query still runs. Still, strict session modes or error handlers can treat the HY000 state as a hard failure.

Will row-based binary logging stop this error?

Yes, row-based logging records the final row changes rather than the raw query text. Replicas copy the exact row values without reading their own local system variables.

The query runs. The emails stay hidden.