`

MySQL查询超时的设置

 
阅读更多
介绍MySQL查询超时的设置方法
2013-05-06 00:00中国IT实验室佚名
关键字:mySQL
  为了优化OceanBase的query timeout设置方式,特调研MySQL关于timeout的处理,记录如下。

  [plain]

  mysql> show variables like '%time%';

  +----------------------------+-------------------+

  | Variable_name | Value |

  +----------------------------+-------------------+

  | connect_timeout | 10 |

  | datetime_format | %Y-%m-%d %H:%i:%s |

  | delayed_insert_timeout | 300 |

  | flush_time | 1800 |

  | innodb_lock_wait_timeout | 50 |

  | innodb_old_blocks_time | 0 |

  | innodb_rollback_on_timeout | OFF |

  | interactive_timeout | 28800 |

  | lc_time_names | en_US |

  | lock_wait_timeout | 31536000 |

  | long_query_time | 10.000000 |

  | net_read_timeout | 30 |

  | net_write_timeout | 60 |

  | slave_net_timeout | 3600 |

  | slow_launch_time | 2 |

  | system_time_zone | |

  | time_format | %H:%i:%s |

  | time_zone | SYSTEM |

  | timed_mutexes | OFF |

  | timestamp | 1366027807 |

  | wait_timeout | 28800 |

  +----------------------------+-------------------+

  21 rows in set, 1 warning (0.00 sec)

  重点解释其中几个参数:

  connect_timeout:

  The number of seconds that the mysqld server waits for a connect packet before respondingwith Bad handshake. The default value is 10 seconds as of MySQL 5.1.23 and 5 seconds before that. Increasing the connect_timeout value might help if clients frequently encounter errors of the form Lost connection to MySQL server at ‘XXX’, system error: errno.

  解释:在获取链接时,等待握手的超时时间,只在登录时有效,登录成功这个参数就不管事了。主要是为了防止网络不佳时应用重连导致连接数涨太快,一般默认即可。

  interactive_timeout:

  The number of seconds the server waits for activity on an interactive connection before closing it. An interactive client is defined as a client that uses the CLIENT_INTERACTIVE option to mysql_real_connect(). See alsowait_timeout.

  解释:一个持续SLEEP状态的线程多久被关闭。线程每次被使用都会被唤醒为acrivity状态,执行完Query后成为interactive状态,重新开始计时。wait_timeout不同在于只作用于TCP/IP和Socket链接的线程,意义是一样的。

  MySQL可以配置连接的超时时间,这个时间如果做得太长,甚至到了10min,那么很可能发生这种情况,3000个链接都被占满而且sleep在哪,新链接进不来,导致无法正常服务。因此这个配置尽量配置一个符合逻辑的值,60s或者120s等等。

  说人话:

  命令行下面敲一个命令后,直至下一个命令到来之前的时间间隔为interactive_time,如果这个时间间隔超过了interactive_timeout,则连接会被自动断开,下一个命令失败。不过一般的mysql客户端都有自动重连机制,下一个命令会在重连后执行。

  [sql]

  mysql> set interactive_timeout = 1;

  Query OK, 0 rows affected (0.00 sec)

  mysql> show session variables like '%timeout%';

  +----------------------------+----------+

  | Variable_name | Value |

  +----------------------------+----------+

  | connect_timeout | 10 |

  | interactive_timeout | 1 |

  | wait_timeout | 28800 |

  +----------------------------+----------+

  10 rows in set (0.00 sec)

  =====

  [sql]

  mysql> set wait_timeout = 1;

  Query OK, 0 rows affected (0.00 sec)

  【去泡杯茶,等会儿】

  mysql> show session variables like '%timeout%';

  ERROR 2006 (HY000): MySQL server has gone away

  No connection. Trying to reconnect...

  Connection id: 7

  Current database: *** NONE ***

  +----------------------------+----------+

  | Variable_name | Value |

  +----------------------------+----------+

  | connect_timeout | 10 |

  | interactive_timeout | 28800 |

  | wait_timeout | 28800 |

  +----------------------------+----------+

  10 rows in set (0.01 sec)

  wait_timeout:

  The number of seconds the server waits for activity on a noninteractive connection (连接上没有活动命令,可能是客户端喝咖啡去了。)before closing it. Before MySQL 5.1.41, this timeout applies only to TCP/IP connections, not to connections made through Unix socket files, named pipes, or shared memory.

  On thread startup, the session wait_timeout value is initialized from the global wait_timeout value or from the global interactive_timeout value, depending on the type of client

  这里顺带解释一下什么是non-interactive connection

  > Non-Interactive Commands

  Just do a quick look up on a table without logging into the client, running the query then logging back out again.

  You can instead just type one line using the ' -e ' flag.

  [sql]

  c:\mysql\bin\mysql -u admin -p myDatabase -e 'SELECT * FROM employee'

  net_read_timeout / net_write_timeout

  The number of seconds to wait for more data from a connection before aborting the read. Before MySQL 5.1.41, this timeout applies only to TCP/IP connections, not to connections made through Unix socket files, named pipes, or shared memory. When the server is reading from the client, net_read_timeout is the timeout value controlling when to abort. When the server is writing to the client, net_write_timeout is the timeout value controlling when to abort. See also slave_net_timeout.

  On Linux, the NO_ALarm build flag affects timeout behavior as indicated in the description of the net_retry_count system variable.

  解释:这个参数只对TCP/IP链接有效,分别是数据库等待接收客户端发送网络包和发送网络包给客户端的超时时间,这是在Activity状态下的线程才有效的参数

  JDBC setQueryTimeout函数:

  为了避免查询出现死循环,或时间过长等现象,而导致线程阻塞,在获得Statement的实例后,stmt.setQueryTimeout(10); 避免因为查询导致程序出现线程阻塞。

  但昨天发现程序出现了,“ORA-01013: 用户请求取消当前的操作”的异常。手工执行出错SQL语句发现,这个语句耗时20多秒。因为setQueryTimeout(10),所以还没有执行完查询语句就抛出异常了。使用setQueryTimeout(10)时一定要把时间设置的长一些,如60秒以上。只要不导致线程长期阻塞,就可以。太短了容易抛出,“ORA-01013: 用户请求取消当前的操作”的异常

  JDBC实现setQueryTimeout的原理:

  [java]

  class IfxCancelQueryImpl extends TimerTask

  implements IfmxCancelQuery

  {

  IfxStatement stmt;

  Timer t = null;

  public void startCancel(IfxStatement paramIfxStatement, int paramInt)

  throws Exception

  {

  this.stmt = paramIfxStatement;

  this.t = new Timer(true);

  this.t.schedule(this, paramInt * 1000);

  }

  public void run()

  {

  try

  {

  this.stmt.cancel();

  this.t.cancel();

  }

  catch (SQLException localSQLException)

  {

  this.t.cancel();

  throw new Error(localSQLException.getErrorCode() + ":" + localSQLException.getMessage());

  }

  }

  }

  可见,query timeout是通过客户端解决方案来做的,服务器端无需知晓。通过一个timer线程来监控执行时间,如果执行时间超时,则会schedule run()函数。
分享到:
评论

相关推荐

    nodejs 关于mysql模块 连接超时自动断开解决方法

    nodejs 关于mysql模块 连接超时自动断开解决方法,在这块遇到的坑,而且坑了很久才找到的,分享只为求分

    qt中mysql超时自动断开连解决方案.txt

    可以直接通过设置来达到重连问题,方法很简单,里面有复现自动断开连接导致不能正常插入数据的方法,是自己没有积分所以才要积分

    SQL查询超时的设置方法(关于timeout的处理)

    在MySQL数据库管理中,SQL查询超时的设置是性能优化的重要环节,特别是在处理大量数据或复杂查询时。本文主要探讨了如何调整MySQL的超时设置,以适应OceanBase的query timeout需求。首先,我们通过`show variables ...

    mysql 事务等待锁超时分析

    mysql 事务等待锁超时分析

    mysql 8小时空闲后连接超时的问题

    MySQL数据库在设计时为了优化资源使用和安全性,设置了一些连接超时机制。当数据库客户端与服务器之间的连接在一定时间内无任何交互(即空闲),MySQL会自动关闭这个连接以释放系统资源。通常,默认的`wait_timeout`...

    MySQL数据库参数设置

    ### MySQL数据库参数设置详解 #### 一、概述 在MySQL数据库管理中,合理地设置数据库参数对于提升系统性能、确保数据安全与完整性至关重要。本文档将详细介绍一个针对MySQL 5.6版本的服务器参数设置模版,适用于...

    Qt Mysql超时断开后自动重连完美解决方法

    网上找了很多解决办法,几乎没有一个能完美解决问题的,大体都是首先执行一段query后看返回结果,但这个办法也太无用和耗费资源了吧...因为断开后isOpen和isValid都会返回true,所以不能从这里入手...

    mysql用navicat查询数据量大的数据,运行缓慢查不出来,且报超时错误的问题解决.txt

    mysql用navicat查询数据量大的数据,运行缓慢查不出来,且报超时错误的问题解决

    c#socket连接池和连接超时时间设置

    提供一个c# socket连接池设计的例子,解决socket并发连接限制的问题,并且提供一种设置连接超时时间的方法,默认连接超时时间是不能设置的,提供了socket网络发送数据的接口.可用于pos小票打印机通讯。

    关于MySQL的wait-timeout连接超时问题报错解决方案.docx

    这是因为 Hibernate 框架在执行数据库操作时,可能会长时间保持连接,而 MySQL 的 wait_timeout 参数设置的太小,导致连接超时。 error 信息分析 在出现连接超时问题时,错误信息可能如下所示: ERROR [org....

    VB使用mysql例子(带控件)

    创建一个新的命令对象,设置其CommandText属性为SQL语句,然后使用`ExecuteReader()`或`ExecuteNonQuery()`方法执行。 ```vb Dim cmd As New MySqlCommand("SELECT * FROM myTable", conn) Dim reader As ...

    HeidiSQL mysql查询工具

    **HeidiSQL:一款强大的MySQL查询工具** HeidiSQL是一款专为MySQL数据库设计的轻量级客户端工具,它以其简洁的界面、丰富的功能和高效的操作性赢得了广大用户的喜爱。作为一个专业的MySQL管理工具,HeidiSQL提供了...

    C#在MySQL大量数据下的高效读取、写入详解

    同时,设置命令超时以防止在处理海量数据时出现超时。 ```csharp using (var conn = new MySqlConnection("Connection String...")) { conn.Open(); // 设置超时 var c = new MySqlCommand("set ...

    MySQL wait_timeout连接超时

    确保正确配置连接池的超时和验证设置至关重要。 5. **连接有效性检查**:在获取连接之前,应用可以进行连接有效性检查。例如,在执行任何操作前先尝试一个简单的查询,如果失败则重新获取连接。 6. **事务管理**:...

    解决PHP mysql_query执行超时(Fatal error: Maximum execution time …)

    您可能感兴趣的文章:C#访问SqlServer设置链接超时的方法SQL查询超时的设置方法(关于timeout的处理)Mysql DNS反向解析导致连接超时过程分析(skip-name-resolve)PHP访问MySQL查询超时处理的方法SQL语句执行

    mysql windows安全审计、登录失败、口令策略设置

    mysql windows安全审计、登录失败、口令策略设置

    MySQL_C_API.rar_API_mysql_doc_mysql api_mysql c语言

    10. **性能优化**:使用`mysql_options()`可以设置各种选项来优化性能,例如设置连接超时时间、重试次数等。 文档“MySQL与C API.doc”会详细解释这些函数的使用方法,示例代码以及最佳实践。学习和熟练掌握这些...

Global site tag (gtag.js) - Google Analytics