zoukankan      html  css  js  c++  java
  • The Downside of MySQL Auto-reconnect

    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/

  • 相关阅读:
    python爬虫-selenium八大定位笔记
    git pull : error: cannot lock ref 'refs/remotes/origin/*' (unable to update local ref) 解决方案
    Lua table.sort()原理和使用的坑
    Unity---有关游戏物体角度的两种赋值方法这件事
    C# #if、#endif和预处理指令
    Unity 4大坐标系 和 屏幕坐标与UI坐标的转换问题
    第2次参加游戏开发比赛
    Unity Text添加空格导致换行问题的两种解决方法(还有lua的解决方法)
    MySQL是如何处理千万级数据
    PHP使用守护进程处理队列
  • 原文地址:https://www.cnblogs.com/DataArt/p/10174444.html
Copyright © 2011-2022 走看看