处理“警告:mysql_connect():连接过多”的最佳方法是什么?与小花3?
我有一个 Web 应用程序,一家公司使用它来记录员工的工作。
很多人经常同时登录。
该应用程序在共享主机上运行。
我有时会收到...
警告:mysql_connect() [function.mysql-connect]:连接过多
这会导致进一步的错误级联...例如 mysql_select_db()
、mysql_error()
的错误, mysql_errnon()
以及最后未捕获的 Database_Eexception
。
当我运行主请求时,我将其包装在 try
中并捕获任何异常并显示未找到页面。这是因为如果找不到资源(尽管路由可能有效),通常我的控制器会抛出异常,例如 http://example.com/products/30
是有效路由,但产品 #30不存在。
处理过多连接的最佳方法是什么?理想情况下,我想单独捕获该异常,然后显示一个漂亮的页面,通知员工在 5 分钟后重试。
在 application/bootstrap.php
中运行我的主要请求的代码如下所示...
$request = Request::instance();
try {
$request->execute();
} catch (Exception $e) {
if (Kohana::$environment === Kohana::DEVELOPMENT) throw $e;
// Log the error
Kohana::$log->add(Kohana::ERROR, Kohana::exception_text($e));
// Create a 404 response
$request->status = 404;
$request->response = Request::factory(Route::get('catch_all')->uri(array('path' => 'errors/404')))->execute();
}
$request->send_headers();
echo $request->response;
感谢您的帮助!
I've got a web application that is used by a company for logging their employees' work.
A lot of people are often logged in at once.
The application runs on a shared host.
I sometimes receive...
Warning: mysql_connect() [function.mysql-connect]: Too many connections
Which then lets further errors cascade... like errors with mysql_select_db()
, mysql_error()
, mysql_errnon()
and finally the uncaught Database_Eexception
.
When I run my main request, I wrap it in a try
and capture any exception and display a not found page. This is because usually my controllers throw exceptions if a resource is not found (though the route may be valid) e.g. http://example.com/products/30
is a valid route, but product #30 doesn't exist.
What is the best way to handle the too many connections? Ideally I'd like to capture that exception separately, then display a nice page that informs the employee to try again in 5 minutes.
The code that runs my main request in application/bootstrap.php
looks like this...
$request = Request::instance();
try {
$request->execute();
} catch (Exception $e) {
if (Kohana::$environment === Kohana::DEVELOPMENT) throw $e;
// Log the error
Kohana::$log->add(Kohana::ERROR, Kohana::exception_text($e));
// Create a 404 response
$request->status = 404;
$request->response = Request::factory(Route::get('catch_all')->uri(array('path' => 'errors/404')))->execute();
}
$request->send_headers();
echo $request->response;
Thanks for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我刚刚创建了这样的文件来处理所有错误:
现在它只是一个草图,但它可以给你一些想法。
ps:我的bootstrap没有修改
I just created such file to handle all the errors:
For now it is just a sketch, but it could give you some ideas.
ps: my bootstrap is not modified
在
/etc/my.cnf
中的[mysqld]
部分添加:然后在MySQL中执行
SET GLOBAL max_connections = 500;
或重新启动MySQL 。In
/etc/my.cnf
under the[mysqld]
section add:Then either execute
SET GLOBAL max_connections = 500;
in MySQL or restart MySQL.您可能需要首先检查 MySQL 系统属性 max_connections 当前设置的值,并了解其与使用要求的比较。如果您不能很好地处理使用要求,那么您可能会做得比使用代码来记录有关同时连接的数据更糟糕;或者使用实时数据库分析工具来监控这一点。
您还可以查看您的代码是否占用连接时间太长(即不必要的)并纠正它。也许研究一下连接池。
You might want to begin by checking to see what your MySQL system property max_connections is currently set to, and see how that compares usage requirements. If you don't have a good handle on usage requirements then you could do worse than instrument your code to log data about simultaneous connections; or your a live database profiling tool to monitor this.
You could also look to see if your code is hogging connections for too long (ie. unnecessarily) and correct this. Perhaps investigate connection pooling.