file_get_contents 超时 mysql关闭
今天使用 file_get_contents 获取网页内容,代码是这样的:
$opts = array("http" => array("method" => "GET","timeout" => 60,"header" => "Host:www.****.com\r\n" . "User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36\r\n")); $context = stream_context_create($opts); $html = @file_get_contents("http://www.***.com/",false,$context);
看:"timeout" => 60 ,file_get_contents 该函数超时时间设置为60秒,而我的php空间mysql连接超时时间同样是60秒。所以,百度一下,找到这篇文章:
以下是摘要:
最近做网站有一个站要用到WEB网页采集器功能,当一个PHP脚本在请求URL的时候,可能这个被请求的网页非常慢慢,超过了mysql的 wait-timeout时间,然后当网页内容被抓回来后,准备插入到MySQL的时候,发现MySQL的连接超时关闭了,于是就出现了"MySQL server has gone away"这样的错误提示,解决这个问题,我的经验有以下两点,或许对大家有用处:
原文出自【比特网】,转载请保留原文链接:http://soft.chinabyte.com/os/132/12546132.shtml
或者,将file_get_contents 超时时间设置30秒,"timeout" => 30。30秒都打不开网页也就算了吧。
我还找了好多资料,这次真是学习了。
MySQL server has gone away的原因及解决方案
http://blog.163.com/lgh_2002/blog/static/44017526201261910441639/
如何解决mysql server has gone away
http://jingyan.baidu.com/article/ea24bc399f6bb9da62b3311c.html //wamp目录下有这个文件:my.ini 又叫 my.cnf
MySQL max_allowed_packet设置及问题
http://blog.csdn.net/loseinworld/article/details/6856261
MySQL里的set global wait_timeout
http://blog.csdn.net/wulantian/article/details/21461363 //注意:这里一个容易把人搞蒙的地方是如果查询时使用的是show variables的话,会发现设置好像并没有生效,这是因为单纯使用show variables的话就等同于使用的是show session variables,查询的是会话变量,只有使用show global variables,查询的才是全局变量。
MySQL的timeout那点事
http://www.penglixun.com/tech/database/mysql_timeout.html
mysql查看connect_timeout设置
http://www.360sdn.com/mysql/2013/0724/603.html