I just started using mysqli on my development server with PHP 5.1.6 and MySQL 5.0.27. We need mysqli to take advantage of some stored procedures that we’ve created.
However, the problem is we get this error ever once in a while:
Warning: mysqli::mysqli() [function.mysqli-mysqli]: (00000/1040): Too many connections in init.php on line 38
It’s especially annoying because once this error occurs, the MySQL server won’t come back up. I have no idea how we’re building up so many connections. We do call $mysqli->close() at the end of the scripts. We don’t have any extremely intensive queries that get called a lot.
It’s a development server and it’s not heavily trafficked. We have a whole box and probably only two or three people on it at a time. In php.ini I have mysqli.max_links = -1.
I saw a really old thread dealing with this [link], but does anybody have any more recent experience with this?
In the old thread the bit about explicitly freeing results was interesting.
I notice one thing everyone has in common is the setting:
mysqli.max_links = -1
Would the database prefer a cap on that setting?
If PHP is not going to cap the number of connections, then you need to ensure that your database allows at least as many simultaneous connections as your web server allows worker threads. Although unless you’re using persistent connections I can’t imagine such a low-traffic server ever eating up the entire pool of connections even if the web server allows more worker threads than the database server allows connections.
kromey: yeah, we’re not using persistent connections. mysqli doesn’t even allow them
Trent Reimer: I actually did put in the free_results() thing, but it didn’t fix the problem.
I did find out why we’re getting this error. The problem is that after we run any queries, we’re getting a bunch of connections that don’t go away and just “sleep.” That’s how it’s building up.