views:

337

answers:

3

Apache/PHP/MySQL persistent connections have such a bad reputation because Apache handles each request as a child php process, each having 1 persistent connection. When visitors scale, MySQL reachs max connections limit from all the apache/php child processes, each with 1 persistent connection. There's also the issues of temporary tables, user variables, charsets, transactions and last-insert-id.

In my situation, we don't have to deal with the latter issues, because we are only READING from MySQL. There are no updates to the db: these are handled by another set of scripts on a different machine. The scripts we want to have persistent connections are the server-end of AJAX connections, returning JSON data.

Each pageview has 5 AJAX requests, so 6 different php child processes are started on the server for each page requested (5 ajax, 1 html). Ideally, I could have ONLY 1 connection from the php/ajax server to MySQL. This connection would be shared by all php child processes.

So, how can I do this? Should I use another webserver software other than apache? ngynx?

cheers

UPDATE: In this situation, the right way to connect to the MySQL server (http://bit.ly/15rBDP):

  • using mysql native driver (mysqlnd) and mysqli
  • each client reconnecting using the mysql-change-user function
  • using MYSQLI-NO-CHANGE-USER-ON-PCONNECT in php config ('coz we don't need to cleanup).

UPDATE 2:

To clarify my question, what i want to have is: ALL php client processes, connecting through only ONE persistent connection. This connection is defined, ran and stored some how (my question), but all new php client processes know it and can use it. The problem with apache/php is that each php client process has 1 connection. If I serve 20,000 pages per minute, there will be 20,000 persistent connections. I want the 20,000 php child processes connecting to 1 unique, central, persistent connection to mysql.

+1  A: 

Hi,

Read the accepted answer on this post : http://stackoverflow.com/questions/1340859/which-is-better-mysqlconnect-or-mysqlpconnect

Simply, using mysql persistent connections may be good or bad, depending on the hardware resources that you have as well as the way you code your applications.

yoda
thanks for the comment, but I'm not really asking if it's bad or not. I want to make it good. ;)
sopppas
then, what's the question? mysql_pconnect works just like mysql_connect ..
yoda
yes, it's dubious. I'm doubting Apache is the right webserver.
sopppas
well, deviantart.com use it, and it's a very busy server (if you know it) .. I think it suits your need.
yoda
deviantart.com uses what? ngynx? Wordpress.com uses ngynx too... i'm a fan of both sites ;)
sopppas
was talking about apache
yoda
+1  A: 

A php native mysql connector is included in php 5.3 which has improved support for persistent connections.

http://dev.mysql.com/downloads/connector/php-mysqlnd/

http://blog.ulf-wendel.de/?p=211

txyoji
+1  A: 

You do realize that having only one (persistent) connection for all your requests, effectively serializes all requests to your server. So request C has to wait for request B to finish, which has to wait for request A to finish etc.

So having one connection turns your multi-threaded/multi-process webserver into a single-threaded application.

mjax
No, I didn't realize. ;) But, comes to mind having 200 persistent connections always available and do some load-balancing between them. The AJAX requests are really just one SELECT statement each, so don't take too long. Queieng the requests over these 200 always-on connections? cheers
sopppas