Help - Search - Members - Calendar
Full Version: Persistent MySQL Connections
Invision Power Services > Community Forums > Community Web Design and Coding
Sebastian Mares
Hello!

I was wondering if it is dangerous to use persistent MySQL connections on shared servers. Is it possible that somebody can "grab" my connection and use it himself? Any other things?

Regards,
Sebastian

PS: The title should read "Persistent MySQL Connections"
cojo
If PHP was built as a CGI program on your server, it should not pose a problem. Under this config, every time a page is requested an instance of the PHP interpreter is first created then destroyed. So, any connection made to the database is closed after each page request.

If PHP is built as a module, there is a danger of your script spawning a lot of children processes eating up server resources. Each database has a limited number of simulataneous connections. This is set by your host. Any request made above this limit won't connect. If your script happens to have a bug like an infinite loop or you get heavy traffic, the entire server will most likely crash. Under this config, I recommend you ask your host whether they prefer you use non-persistent or persistent connections. It's possible they may have set up their servers to handle persistent ones okay.

QUOTE
Is it possible that somebody can "grab" my connection and use it himself?

Only if he knows your database username and password.
Sebastian Mares
According to phpinfo(), "mysql.max_persistent" is set to "Unlimited".
princetontiger
You should always use a persistent connection. It uses an already opened connection, so you don't have to worry about opening another MySQL connection.
Brendon Koz
If you don't want to worry about opening another MySQL connection, use mysql_ping() and on failure, connect.
Rikki
QUOTE(Transverse Styles @ Jul 2 2005, 08:19 PM) *
You should always use a persistent connection. It uses an already opened connection, so you don't have to worry about opening another MySQL connection.


That's wrong.

You should only really use persistent connections if opening a connection on each page is particularly slow for some reason or has a big overhead.
princetontiger
http://www.php.net/manual/en/function.mysql-pconnect.php

QUOTE
First, when connecting, the function would first try to find a (persistent) link that's already open with the same host, username and password. If one is found, an identifier for it will be returned instead of opening a new connection.

Second, 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()).


So I'm wrong? It says right there, from the php.net manual, that it will use an already opened connection.

I may admit, that "always" is quite strong... and that a persistent connection will not work as a CGI wrapper.
Rikki
Where does it say 'always use pconnect'? All that does is tell you what purpose it serves.

http://uk2.php.net/manual/en/features.pers...connections.php

There it explains that they are good if there's a lot of overhead in creating connections. If there isn't, there's little point in using pconnect since the problems it can cause will probably outweigh the benefits. If there's no problem creating connections on your server you might just as well use mysql_connect.
Brendon Koz
...don't forget mysql_ping()! original.gif

It can slow down the connection on any first attempts (as it waits for a reply) but otherwise isn't all that bad. In the long run, it's your own choice and how you wrote the script. Since I wrote a script that incorporates something like 4 files in to one and can't necessarily be certain that one file won't open a NEW mysql connection after one is already open, I use ping in my own mysql connecting function so that I'm not wasting resources. It does take slightly longer since it is pinging, but considering how fast I try to get my pages to load anyway, I'm not too worried in this case. wink.gif
Wombat
There are numerous potential issues, especially on a shared server. If they haven't set an inactivity timeout for mysql connections then neither Apache nor MYSQL will ever close that connection.

Also, if a realistic upper limit has not been set on the number of mysql_pconnections PHP can open, during high-traffic periods it is possible for PHP to take up all available connection slots to the MySQL server with mysql_pconnect's. If this happens then no other connections will be permitted. Standard mysql_connect statements will fail. If combined with the other issue I mentioned the only solution left would be to restart the severs.

I agree with Rikki here. If, for example, MySQL was running on a different server to Apache/PHP then yes, you may see a good performance increase in using mysql_pconnect. But for most setups there isn't really much need... and on shoddy shared hosting environments, it can cause a number of problems.
Rikki
QUOTE(malikyte @ Jul 2 2005, 10:11 PM) *
...don't forget mysql_ping()! original.gif

It can slow down the connection on any first attempts (as it waits for a reply) but otherwise isn't all that bad. In the long run, it's your own choice and how you wrote the script. Since I wrote a script that incorporates something like 4 files in to one and can't necessarily be certain that one file won't open a NEW mysql connection after one is already open, I use ping in my own mysql connecting function so that I'm not wasting resources. It does take slightly longer since it is pinging, but considering how fast I try to get my pages to load anyway, I'm not too worried in this case. wink.gif


A script that 'accidently' opens new connections (which aren't required) when one already exists in the same execution is poorly written IMO.

When we're talking about connections that continue we mean between server processes, not between files or a single page load of a php script.
Wombat
I *THINK* (but am not sure) that if you try to open an identical connection to one that is already open (i.e. same username, password, db) then PHP will just return the reference to the original connection resource.

Not sure whether this is default behavior, or a configurable option, but it is behavior I have noticed in the past. ermm.gif unsure.gif
Brendon Koz
QUOTE(Rikki @ Jul 2 2005, 02:39 PM) *
A script that 'accidently' opens new connections (which aren't required) when one already exists in the same execution is poorly written IMO.

When we're talking about connections that continue we mean between server processes, not between files or a single page load of a php script.



sad.gif Yes, perhaps...that's the problem with scope creep, I suppose. I can only plan so far ahead, but then the client wants this, that, and the other thing which weren't even CLOSE to the original specifications. Rather than replan and rewrite from scratch, I ended up trying to add modules that didn't really act, ummm...modular. Had I created a session for ALL visitors and required the database on ALL pages, then the problem would be gone. Unfortunately, that isn't the case...but at each new stumble in the road, I learn from past experiences. It might be poorly adapted/patched together, but I am glad there is a function such as mysql_ping so I can avoid recreating a new connection.

Wombat, I believe that's a configuration option and I would have no idea where it could be set.
Sebastian Mares
If I use sessions, I can create a connection on session start and destroy it on session end, correct?
princetontiger
You're going to have to create some type of connection every time you view a page.

Starting with PHP 4's Zend Engine, you don't have to destroy resources unless you are using mysqlp_connection()... which in that case you shouldn't.
Brendon Koz
Sebastian, I fail to understand your question about sessions and connections... They are not interconnected, so please be more specific.

Transverse Styles: You can't destroy a persistent connection, it can only time out.
Sebastian Mares
I thought that when using PHP sessions, I don't have to use persistent connections, but simply create a MySQL connection which I carry over to the other pages.
Wombat
QUOTE(Sebastian Mares @ Jul 3 2005, 12:01 AM) *
I thought that when using PHP sessions, I don't have to use persistent connections, but simply create a MySQL connection which I carry over to the other pages.


No, it won't work as mysql_connect returns a *reference* to a connection resource. Not sure how PHP handles references when they are stored in a session, but somehow I expect they wont work.
Brendon Koz
A session is basically a server-side cookie. It can hold information that you give to it, but that is all. I suppose you could give it the resource ID of the mysql connection, but unfortunately (as far as I'm aware) you can't reopen a specific resource ID using PHP's mysql functions.

I hope that clarifies a little bit for you.
Sebastian Mares
OK, thanks. So the only possiblity would be either reopening the MySQL connection on every page load or using persistent MySQL connections which I would rather not do.
Brendon Koz
That is correct.
Rikki
I hope this topic hasn't confused people biggrin.gif

Opening a MySQL connection on every page is fine under most circumstances. IPB for example does it (by default) as does virtually every other script. It's only if your server setup means you need to keep a persistant connection that you need to use mysql_pconnect. That still requires you to call mysql_pconnect on every page you want to use MySQL in but behind the scenes, Apache will reuse a connection.
Michael_C
QUOTE(Wombat @ Jul 3 2005, 09:54 AM) *
I *THINK* (but am not sure) that if you try to open an identical connection to one that is already open (i.e. same username, password, db) then PHP will just return the reference to the original connection resource.

Not sure whether this is default behavior, or a configurable option, but it is behavior I have noticed in the past. ermm.gif unsure.gif

Correct, from the manual(new_link is the fourth parameter for mysql_connect):
QUOTE
new_link

If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned. The new_link parameter modifies this behavior and makes mysql_connect() always open a new link, even if mysql_connect() was called before with the same parameters.
Brendon Koz
...so in effect, me using mysql_ping is completely useless? $%#@^%@#%$#

Thanks, Michael.
Michael_C
Yes, it seems that way. That behaviour can be quite annoying at times though, I spent a couple of hours trying to work out why both connections had the same resource ID when writing a forum convertor script a while ago.
Brendon Koz
That kind of makes me wonder what the purpose of mysql_ping() is now...seems redundant, and therefore bloats the codebase of PHP (although it technically is bloated anyway for convenience).
Delphi
I recently did work for a large website and we decided to bring their mySQL server to a seperate dedicated server. Their site was also run off of a dedicated server. Since mySQL connections had to go across the LAN, the slowed down a bit. Using mySQL pconnect helped in this case as it reduced the connection time.

In some cases it can be useful original.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.