简体   繁体   English

如何解决mysql错误中的连接过多?

[英]how to solve Too many connections in mysql error?

I have a php application.我有一个 php 应用程序。 There is a lot of database operation and i do mysql_connect() to open connection.有很多数据库操作,我做 mysql_connect() 来打开连接。 Do I have to close the connection manually?我必须手动关闭连接吗?

In general, after script execution the connections should close automatically.一般来说,脚本执行后,连接应该会自动关闭。 However in some cases (eg like yours) you have no other way but to do it manually (make mysql_close($connection) to be the last line of your script).然而,在某些情况下(例如像你的情况),你别无他法,只能手动进行(使mysql_close($connection)成为脚本的最后一行)。

Alternatively, consider using mysql_pconnect() to have persistent connection, which doesn't close after script completion, and is (implicitly) reused on the next request.或者,考虑使用mysql_pconnect()具有持久连接,在脚本完成后不会关闭,并且(隐式)在下一个请求中重用。

Yes.是的。 But you do mysql_connect() many times.但是您多次执行mysql_connect() you can keep it in seperate file and include where ever needed.您可以将其保存在单独的文件中并包含在需要的地方。

Increase the number of allowable connection in mysql settings should do the trick i guess.我猜想增加 mysql 设置中允许的连接数应该可以解决问题。 But before that make sure on each page you are only opening the mysql connection once.但在此之前,请确保在每个页面上您只打开一次 mysql 连接。 At the end of the script the connection should automatically be closed, so no you dont really have to close it manually.在脚本结束时,连接应自动关闭,因此您不必手动关闭它。

Use singleton to keep just one open connection on each page.使用 singleton 在每一页上只保留一个打开的连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM