mysql sql_log_bin and log_bin

sql_log_bin 是什么?他和 log_bin 有什么关系?为什么 关闭了log_bin,sql_log_bin 还是ON?如何关闭 sql_log_bin ? 关闭 sql_log_bin 有什么影响?
 show variables like '%log_bin%';

输出:

Variable_name Value
log_bin OFF
log_bin_basename
log_bin_index
log_bin_trust_function_creators OFF
log_bin_use_v1_row_events OFF
sql_log_bin ON

注意最后一行:sql_log_bin ON

这就是本文的重点:

sql_log_bin 是什么?他和 log_bin 有什么关系?为什么 关闭了log_bin,sql_log_bin 还是ON,如何关闭 sql_log_bin ? 关闭 sql_log_bin 有什么影响?

sql_log_bin 是什么?

SET sql_log_bin = {OFF|ON}

Mysql 文档原文

The sql_log_bin variable controls whether logging to the binary log is enabled for the current session (assuming that the binary log itself is enabled). The default value is ON. To disable or enable binary logging for the current session, set the session sql_log_bin variable to OFF or ON.

Set this variable to OFF for a session to temporarily disable binary logging while making changes to the source you do not want replicated to the replica.

Setting the session value of this system variable is a restricted operation. The session user must have privileges sufficient to set restricted session variables. See Section 5.1.8.1, “System Variable Privileges”.

It is not possible to set the session value of sql_log_bin within a transaction or subquery.

Setting this variable to OFF prevents GTIDs from being assigned to transactions in the binary log. If you are using GTIDs for replication, this means that even when binary logging is later enabled again, the GTIDs written into the log from this point do not account for any transactions that occurred in the meantime, so in effect those transactions are lost.

The global sql_log_bin variable is read only and cannot be modified. The global scope is deprecated; expect it to be removed in a future MySQL release.

翻译:

sql_log_bin变量控制是否为当前会话启用对二进制日志的日志记录(假设二进制日志本身已启用)。缺省值为 ON。要禁用或启用当前会话的二进制日志记录,请将会话sql_log_bin变量设置为 OFF 或 ON 。

将此变量设置OFF用于会话,以便在对不希望复制到副本的源进行更改时暂时禁用二进制日志记录。

设置此系统变量的会话值是受限制的操作。会话用户必须具有足够的权限来设置受限制的会话变量。请参见第 5.1.8.1 节 “系统变量特权”。

无法在事务或子查询中设置sql_log_bin会话值。

将此变量设置为 OFF 可防止将 GTID 分配给二进制日志中的事务。如果使用 GTID 进行复制,这意味着即使以后再次启用二进制日志记录,从此时写入日志的 GTID 也不会考虑在此期间发生的任何事务,因此实际上这些事务将丢失。

全局sql_log_bin变量是只读的,不能修改。全局范围已弃用;预计它将在将来的MySQL版本中被删除。

总结起来两句话:

  • 1、sql_log_bin有全局和会话两个级别的设置,全局级别目前为只读,将来会舍弃,会话级别不能用在事务或子查询
  • 2、设为OFF后,在Master数据库上执行的语句都不记录binlog,所以也不会同步到从库

和 log_bin 有什么关系?

  • 根据官方文档,sql_log_bin主要用来控制会话级别是否记录日志,使用场景非常有限,并且受到 log_bin 限制
  • log_bin如果关闭,sql_log_bin无论开关,都不会记录日志文件

如何关闭 sql_log_bin ?

  • 无法全局关闭 sql_log_bin,全局 sql_log_bin 将会弃用
  • 会话级别 sql_log_bin 通过 ON 和 OFF 控制开关
  • 无法在事务或子查询中设置 sql_log_bin

关闭 sql_log_bin 有什么影响?

  • 设为OFF后,在Master数据库上执行的语句都不记录binlog,所以也不会同步到从库
# mysql  

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×