pconnect is preferred when you are using a remote database server on a major web site. mysql in particular stays happier with 1 open connection as opposed to 1000 connections a minute =)
![]() | mysql_pconnect说明resource mysql_pconnect ( [string server [, string username [, string password [, int client_flags]]]] )如果成功则返回一个正的 MySQL 持久连接标识符,出错则返回 FALSE。 mysql_pconnect() 建立一个到 MySQL 服务器的连接。如果没有提供可选参数,则使用如下默认值:server = 'localhost:3306',username = 服务器进程所有者的用户名,password = 空密码。client_flags 参数可以是以下常量的组合:MYSQL_CLIENT_COMPRESS,MYSQL_CLIENT_IGNORE_SPACE 或者 MYSQL_CLIENT_INTERACTIVE。 server 参数也可以包括端口号,例如 "hostname:port",或者是本机套接字的的路径,例如 ":/path/to/socket"。
mysql_pconnect() 和 mysql_connect() 非常相似,但有两个主要区别。 首先,当连接的时候本函数将先尝试寻找一个在同一个主机上用同样的用户名和密码已经打开的(持久)连接,如果找到,则返回此连接标识而不打开新连接。 其次,当脚本执行完毕后到 SQL 服务器的连接不会被关闭,此连接将保持打开以备以后使用(mysql_close() 不会关闭由 mysql_pconnect() 建立的连接)。 可选参数 client_flags 自 PHP 4.3.0 版起可用。 此种连接称为“持久的”。
![]()
3ch0
21-May-2006 05:01
pconnect is preferred when you are using a remote database server on a major web site. mysql in particular stays happier with 1 open connection as opposed to 1000 connections a minute =)
Ron
15-Sep-2005 11:34
Here's a nifty little class which will load balance across multiple replicated MySQL server instances, using persistent connections, automatically removing failed MySQL servers from the pool.
tomek at 3kropki dot pl
30-Dec-2004 01:36
Instead of use wait_timeout, you can set interactive_timeout to short period of time (for ex. 20 sec.) this is a lot better solution in apache + mysql environment than wait_timeout.
federico dot chiesa at tiscali dot it
20-Nov-2004 09:08
Be very careful when using persistent connections and temporary tables on MySQL: in MySQL, temporary tables are visible only to the currenct connection, but if you have a persistent connection the temporary tables will supposedly be visible to everybody sharing the same persistent connection. This can lead to major trouble. I suggest to use totally random temporary table names when using persistent connections to avoid major problems.
markunderscoreconnollyatacmdotorg
28-Oct-2004 01:59
I would like to comment on the post from dfischer at qualcomm dot com that proposes spanning transactions over multiple application invocations, in case someone is bold enough to try it.
dfischer at qualcomm dot com
07-Jul-2004 08:57
You also may consider using pconnect if you have transactions that span multiple pages. For example, in applications that I develop, I start a transaction on the moment I query selecting the data that the user plans on editing. I then commit the transactions after the user hits the submit button and the data is committed.
matt *at* roughest *dot* net
11-Mar-2004 12:18
Be very careful when using persistent connections on a machine running multiple mysql servers. You must specify the correct socket path, otherwise PHP will reuse connections irregardless of what server they are connected to. That is, it will see an open connection with matching parameters and use it, even if the connection is actually for a different server.
mightye (at) mightye (dot) org
04-Feb-2004 08: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.
aaryal at foresightint dot com
16-Jan-2004 07:25
don't use pconnect in a situation with MySQL running on one host but on multiple ports (a multiple database configuration). the connection pooling algo in php apparently only keys off of the host, username and password but not the port. therefore, if you use the same host, username and password but a different port, you might get a connection that is connected to a different port than the one you asked for.
spam at vrana dot cz
05-Nov-2003 02:53
Be warned if you use different parameters for mysql_pconnect() in different scripts on server: PHP can create single persistent connection for every set of parameters in each process up to mysql.max_persistent (PHP directive) per process. So even if you have MaxClients Apache directive set lesser then max_connections MySQL directive, you can easily get Too many connections MySQL error.
script_this at yahoo dot com
06-Aug-2003 02:41
in response to uthayakutty76 at yahoo dot com's
pulstar at mail dot com
19-Jul-2003 02:13
I had some problems when connecting to a remote server with mysql_pconnect and using the flag MYSQL_CLIENT_COMPRESS. Sometimes it connect, but many times it give me the error:
rochlin at centralpets dot com
13-Jul-2003 11:27
Do not use transactions (e.g. with InnoDB MySQL tables) with persistent connections. If your script stops or exits for any reason, your transaction will be left open and your locks will be left on. You have to reset MySQL to release them (so far as I can figure). They won't ROLLBACK automatically on error, like they ought to. When you restart the script, you'll get a new connection, so you can't rollback or commit for the previous script. Any script with a start transaction, rollback, or commit SQL statement should use regular (not persistent) connections. Seems like PHP ought to automatically issue a ROLLBACK on any open transactions when a script exits (error or otherwise) without a COMMIT. ZEND's site has a brief blurb on this. It's OK to mix/match so you use a persistent connection for the read stuff, but open a new regular connection conditionally (if you have to update, which is usually less often).
rudenko at id.com.ua
09-Apr-2003 10:15
You can solve problem with persistent connections setting directive mysql.allow_persistent = Off in php configuration file. The users whitch will try to create persistant connetion /mysql_pconnect()/ will be connected to db
amn -at- frognet.net
12-Mar-2003 02:45
Be careful using mysql_pconnect. If you are hosting on an ISP, they may frown upon you using multiple persistant mysql connections as this consumes resources for a longer period of time. If your script crashes, your connection can stay open for long periods of time. If there is a loop involved, you could accidently eat up all the available connections. That might be considered abuse by an ISP and you could get in trouble. Try using mysql_connect first instead. 90% of the time, a non-persistant mysql_connect call will do the trick just fine.
fate_amendable_to_changeNOSPAM at yahoo dot com
30-Oct-2002 07:58
To tell mysql_pconnect to connect to mysql on a port other than the default use a colon - eg
david at acz dot org
24-Apr-2002 02:41
You need to be VERY careful when using LOCK TABLES with persistent connections. If the script terminates before the UNLOCK TABLES is executed, the the table(s) will stay locked, and very likely hang future scripts. This is noted in a bug report, but is still not reflected in the documentation: http://bugs.php.net/bug.php?id=7634
bartek at bulzak dot com
27-Feb-2002 06:37
PHP 4.1.1 running with Apache under Linux doesn't seem to be doing all necessary flushing when handling persistant mysql connections. Try it out for yourself. Create a temporary table in a pconnect session, add rows (non unique), select/display and drop the table. Now reload your script multiple times, you will see that your results are not consistent, even though you are creating a new table everytime and dropping it..
aramos at lander dot es
08-Mar-2001 02:15
If your MySQL recive this error log: "Got an error reading communication packets" see this post: http://marc.theaimsgroup.com/?l=php-dev&m=97858932431876&w=2. This patch work property in 4.0.4pl1 (when httpd.conf have MaxRequestsPerChild non-cero)
aramos at lander dot es
07-Mar-2001 07:52
if you set wait_timeout, and you have high number of connection closed, you need too, set max_connect_errors (in MySQL) http://www.mysql.com/doc/en/Blocked_host.html
sasha at mysql dot com
28-Apr-2000 12:43
If you need to close idle persistent connections, set a low wait_timeout in MySQL
| ![]() | ||