PHP mysql_connect设置wait_timeout [英] PHP mysql_connect set wait_timeout

查看:302
本文介绍了PHP mysql_connect设置wait_timeout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
MySQL服务器已消失-恰好在60秒内

Possible Duplicate:
MySQL server has gone away - in exactly 60 seconds

我们正在PHP中使用mysql_connect方法来创建守护程序的数据库句柄.问题是,该连接可能无法使用超过8个小时(有时超过几周).

We're using the mysql_connect method in PHP to create database handles for a daemon. The problem is, that connection might not be used for more than 8 hours (sometimes for more than a few weeks.)

由于遇到了wait_timeout,我们遇到了MySQL将结束会话的问题. http://dev.mysql.com/doc/refman/5.0/en/gone-away.html

We're running into issues where MySQL will end the session because the wait_timeout is reached. http://dev.mysql.com/doc/refman/5.0/en/gone-away.html

我们不想为所有连接增加该值.有没有一种方法只能通过PHP增加该句柄的超时时间?通过MySQL吗?

We don't want to increase this value for ALL connections. Is there a way to increase the timeout for that handle only through PHP? Through MySQL?

推荐答案

长时间挂在数据库连接上不好,因为数据库一次只能提供固定数量的连接;如果您使用不了多久,这意味着您的数据库处理其他请求的能力会降低,即使您实际上没有对该连接做任何事情.

It's not good to hang onto DB connections for long periods because the DB only provides a fixed number of connections at any one time; if you're using one up for ages it means your DB has less capacity to deal with other requests, even if you're not actually doing anything with that connection.

我建议暂时关闭该程序的连接,并在需要做更多数据库工作时重新连接.

I suggest dropping the connection if the program is finished using it for the time being, and re-connect when the time comes to do more DB work.

此外,此解决方案将保护您的程序免受可能的数据库停机时间的影响,即,如果您需要重新引导数据库服务器(即使在受支持的最佳网络中,这种情况也会发生).如果使连接保持活动状态(即,根据其他答案进行数据库ping操作),则类似的事件将使您面临与现在完全相同的问题.通过适当管理的连接(不需要时断开),即使您计划在数据库上进行停机,您也可以安全地使守护程序运行.只要它在此期间保持空闲状态,就不必再明智了.

In addition, this solution will protect your program from possible database downtime, ie if you need to reboot your DB server (it happens, even in the best supported network). If you keep the connection alive (ie a DB ping as per the other answers), then an event like that will leave you with exactly the same problem you have now. With a properly managed connection that is dropped when not needed, you can safely leave your daemon running even if you have planned downtime on your DB; as long as it remains idle for the duration, it needn't be any the wiser.

(顺便说一句,我还要质疑编写连续运行的PHP程序的智慧; PHP是为短期Web请求而设计的.它可能能够运行长期的守护程序,但是有更好的工具可用于工作)

(as an aside, I'd also question the wisdom of writing a PHP program that runs continuously; PHP is designed for short duration web requested. It may be capable of running long term daemon programs, but there are better tools for the job)

这篇关于PHP mysql_connect设置wait_timeout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆