A few days ago I was doing some cleanup on a passive master database using the MySQL client. I didn’t want my commands to be replicated so I executed set sql_log_bin=0 in my session.

One of my queries dropped an unused schema that I knew was corrupt, so I wasn’t too surprised when the drop database command crashed the MySQL server. After the crash, the server came back up quickly, and my client automatically reconnected, so it was safe to keep running queries right?

Wrong.

When the client reconnected I lost my session state, so sql_log_bin reverted to 1, and any commands I ran from that point forward would be replicated, which I did not want.

This behavior makes sense, and it’s documented in the manual:

Automatic reconnection can be convenient because you need not implement your own reconnect code, but if a reconnection does occur, several aspects of the connection state are reset on the server side and your application will not know about it. The connection-related state is affected as follows:

  • Any active transactions are rolled back and autocommit mode is reset.
  • All table locks are released.
  • All TEMPORARY tables are closed (and dropped).
  • Session variables are reinitialized to the values of the corresponding variables. This also affects variables that are set implicitly by statements such as SET NAMES.
  • User variable settings are lost.
  • Prepared statements are released.
  • HANDLER variables are closed.
  • The value of LAST_INSERT_ID() is reset to 0.
  • Locks acquired with GET_LOCK() are released.

But it’s easy to overlook such details when working with automatic features like MySQL client auto-reconnect. In this specific case I didn’t execute any other commands in the reconnected session so I didn’t inadvertantly replicate anything, but this incident served as a good reminder to be vigilant about my session state when using the MySQL client.

Here’s a snippet from my session to show the value of sql_log_bin before and after the crash:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
mysql> set sql_log_bin = 0;
Query OK, 0 rows affected (0.00 sec) mysql> select @@sql_log_bin;
+---------------+
| @@sql_log_bin |
+---------------+
| 0 |
+---------------+
1 row in set (0.00 sec) mysql> drop database test;
Query OK, 1 row affected (0.19 sec) mysql> select @@sql_log_bin;
+---------------+
| @@sql_log_bin |
+---------------+
| 0 |
+---------------+
1 row in set (0.00 sec) mysql> drop database reports;
ERROR 2013 (HY000): Lost connection to MySQL server during query
mysql> select @@sql_log_bin;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 505
Current database: *** NONE *** +---------------+
| @@sql_log_bin |
+---------------+
| 1 |
+---------------+
1 row in set (0.00 sec)

https://mechanics.flite.com/blog/2013/05/03/the-downside-of-mysql-auto-reconnect/

The Downside of MySQL Auto-reconnect的更多相关文章

  1. mysql AUTO INCREMENT字段 语法

    mysql AUTO INCREMENT字段 语法 作用:在新记录插入表中时生成一个唯一的数字 说明:我们通常希望在每次插入新记录时,自动地创建主键字段的值.我们可以在表中创建一个 auto-incr ...

  2. mysql auto reset

    参数说明: •相关参数说明: •dataSource: 要连接的 datasource (通常我们不会定义在 server.xml) defaultAutoCommit: 对于事务是否 autoCom ...

  3. MySQL(Percona Server) 5.6 主从复制

    MySQL(Percona Server) 5.6.15 主库:192.168.2.21 从库:192.168.2.22 例如我们同步的数据库为:test. 如果需要同步多个数据库下面会有说明. My ...

  4. 使用 xtrabackup 进行MySQL数据库物理备份

    0. xtrabackup的功能 能实现的功能: 非阻塞备份innodb等事务引擎数据库. 备份myisam表会阻塞(需要锁). 支持全备.增量备份.压缩备份. 快速增量备份(xtradb,原理类似于 ...

  5. centos升级mysql至5.7

    1.备份原数据库 [root@www ~] #mysqldump -u root –p -E –all-database > /home/db-backup.sql 加-E是因为mysqldum ...

  6. mysql 双机热备注意事项

    上一篇文章已经介绍过    主从复制,   本文对主从复制只是简单描述,如对主从复制不清楚的,可以先看上一篇文章   主从复制  一:介绍 mysql版本:5.7.20 第一个主服服务器ip:192. ...

  7. .NET Core+MySql+Nginx 容器化部署

    .NET Core容器化@Docker .NET Core容器化之多容器应用部署@Docker-Compose .NET Core+MySql+Nginx 容器化部署 GitHub-Demo:Dock ...

  8. Mysql主从复制_模式之日志点复制

    MySQL数据复制的原理 MySQL复制基于主服务器在二进制日志中跟踪所有对数据库的更改(更新.删除等等).因此,要进行复制,必须在主服务器上启用二进制日志. 每个从服务器从主服务器接收主服务器已经记 ...

  9. 安装Percona版本的MySQL主从复制

    准备两台虚拟机,按顺序执行1.1节的公共部分 1.1 首先安装 cmake # yum –y install cmake     //也需要安装gcc-c++,openssl openssl-deve ...

随机推荐

  1. linux下mysql5.7以上my.cnf配置文件配置

    简单配置,低配置服务器配置 [client] #客户端设置 port = 3306 socket = /data/mysql/data/mysql.sock default-character-set ...

  2. 基于Web Service的客户端框架搭建三:代理层(Proxy)

    前言 代理层的主要工作是调用Web Service,将在FCL层序列化好的Json数据字符串Post到Web Service,然后获得Reponse,再从响应流中读取到调用结果Json字符串,在Dis ...

  3. Solidity字符串和函数

    字符串:需要使用双引号""或者单引号''括起来,例如:定义一个字符串变量:string name="jake":string字符串不能通过length方法获得长 ...

  4. JVM内存结构(转)

    所有的Java开发人员可能会遇到这样的困惑?我该为堆内存设置多大空间呢?OutOfMemoryError的异常到底涉及到运行时数据的哪块区域?该怎么解决呢?其实如果你经常解决服务器性能问题,那么这些问 ...

  5. springMVC实现 MultipartFile 多文件上传

    1.Maven引入所需的 jar 包(或自行下载) <dependency> <groupId>commons-io</groupId> <artifactI ...

  6. js闭包应用

    先来看一个例子: function foo() { var a = 10; function bar() { a *= 2; return a; } return bar; } var baz = f ...

  7. CAlayer二

    下面学习一下图层的anchorPoint,position属性在ViewDidLoad中self.View添加View1,在View1中添加图层calayer self.view1=[[UIView ...

  8. 部署---阿里云服务器,linux, ubuntu ,部署django用到的一些命令

    部署项目<下课说>APP时,总结出的一些命令和方法细节 Linux.ubuntu.django.uwsgi.nginx.mysql 里面有些是查找的资料,我也不大懂[手动笑哭],这还是部署 ...

  9. c++语言对c的扩充

    1.命名空间的使用 参见下列链接:http://www.cnblogs.com/uniqueliu/archive/2011/07/10/2102238.html 需要注意的地方:如果使用了命名空间s ...

  10. 深入出不来nodejs源码-流程总览

    花了差不多两周时间过了下primer C++5th,完成了<C++从入门到精通>.(手动滑稽) 这两天看了下node源码的一些入口方法,其实还是比较懵逼的,语法倒不是难点,主要是大量的宏造 ...