CI框架出现 mysql server has gone away的解决办法

来源:互联网 发布:暗黑战神 源码 编辑:程序博客网 时间:2024/06/03 22:00

项目中测试环境后台运行的php进程出现mysql server has gone away的问题,但同样的代码和配置线上环境没有问题。

研究半天后来重启测试环境后台进程,问题就没有了。(线上环境进程会定时重启)

因此CI框架中出现这个问题可以将db配置设置成

$db['default']['pconnect'] = FALSE;


开启pconnect的作用是:

  1. 当连接的时候本函数将先尝试寻找一个在同一个主机上用同样的用户名和密码已经打开的(持久)连接,如果找到,则返回此连接标识而不打开新连接。
  2. 其次,当脚本执行完毕后到 SQL 服务器的连接不会被关闭,此连接将保持打开以备以后使用(mysql_close() 不会关闭由 mysql_pconnect() 建立的连接)

下面是在stackoverflow上找到的该配置优缺点,就不翻译了偷笑

  • If you have:
    • Dedicated web server and database hardware in production
    • and have tuned the web server and database correctly
    • and have an accurate production-like test environment
    • And still think your performance problems are caused by database connection time,

CONSIDER turning it on

Persistent connections can cause

  • Bugs because some connection state persisted unintentionally (this is a biggie!)
  • Database connection limits to be exceeded
  • Database performance to drop because of lots of ram used by the many (mostly idle) connections
  • Bugs because connections have gone "stale" and the app didn't notice

But CAN

  • Reduce latency on initial connection

原创粉丝点击