MySQL登录超时退出设置

1、MySQL登录超时退出介绍

  登录超时退出策略指的是当用户在登录后一定时间内未进行任何操作,系统会自动断开用户的连接。为了保障数据库的安全性,可以启用登录超时退出策略,能够大大降低被攻击的风险。这一策略能有效防止因长时间不活动而导致的未授权访问,以及减少因意外长时间连接造成的资源浪费问题。

2、实验环境

  数据库版本:8.0.45 MySQL Community Server - GPL

  操作系统:Windows Server 2025

3、查看登录超时退出

  在MySQL数据库中,通过设置服务器系统变量 wait_timeout 和 interactive_timeout 来实现用户的连接超时策略。
  wait_timeout   指的是非交互式连接的超时时间。
  interactive_timeout   则是指交互式连接的超时时间。
  这两个参数的单位都是秒,默认值通常为28800秒(即8小时)
  使用以下语句查询当前MySQL数据库登录超时退出策略:
mysql> show global variables like '%timeout%';
+-----------------------------------+----------+
| Variable_name                     | Value    |
+-----------------------------------+----------+
| connect_timeout                   | 10       |
| delayed_insert_timeout            | 300      |
| have_statement_timeout            | YES      |
| innodb_flush_log_at_timeout       | 1        |
| innodb_lock_wait_timeout          | 50       |
| innodb_rollback_on_timeout        | OFF      |
| interactive_timeout               | 28800    |  --交互式连接超时时间,默认28800秒,即8小时
| lock_wait_timeout                 | 31536000 |
| mysqlx_connect_timeout            | 30       |
| mysqlx_idle_worker_thread_timeout | 60       |
| mysqlx_interactive_timeout        | 28800    |
| mysqlx_port_open_timeout          | 0        |
| mysqlx_read_timeout               | 30       |
| mysqlx_wait_timeout               | 28800    |
| mysqlx_write_timeout              | 60       |
| net_read_timeout                  | 30       |
| net_write_timeout                 | 60       |
| replica_net_timeout               | 60       |
| rpl_stop_replica_timeout          | 31536000 |
| rpl_stop_slave_timeout            | 31536000 |
| slave_net_timeout                 | 60       |
| ssl_session_cache_timeout         | 300      |
| wait_timeout                      | 28800    |  --非交互式连接超时时间,默认28800秒,即8小时
+-----------------------------------+----------+
23 rows in set, 1 warning (0.00 sec)

4、设置登录超时退出

4.1、永久配置

  编辑配置文件my.ini(Windows默认位置C:\ProgramData\MySQL\MySQL Server 8.0),在mysqld部分添加以下内容:

[mysqld]
wait_timeout=300
interactive_timeout=300

  修改完成后重启数据库生效:

PS C:\Users\Administrator\Desktop> net stop MySQL80
MySQL80 服务正在停止.
MySQL80 服务已成功停止。

PS C:\Users\Administrator\Desktop> net start MySQL80
MySQL80 服务正在启动 .
MySQL80 服务已经启动成功。

  查看是否生效:

mysql> show global variables like '%timeout%';
+-----------------------------------+----------+
| Variable_name                     | Value    |
+-----------------------------------+----------+
| interactive_timeout               | 300      |  
| wait_timeout                      | 300      |
+-----------------------------------+----------+
23 rows in set, 1 warning (0.01 sec)

4.2、临时配置 

  使用以下SQL语句进行修改:

mysql> SET GLOBAL interactive_timeout=1800;
Query OK, 0 rows affected (0.00 sec)

mysql> SET GLOBAL wait_timeout=1800;
Query OK, 0 rows affected (0.00 sec)

  查看是否生效:

mysql> show global variables like '%timeout%';
+-----------------------------------+----------+
| Variable_name                     | Value    |
+-----------------------------------+----------+
| interactive_timeout               | 1800     |
| wait_timeout                      | 1800     |
+-----------------------------------+----------+
23 rows in set, 1 warning (0.00 sec)

 

 

 

 

参考链接:https://blog.51cto.com/u_16175513/12082691

     https://blog.51cto.com/u_16175438/12840485

 

  

 

posted @ 2026-01-29 12:36  BK小君  阅读(4)  评论(0)    收藏  举报