阿权的一个数据库有很多进程都是sleep的,这个是openads的网站,管理的是网页的广告,估计有200个,多的时候会更多些,我也在想这个情况到底是否正常呢?
mysql_pconnect()中的p,就是单词persistent(永久的)的首字母。要理解是否正常,那就得看看 mysql_pconnect 是如何工作的了,看看官方文档:
也就是说,会建立一个对数据库的连接,会先查找是否还有空闲的同样host、username、password的连接,没有就新建一个,否则就用这个空闲的连接。
这么理解应该挺好知道为什么会有一些进程是sleep的,就是说之前并发请求有100个,那就会有100个持续连接建立了,并且把连接保存在连接池中,如果连接有效时间过了,连接会自动完结了吧,这样可能就在访问压力低的时候sleep的进程变少了。
如果连接数可以足够,pconnect是不错的选择,mysql启动参数里面可以设定最大连接数的(参考《Mysql的启动参数 》http://www.aslibra.com/blog/read.php?1037),参考一下:
所以空闲进程多是不怕的,就担心影响别的应用,如果连接数足够,那还是用pconnect比connect方式要好
原创内容如转载请注明:来自 阿权的书房
mysql_pconnect()中的p,就是单词persistent(永久的)的首字母。要理解是否正常,那就得看看 mysql_pconnect 是如何工作的了,看看官方文档:
引用
resource mysql_pconnect ([ string $server [, string $username [, string $password [, int $client_flags ]]]] )
Establishes a persistent connection to a MySQL server.
mysql_pconnect() acts very much like mysql_connect() with two major differences.
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()).
This type of link is therefore called 'persistent'.
Establishes a persistent connection to a MySQL server.
mysql_pconnect() acts very much like mysql_connect() with two major differences.
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()).
This type of link is therefore called 'persistent'.
也就是说,会建立一个对数据库的连接,会先查找是否还有空闲的同样host、username、password的连接,没有就新建一个,否则就用这个空闲的连接。
这么理解应该挺好知道为什么会有一些进程是sleep的,就是说之前并发请求有100个,那就会有100个持续连接建立了,并且把连接保存在连接池中,如果连接有效时间过了,连接会自动完结了吧,这样可能就在访问压力低的时候sleep的进程变少了。
如果连接数可以足够,pconnect是不错的选择,mysql启动参数里面可以设定最大连接数的(参考《Mysql的启动参数 》http://www.aslibra.com/blog/read.php?1037),参考一下:
max_connect_errors=100000
max_connections=1024
wait_timeout=600
max_connections=1024
wait_timeout=600
所以空闲进程多是不怕的,就担心影响别的应用,如果连接数足够,那还是用pconnect比connect方式要好
引用
pconnect的优点是速度,重用一个现有的连接几乎不花费时间。
缺点是效率。因为服务器(比如apache)可以使用任意一个进程迎接新的页面请求,
所以一段时间之后,可能每个进程都存了一个或几个连接,而且不同进程之间连接不能共享。
最坏的情况是其他进程保持着大量空闲连接,而某个进程却因为数据库连接数达到上限却无法取得连接
缺点是效率。因为服务器(比如apache)可以使用任意一个进程迎接新的页面请求,
所以一段时间之后,可能每个进程都存了一个或几个连接,而且不同进程之间连接不能共享。
最坏的情况是其他进程保持着大量空闲连接,而某个进程却因为数据库连接数达到上限却无法取得连接
原创内容如转载请注明:来自 阿权的书房
收藏本文到网摘
十个习惯让你精通新的开发技术
0512:北京感觉到地震
