2011-03-22 132 views
4

我有一个命令行脚本,当你第一次启动它:mysql_connect保持打开状态多久?

function __construct(){$this->connectToDatabase();} 
protected function connectToDatabase(){ 
    try{ 
     $this->databaseName = $this->dbname; 
     $this->posName = $this->posName; 
     $this->vlog = $this->vlogName; 
     $this->database = mysql_connect($this->dbhost, $this->dbuser, $this->dbpass); 
     mysql_select_db($this->databaseName, $this->database); 
    } 
    catch(Exception $e){ 
     $this->console($e); 
    } 
} 

此CLI脚本可以保持运行几天。我如何保持MySQL连接打开?或者在每个mysql_query之前,我必须检查连接是否仍然打开?

这是错误即时得到:MySQL Error: MySQL server has gone away

谢谢!

+0

你有任何与此问题?连接应该保持打开状态(除非有一些设置限制了服务器上可以打开连接的时间)。 – 2011-03-22 21:03:58

+0

是的,我有问题。基本上这是一个接受websocket连接的'服务器'cli。由于某种原因,当我尝试连接一天latr我得到了'MySQL错误:MySQL服务器已经消失'错误 – Neal 2011-03-22 21:05:26

回答

8

检查:http://techgurulive.com/how-to-solve-mysql-server-has-gone-away-or-lost-connection-to-server-during-query-fix/

特别是,它指出:

The server timed out and closed the connection. By default, the server closes the connection after 8 hours or 28800 seconds if nothing has happened. You can change the time limit by setting the wait_timeout variable when you start mysqld via your server’s /etc/my.cnf [...]

+0

我该如何改变? – Neal 2011-03-22 21:07:10

+0

已编辑答案从链接网站复制更多内容... – 2011-03-22 21:08:39

+0

这是一台服务器,必须至少每天运行12小时。我不能在工作时间内让它超时 – Neal 2011-03-22 21:08:48