处理“警告:mysql_connect():连接过多”的最佳方法是什么?与小花3?

发布于 2024-10-03 08:22:22 字数 1192 浏览 5 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

哭了丶谁疼 2024-10-10 08:22:22

我刚刚创建了这样的文件来处理所有错误:

<?php

class Kohana extends Kohana_Core
{
  /**
   * Redirect to custom exception_handler
   */
  public static function exception_handler(Exception $e)
  {
    if (Kohana::DEVELOPMENT === Kohana::$environment)
    {
      // Pass to Kohana if we're in the development environment
      parent::exception_handler($e);
    }
    else
    {
      Kohana::$log->add(Kohana::ERROR, Kohana::exception_text($e));

      // Default route
      $route = Route::url('default', array('controller' => 'error', 'action' => '404'));

      // Error sub-request.
      echo Request::factory($route)
        ->execute()
        ->send_headers()
        ->response;
    }
  }
}

现在它只是一个草图,但它可以给你一些想法。

ps:我的bootstrap没有修改

I just created such file to handle all the errors:

<?php

class Kohana extends Kohana_Core
{
  /**
   * Redirect to custom exception_handler
   */
  public static function exception_handler(Exception $e)
  {
    if (Kohana::DEVELOPMENT === Kohana::$environment)
    {
      // Pass to Kohana if we're in the development environment
      parent::exception_handler($e);
    }
    else
    {
      Kohana::$log->add(Kohana::ERROR, Kohana::exception_text($e));

      // Default route
      $route = Route::url('default', array('controller' => 'error', 'action' => '404'));

      // Error sub-request.
      echo Request::factory($route)
        ->execute()
        ->send_headers()
        ->response;
    }
  }
}

For now it is just a sketch, but it could give you some ideas.

ps: my bootstrap is not modified

云裳 2024-10-10 08:22:22

/etc/my.cnf中的[mysqld]部分添加:

max_connections = 500

然后在MySQL中执行SET GLOBAL max_connections = 500;或重新启动MySQL 。

In /etc/my.cnf under the [mysqld] section add:

max_connections = 500

Then either executeSET GLOBAL max_connections = 500; in MySQL or restart MySQL.

拥有 2024-10-10 08:22:22

您可能需要首先检查 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文