Forum Moderators: coopster
I have a problem with MySQL connections on my server. There are only about 10 different databases that are regularly accessed, using PHP scripts. This is done using mysql_pconnect, so the connections should be persistent. From what I learned, this would mean that if another connection is necessary for the same database/user combination, an old, already existing connection can be used without the need for establishing a new one. This sounds good to me, because I have only few databases that are often accessed, so this way there are only few connections open. Recently, however, I had a major problem when there were too many connections. There were more than the maximum, so no more connections were allowed. When I showed the processes, I saw that there were a lot of the same connections (same user accessing the same database). I do now understand why. If I use persistant connections, there is no need to create all these duplicate connections, right?
I now kill off manually the connections every now and then so that there are not too many, but that's not a long term solution. I hope somebody knows how to limit the number of connections automatically.
Thanks,
Paul
mightye (at) mightye (dot) org
03-Feb-2004 04:19
Normally you do NOT want to use mysql_pconnect. This function is designed for environments which have a high overhead to connecting to the database. In a typical MySQL / Apache / PHP environment, Apache will create many child processes which lie in idle waiting for a web request to be assigned to them. Each of these child processes will open and hold its own MySQL connection. So if you have a MySQL server which has a limit of 50 connections, but Apache keeps more than 50 child processes running, each of these child processes can hold a connection to your MySQL server, even while they are idle (idle httpd child processes don't lend their MySQL connection to other httpd children, they hold their own). So even if you only have a few pages which actually connect to MySQL on a busy site, you can run out of connections, with all of them not actually being used.
In general use mysql_connect() for connecting to MySQL unless that connection takes a long time to establish.
Also if you do use pconnect you might want to look at the use of wait_timeout/interactive_timeout.
Greetings,
Herenvardö