This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
MySQL 应该不需要持久连接。 在其他数据库(例如 Oracle)中,建立连接既昂贵又耗时,因此如果您可以重复使用连接,那就是一个巨大的胜利。 但是那些品牌的数据库提供了连接池,这更好地解决了这个问题。
与其他品牌相比,连接到 MySQL 数据库的速度很快,因此使用持久连接为 MySQL 带来的好处比其他品牌的数据库要少。
持久连接也有缺点。 数据库服务器为每个连接分配资源,无论这些连接是否需要。 因此,如果连接空闲,您会看到大量资源无缘无故地被浪费。 我不知道你是否会达到 10,000 个空闲连接,但即使是几百个也是昂贵的。
连接具有状态,PHP 请求从先前由另一个 PHP 请求使用的会话中“继承”信息是不合适的。 例如,临时表和用户变量通常会在连接关闭时被清除,但如果您使用持久连接则不会。 同样基于会话的设置,如字符集和排序规则。 另外,
LAST_INSERT_ID()
会报告会话期间最后生成的 ID——即使是在之前的 PHP 请求期间。至少对于 MySQL 来说,持久连接的缺点可能大于其优点。 还有其他更好的技术来实现高可扩展性。
2014 年 3 月更新:
与其他品牌的 RDBMS 相比,MySQL 连接速度始终较低,但它正在变得更好。
请参阅http://mysqlserverteam.com/improving-connectdisconnect-performance/
请阅读博客了解更多详细信息和速度比较。
Persistent connections should be unnecessary for MySQL. In other databases (such as Oracle), making a connection is expensive and time-consuming, so if you can re-use a connection it's a big win. But those brands of database offer connection pooling, which solves the problem in a better way.
Making a connection to a MySQL database is quick compared to those other brands, so using persistent connections gives proportionally less benefit for MySQL than it would for another brand of database.
Persistent connections have a downside too. The database server allocates resources to each connection, whether the connections are needed or not. So you see a lot of wasted resources for no purpose if the connections are idle. I don't know if you'll reach 10,000 idle connections, but even a couple of hundred is costly.
Connections have state, and it would be inappropriate for a PHP request to "inherit" information from a session previously used by another PHP request. For example, temporary tables and user variables are normally cleaned up as a connection closes, but not if you use persistent connections. Likewise session-based settings like character set and collation. Also,
LAST_INSERT_ID()
would report the id last generated during the session -- even if that was during a prior PHP request.For MySQL at least, the downside of persistent connections probably outweighs their benefits. And there are other, better techniques to achieve high scalability.
Update March 2014:
MySQL connection speed was always low compared to other brands of RDBMS, but it's getting even better.
See http://mysqlserverteam.com/improving-connectdisconnect-performance/
Read the blog for more details and speed comparisons.
基本上,您必须平衡创建连接与保持连接的成本。 尽管 MySQL 建立新连接的速度非常快,但它仍然需要付出代价——线程设置时间以及 Web 服务器的 TCP/IP 设置时间。 这在流量足够高的网站上很明显。 不幸的是,PHP 对连接的持久性没有任何控制。 因此,答案是大幅降低 MySQL 中的空闲超时(例如降低到 20 秒),并增加线程缓存大小。 总之,这通常效果非常好。
另一方面,您的应用程序需要尊重连接的状态。 最好不要假设会话所处的状态。如果您使用临时表,那么使用 CREATE IF NOT EXISTS 和 TRUNCATE TABLE 会有很大帮助,对它们进行唯一命名(例如包含为 userid)也有很大帮助。 交易问题比较多; 但您的代码始终可以在顶部执行 ROLLBACK,以防万一。
Basically you have to balance the cost of creating connections versus keeping connections. Even though MySQL is very fast at setting up a new connection, it still costs -- in thread setup time, and in TCP/IP setup time from your web server. This is noticeable on a high-enough traffic site. Unfortunately, PHP does not have any controls on the persistence of connections. So the answer is to lower the idle timeout in MySQL a long way (like down to 20 seconds), and to up the thread cache size. Together, this generally works remarkably well.
On the flip side, your application needs to respect the state of the connection. It is best if it makes no assumptions about what state the session is in. If you use temporary tables, then using CREATE IF NOT EXISTS and TRUNCATE TABLE helps a lot, as does naming them uniquely (such as including as userid). Transactions are bit more problematic; but your code can always do ROLLBACK at the top, just in case.
mysql_connect()
和mysql_pconnect()
都适用于数据库连接,但差别不大。 在mysql_pconnect()
中,p
代表持久连接。当我们使用
mysql_connect()
函数时,每次它都会根据请求打开和关闭数据库连接。但在
mysql_pconnect()
函数的情况下:首先,在连接时,该函数将尝试查找已使用相同主机、用户名和密码打开的(持久)连接。 如果找到,将返回它的标识符,而不是打开新连接。
其次,当脚本执行结束时,与SQL服务器的连接不会关闭。 相反,连接将保持打开状态以供将来使用(
mysql_close()
不会关闭mysql_pconnect()
建立的连接)。当您的网站流量很大时,
mysql_pconncet()
非常有用。 此时,对于每个请求,它都不会打开连接,而是从池中获取连接。 这将提高您网站的效率。 但对于一般用途,mysql_connect() 是最好的。mysql_connect()
andmysql_pconnect()
both are working for database connection but with little difference. Inmysql_pconnect()
,p
stands for persistance connection.When we are using
mysql_connect()
function, every time it is opening and closing the database connection, depending on the request.But in case of
mysql_pconnect()
function:First, when connecting, the function would try to find a (persistent) connection 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 connection will remain open for future use (
mysql_close()
will not close connection established bymysql_pconnect()
).mysql_pconncet()
is useful when you have a lot of traffice on your site. At that time for every request it will not open a connection but will take it from the pool. This will increase the efficiency of your site. But for general use mysql_connect() is best.您不太可能达到 10000 个连接。 无论如何,请访问官方来源。 (强调我的)。
It's very unlikely that you'll reach 10000 connections. Anyhow, go to the official source. (Emphasis mine).
MYSQL_CONNECT()
1.mysql_connect可用于关闭连接。每次打开和关闭数据库连接时,根据请求而定。
2.这里在MYSQL connect中每次加载页面时都会打开数据库
3.页面加载时每次都会加载数据库
4.用于关闭连接
示例:
说明:
host:指定主机名或IP地址如本地主机。
mysql_user:指定MySQL用户名
mysql_password:指定MySQL密码
MYSQL_PCONNECT()
1.我们使用mysql_pconncet(),它最初尝试查找打开的持久连接。
2.mysql_pconncet()打开持久连接
3.mysql_pconnect()不支持关闭连接
4.mysql_pconnect()无法关闭连接。 这里打开与数据库的持久连接
5.这里数据库不需要每次都连接。
6.mysql_pconncet()中不需要每次都连接数据库。
更多详细信息:http://prittytimes.com/difference- Between-mysql_connect-and-mysql_pconnect /
MYSQL_CONNECT()
1.mysql_connect can be used to close the connection.Every time it is opening and closing the database connection, depending on the request .
2.Here database is opened everytime when the page is loaded in MYSQL connect
3.When the page is loaded, the database is loaded everytime
4.It is used to close the connection
Example:
Description:
host: Specifies a host name or an IP address like localhost.
mysql_user: Specifies the MySQL username
mysql_password: Specifies the MySQL password
MYSQL_PCONNECT()
1.We use the mysql_pconncet(), it initially tries to find an open persistent connection.
2.The mysql_pconncet() opens persistant connection
3.The mysql_pconnect() does not support the close connection
4.mysql_pconnect() cannot close the connection. Here open a persistant connection to the database
5.Here database need not be connected everytime.
6.The database need not be connected every time in mysql_pconncet().
more details:http://prittytimes.com/difference-between-mysql_connect-and-mysql_pconnect/