<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>
<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>
Cannot change the sql_log_bin inside a stored function or
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.
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.
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.
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.
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.
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.
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.
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.
A developer inserts SET sql_log_bin = 0 to suppress logging for bulk inserts, triggering error 1695 on first execution.
Legacy procedures that toggle binlogging before maintenance tasks fail after an upgrade to MySQL 5.5+ where enforcement tightened.
Schema migration tools sometimes inject sql_log_bin toggles in generated triggers, causing immediate failures on data change.
EXECUTE IMMEDIATE statements that evaluate to SET sql_log_bin inside a routine also raise the error.
Raised when a routine is marked unsafe for statement-based replication. Resolve by rewriting the routine or switching to row-based replication.
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.
Pops up if binlog order information is missing for group replication. Ensure proper GTID and order configurations.
No. Even a SUPER user cannot change sql_log_bin inside a stored routine. The restriction is hard coded for replication safety.
No. That variable only affects deterministic checks. It does not allow sql_log_bin changes inside routines.
Yes. The rule applies across supported versions including 5.5, 5.6, 5.7, and 8.0.
Galaxy's AI copilot flags attempts to set sql_log_bin inside stored code during editing, suggesting safe alternatives before you run the script.