Nginx + PHP mysql_pconnect = Database errors (Too many connections)
2012-05-23 10:38
676 查看
这里说了mysql中的connect和pconnect的东东
If you’re using NGinx spawn-cgi or FPM with PHP and calling mysql_pconnect, you are likely going to experience frequent database crashes and “Too many connections” errors.
This took a while to trace, but once you understand the issue, it all makes sense.
mysql_pconnect opens a “persistent” connection to the database. From the documentation: “the connection to the SQL server will not be closed when the execution of the script ends. Instead, the link will remain open for future use (mysql_close() will not close links established by mysql_pconnect()).”
The issue is that FPM keeps a number of php-cgi processes running in the background to process php scripts. These php-cgi processes never die and so MySQL connections keep open forever…
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:
If you’re using NGinx spawn-cgi or FPM with PHP and calling mysql_pconnect, you are likely going to experience frequent database crashes and “Too many connections” errors.
This took a while to trace, but once you understand the issue, it all makes sense.
mysql_pconnect opens a “persistent” connection to the database. From the documentation: “the connection to the SQL server will not be closed when the execution of the script ends. Instead, the link will remain open for future use (mysql_close() will not close links established by mysql_pconnect()).”
The issue is that FPM keeps a number of php-cgi processes running in the background to process php scripts. These php-cgi processes never die and so MySQL connections keep open forever…
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:
1 2 3 | [MySQL] ; Allow or prevent persistent links. mysql.allow_persistent = Off |
相关文章推荐
- Nginx + PHP mysql_pconnect = Database errors (Too many connections)
- Nginx + PHP mysql_pconnect = Database errors (Too many connections)
- PHP + Mysql 的Too many connections
- mysql出现too many connections错误提示
- 解决mysql 1040错误Too many connections的方法
- Troubleshooting For Mysql "Too many connections"
- mysql: Data source rejected establishment of connection, message from server: "Too many connections"
- SpringMVC中普通类里获取自动注解bean衍生出Mysql报Too many connections错误的问题
- MySQL 1040 错误 Too many connections的解决方法
- MySQL 1040 Too many connections
- 解决mysql错误 Too many connections的方法
- mysql 报错 Too many connections
- Mysql遇到Too many connections的解决办法
- 关于PHP出现Too many connections 的问题
- OpenStack环境出现MySQL ‘Too many connections’
- mysql出现Too many connections的解决...
- mysql----ERROR 1040 (HY000): Too many connections
- Mysql的错误,待解决[已解决]Data source rejected establishment of connection message from server: "Too many connections";
- MySQL遇到Too many connections的解决办法
- MySQL提示“too many connections”的解决办法