MySQL如何知道连接何时“结束”?

时间:2016-06-27 16:09:09

标签: mysql

我刚刚在命令提示符下单击x关闭了一个MySQL shell,现在我很好奇当你关闭窗口时数据库连接会发生什么。

我知道MySQL服务器在端口3306上运行。如果我理解正确,服务器会等待连接请求并将它们传递给新的线程/可用线程来管理连接。

服务器如何知道连接何时终止,以便线程可以被释放或放回到可用线程池中?

场景如:

  1. 与远程连接上的MySQL服务器连接时的蓝屏。
  2. 程序在可以调用断开连接请求之前终止。

1 个答案:

答案 0 :(得分:1)

来自mysql客户端:

show processlist;
+-----+------+-----------------+---------------+---------+-------+-------+------------------+
| Id  | User | Host            | db            | Command | Time  | State | Info             |
+-----+------+-----------------+---------------+---------+-------+-------+------------------+
|   2 | root | localhost:6509  | so_gibberish2 | Sleep   |   105 |       | NULL             |
|   3 | root | localhost:6510  | so_gibberish2 | Sleep   |   105 |       | NULL             |
| 820 | root | localhost:40286 | stackoverflow | Sleep   | 13457 |       | NULL             |
| 821 | root | localhost:40287 | stackoverflow | Sleep   |   966 |       | NULL             |
| 827 | root | localhost:42254 | so_gibberish2 | Query   |     0 | init  | show processlist |
| 831 | root | localhost:44036 | stackoverflow | Sleep   |   230 |       | NULL             |
+-----+------+-----------------+---------------+---------+-------+-------+------------------+

select connection_id();
+-----------------+
| connection_id() |
+-----------------+
|             827 |
+-----------------+

现在点击[x]就像你对运行上面的mysql客户端所说的那样。

以下是来自Mysql Workbench:

show processlist;
+-----+------+-----------------+---------------+---------+-------+-------+------------------+
| Id  | User | Host            | db            | Command | Time  | State | Info             |
+-----+------+-----------------+---------------+---------+-------+-------+------------------+
|   2 | root | localhost:6509  | so_gibberish2 | Sleep   |   105 |       | NULL             |
|   3 | root | localhost:6510  | so_gibberish2 | Sleep   |   105 |       | NULL             |
| 820 | root | localhost:40286 | stackoverflow | Sleep   | 13457 |       | NULL             |
| 821 | root | localhost:40287 | stackoverflow | Sleep   |   966 |       | NULL             |
+-----+------+-----------------+---------------+---------+-------+-------+------------------+
select connection_id();
+-----------------+
| connection_id() |
+-----------------+
|               3 |
+-----------------+

因此关闭一个程序会自然地关闭它所有的开放套接字。

老实说,后者的输出更像是: enter image description here

但是我总是使用mysql客户端来输出表格的输出格式,这会抛弃结果。所以你有它作为一个图像。