MySQL wait_timeout 变量 - GLOBAL 与 SESSION

发布于 2024-10-07 16:51:10 字数 245 浏览 6 评论 0原文

SHOW VARIABLES LIKE "%wait%"

Result: 28800

SET @@GLOBAL.wait_timeout=300

SHOW GLOBAL VARIABLES LIKE "%wait%"

Result: 300

SHOW SESSION VARIABLES LIKE "%wait%"

Result:28800

我对结果感到困惑。为什么最后一个查询给出 Result:28800 ?

SHOW VARIABLES LIKE "%wait%"

Result: 28800

SET @@GLOBAL.wait_timeout=300

SHOW GLOBAL VARIABLES LIKE "%wait%"

Result: 300

SHOW SESSION VARIABLES LIKE "%wait%"

Result:28800

I am confused by the results. Why does the last query give Result:28800 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你的心境我的脸 2024-10-14 16:51:10

您的会话状态在启动会话后设置,并且默认情况下采用当前的 GLOBAL 值。

如果您在执行 SET @@GLOBAL.wait_timeout=300 后断开连接,然后重新连接,您会看到

SHOW SESSION VARIABLES LIKE "%wait%";

Result: 300

同样,在任何时候,如果您这样做,

mysql> SET session wait_timeout=300;

您会得到

mysql> SHOW SESSION VARIABLES LIKE 'wait_timeout';

+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wait_timeout  | 300   |
+---------------+-------+

Your session status are set once you start a session, and by default, take the current GLOBAL value.

If you disconnected after you did SET @@GLOBAL.wait_timeout=300, then subsequently reconnected, you'd see

SHOW SESSION VARIABLES LIKE "%wait%";

Result: 300

Similarly, at any time, if you did

mysql> SET session wait_timeout=300;

You'd get

mysql> SHOW SESSION VARIABLES LIKE 'wait_timeout';

+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wait_timeout  | 300   |
+---------------+-------+
那请放手 2024-10-14 16:51:10
SHOW SESSION VARIABLES LIKE "wait_timeout"; -- 28800
SHOW GLOBAL VARIABLES LIKE "wait_timeout"; -- 28800

首先,wait_timeout = 28800,这是默认值。要更改会话值,您需要设置全局变量,因为会话变量是只读的。

SET @@GLOBAL.wait_timeout=300

设置全局变量后,会话变量会自动获取该值。

SHOW SESSION VARIABLES LIKE "wait_timeout"; -- 300
SHOW GLOBAL VARIABLES LIKE "wait_timeout"; -- 300

下次服务器重新启动时,会话变量将设置为默认值,即28800。PS

我使用MySQL 5.6.16

SHOW SESSION VARIABLES LIKE "wait_timeout"; -- 28800
SHOW GLOBAL VARIABLES LIKE "wait_timeout"; -- 28800

At first, wait_timeout = 28800 which is the default value. To change the session value, you need to set the global variable because the session variable is read-only.

SET @@GLOBAL.wait_timeout=300

After you set the global variable, the session variable automatically grabs the value.

SHOW SESSION VARIABLES LIKE "wait_timeout"; -- 300
SHOW GLOBAL VARIABLES LIKE "wait_timeout"; -- 300

Next time when the server restarts, the session variables will be set to the default value i.e. 28800.

P.S. I m using MySQL 5.6.16

霓裳挽歌倾城醉 2024-10-14 16:51:10

正如 Riedsio 所指出的,会话变量在连接后不会更改,除非您专门设置它们;设置全局变量只会更改下一个连接的会话值。

例如,如果您有 100 个连接,并且降低了全局 wait_timeout,那么它不会影响现有连接,只会影响变量更改后的新连接。

不过,特别是对于 wait_timeout 变量,有一个变化。
如果您在交互模式下使用 mysql 客户端,或者通过 mysql_real_connect() 设置 CLIENT_INTERACTIVE 连接器,那么您将看到 interactive_timeout 设置为 @@session.wait_timeout

在这里您可以看到这一点:

> ./bin/mysql -Bsse 'select @@session.wait_timeout, @@session.interactive_timeout, @@global.wait_timeout, @@global.interactive_timeout' 
70      60      70      60

> ./bin/mysql -Bsse 'select @@wait_timeout'                                                                                                 
70

> ./bin/mysql                                                                                                                               
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.12-5 MySQL Community Server (GPL)

Copyright (c) 2009-2016 Percona LLC and/or its affiliates
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select @@wait_timeout;
+----------------+
| @@wait_timeout |
+----------------+
|             60 |
+----------------+
1 row in set (0.00 sec)

因此,如果您使用客户端对此进行测试,则它是您需要的 interactive_timeout将在连接时看到,而不是 wait_timeout 的值

As noted by Riedsio, the session variables do not change after connecting unless you specifically set them; setting the global variable only changes the session value of your next connection.

For example, if you have 100 connections and you lower the global wait_timeout then it will not affect the existing connections, only new ones after the variable was changed.

Specifically for the wait_timeout variable though, there is a twist.
If you are using the mysql client in the interactive mode, or the connector with CLIENT_INTERACTIVE set via mysql_real_connect() then you will see the interactive_timeout set for @@session.wait_timeout

Here you can see this demonstrated:

> ./bin/mysql -Bsse 'select @@session.wait_timeout, @@session.interactive_timeout, @@global.wait_timeout, @@global.interactive_timeout' 
70      60      70      60

> ./bin/mysql -Bsse 'select @@wait_timeout'                                                                                                 
70

> ./bin/mysql                                                                                                                               
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.12-5 MySQL Community Server (GPL)

Copyright (c) 2009-2016 Percona LLC and/or its affiliates
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select @@wait_timeout;
+----------------+
| @@wait_timeout |
+----------------+
|             60 |
+----------------+
1 row in set (0.00 sec)

So, if you are testing this using the client it is the interactive_timeout that you will see when connecting and not the value of wait_timeout

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文