天天看点

Nginx + PHP mysql_pconnect = Database errors (Too many connections)

这里说了mysql中的connect和pconnect的东东

This took a while to trace, but once you understand the issue, it all makes sense.

Sooner or later, you are going to run out of MySQL connections (or  worse yet – run out of file descriptors) and that’s when all hell breaks  loose.

And if that’s not enough, after doing some digging into  mysql_pconnect I found a few additional reasons NOT to use  mysql_pconnect:

1. If you use mysql_pconnect on a machine that has a local database  and you are connecting to a remote database, PHP will try to use the  same mysql connection for both databases.

2. Temporary tables don’t work with persistent connections (they are  only visible to the connection that was used to open the table)

3. Setting charset variables on a persistent connection, is going to impact all future queries on that connection as well

4. Calling mysql_pconnect twice (in the same script) with different parameters doesn’t work as expected

5. PHP 4.1 on Apache running with MySQL persistent connections, is known to memory leak (not flushing properly).

Bottom line, never ever use mysql_pconnect.

Replace all occurrences of mysql_pconnect with mysql_connect in your  code and in your php.ini file, prevent persistent connections:

本文转自 小强测试帮 51CTO博客,原文链接:http://blog.51cto.com/xqtesting/874027,如需转载请自行联系原作者