Common SQL Errors

MySQL Error 1695: ER_STORED_FUNCTION_PREVENTS_SWITCH_SQL_LOG_BIN - How to Fix and Prevent

Galaxy Team
August 7, 2025

<p>MySQL raises error 1695 when a stored function, procedure, or trigger tries to change the sql_log_bin setting, which is not allowed inside stored routines.</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 code 1695?

<p>MySQL Error 1695: ER_STORED_FUNCTION_PREVENTS_SWITCH_SQL_LOG_BIN occurs when code inside a stored function, procedure, or trigger executes SET sql_log_bin. MySQL forbids binlog toggling in routines to protect replication. Remove the SET statement from the routine and run it in client code or a privileged session to resolve the issue.</p>

Error Highlights

Typical Error Message

Cannot change the sql_log_bin inside a stored function or

Error Type

Operational Error

Language

MySQL

Symbol

ER_STORED_FUNCTION_PREVENTS_SWITCH_SQL_LOG_BIN

Error Code

1695

SQL State

HY000

Explanation

Table of Contents

What is MySQL error code 1695?

Error 1695, condition ER_STORED_FUNCTION_PREVENTS_SWITCH_SQL_LOG_BIN, appears with the message "Cannot change the sql_log_bin inside a stored function or trigger". It signals that MySQL blocked an attempt to enable or disable binary logging inside a stored routine.

The sql_log_bin session variable controls whether statements are written to the binary log. Writing to the binlog inside routines could break replication consistency, so MySQL disallows it.

When does ER_STORED_FUNCTION_PREVENTS_SWITCH_SQL_LOG_BIN occur?

The error fires during execution of a stored function, procedure, or trigger that contains SET sql_log_bin = {0|1}. It is raised immediately, even if the statement sits in unreachable code.

The same rule applies in event scheduler jobs defined with DO or CALL statements that reference such routines.

Why is it important to fix?

Ignoring the error stops the routine and any caller, interrupting application workflows. In replication topologies, attempting to toggle binlogging inside routines risks data drift between primary and replicas.

What causes this error?

Direct SET sql_log_bin inside routine code is the primary trigger. Indirect triggers include dynamic SQL that resolves to SET sql_log_bin or stored procedures that encapsulate such statements.

How to fix MySQL Error 1695

Move the SET sql_log_bin statement outside the routine. Execute it in client code or in a wrapper script that runs before and after the routine call.

Alternatively, redesign logic so the routine returns data instead of modifying replication state, and let application code handle binlog control.

Common scenarios and solutions

Backup scripts that disable binlogging in a procedure should switch to using mysql command line with --skip-log-bin. ETL jobs can wrap data changes between START TRANSACTION and COMMIT executed under SESSION sql_log_bin control outside stored code.

Best practices to avoid this error

Avoid replication-related variable changes inside routines. Keep routines idempotent and deterministic. Use definer privileges wisely and document any required session settings in calling code.

Related errors and solutions

Error 1419: ER_BINLOG_UNSAFE_ROUTINE warns about nondeterministic or binlog-unsafe routines. Review routine body and set log_bin_trust_function_creators when needed.

Common Causes

SET sql_log_bin inside function body

A developer inserts SET sql_log_bin = 0 to suppress logging for bulk inserts, triggering error 1695 on first execution.

Procedures controlling replication

Legacy procedures that toggle binlogging before maintenance tasks fail after an upgrade to MySQL 5.5+ where enforcement tightened.

Triggers generated by tools

Schema migration tools sometimes inject sql_log_bin toggles in generated triggers, causing immediate failures on data change.

Dynamic SQL paths

EXECUTE IMMEDIATE statements that evaluate to SET sql_log_bin inside a routine also raise the error.

Related Errors

MySQL Error 1419: ER_BINLOG_UNSAFE_ROUTINE

Raised when a routine is marked unsafe for statement-based replication. Resolve by rewriting the routine or switching to row-based replication.

MySQL Error 1336: ER_INVALID_ARGUMENT_FOR_LOGARITHM

Occurs when log_bin_trust_function_creators is off and a nondeterministic function lacks explicit characteristics. Declare the function DETERMINISTIC or enable the global variable.

MySQL Error 1647: ER_NO_BINLOG_ORDER_INFO

Pops up if binlog order information is missing for group replication. Ensure proper GTID and order configurations.

FAQs

Can I bypass Error 1695 with super privileges?

No. Even a SUPER user cannot change sql_log_bin inside a stored routine. The restriction is hard coded for replication safety.

Will setting log_bin_trust_function_creators help?

No. That variable only affects deterministic checks. It does not allow sql_log_bin changes inside routines.

Does the error occur in MySQL 8.0?

Yes. The rule applies across supported versions including 5.5, 5.6, 5.7, and 8.0.

How does Galaxy help avoid this error?

Galaxy's AI copilot flags attempts to set sql_log_bin inside stored code during editing, suggesting safe alternatives before you run the script.

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