How to Fix MySQL Error 1686 (ER_STORED_FUNCTION_PREVENTS_SWITCH_BINLOG_DIRECT)
You get mysql error 1686 when a stored function or trigger tries to change the binlog direct flag. To fix it, move that flag change out of the stored routine and into the outer session or stored procedure. MySQL maps this failure to symbol ER_STORED_FUNCTION_PREVENTS_SWITCH_BINLOG_DIRECT and SQLSTATE HY000. The engine returns the exact message: Cannot change the binlog direct flag inside a stored function or trigger. Database teams hit this rule when routines try to alter how the server logs no
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 1686 when a stored function or trigger tries to change the binlog direct flag. To fix it, move that flag change out of the stored routine and into the outer session or stored procedure.
MySQL maps this failure to symbol ER_STORED_FUNCTION_PREVENTS_SWITCH_BINLOG_DIRECT and SQLSTATE HY000. The engine returns the exact message: Cannot change the binlog direct flag inside a stored function or trigger. Database teams hit this rule when routines try to alter how the server logs non-transactional updates.
Why mysql error 1686 happens in the database engine
MySQL uses the binary log to record data changes for point-in-time recovery and replication. When a query runs, the engine decides how to order transactional and non-transactional changes in that log. A session flag controls whether non-transactional updates go straight to the binary log.
Stored functions and triggers run inside the context of an active outer SQL statement. When the outer statement starts, MySQL locks in the binary logging rules for that execution step. If a function or trigger changes the binlog direct flag mid-statement, the log order can break.
A broken log order causes replicas to drift from the primary database. To stop data drift, MySQL blocks the change and throws code 1686 right away. This safety rule protects every downstream replica in your cluster from silent data loss.
How to find the routine causing the failure
Start by checking your app logs to find which query failed with SQLSTATE HY000. Note the exact table name and the operation type, such as insert, update, or delete. That table name points you straight to the triggers attached to it.
Next, read the routine definitions stored in your schema metadata tables. Look for any stored function or trigger that updates session variables related to binary logging. Keep in mind that a trigger might call a second stored function that hides the flag change.
MySQL Access Governance + Serverless Function Security: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Trace the full call chain from the top-level query down to the deepest helper function. Once you find the line that changes the binlog direct flag, mark that routine for removal or refactor. Do not grant extra admin roles to bypass this check, as the engine blocks it for all users.
How to fix the binlog flag change in production
The cleanest fix is to delete the flag change from the stored function or trigger. Let the database server use its normal session rules to handle binary log writes. In most modern setups using transactional storage engines, you do not need to flip this flag at all.
If your workload still needs to change the binlog direct flag, move the logic up one level. Put the session flag change inside a stored procedure or in your app code instead. Stored procedures run as separate statements, so MySQL lets them update session flags before a query starts.
Set the flag first, run your data change statement, and then reset the flag afterward. Test the updated routine in a staging setup with binary logging turned on. Check that replicas apply the resulting log events without errors before you deploy to production.
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_STORED_FUNCTION_PREVENTS_SWITCH_BINLOG_DIRECT mean?
It means a stored function or trigger tried to change the binlog direct flag while a query was running. MySQL blocks this action to keep the binary log consistent across primary and replica servers.
Can I change the binlog direct flag inside a stored procedure?
Yes, you can change the flag inside a stored procedure if it is not called by a trigger or function. Make sure you set the flag before the main data query runs, and reset it when the work finishes.
Does turning off binary logging stop mysql error 1686?
No, MySQL still checks session flag rules inside stored functions and triggers even if binary logging is off. You must remove the flag change from the function or trigger to clear the error.
Related guides
In this post