简体   繁体   English

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

[英]What is the best way to handle “Warning: mysql_connect(): Too many connections” with Kohana 3?

I've got a web application that is used by a company for logging their employees' work. 我有一个Web应用程序,公司可以使用它来记录员工的工作。

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 警告:mysql_connect()[function.mysql-connect]:连接太多

Which then lets further errors cascade... like errors with mysql_select_db() , mysql_error() , mysql_errnon() and finally the uncaught Database_Eexception . 然后让更多错误级联...像mysql_select_db()mysql_error()mysql_errnon()以及最后未捕获的Database_Eexception

When I run my main request, I wrap it in a try and capture any exception and display a not found page. 运行主请求时,我将其包装起来并try捕获任何异常并显示未找到的页面。 This is because usually my controllers throw exceptions if a resource is not found (though the route may be valid) eg http://example.com/products/30 is a valid route, but product #30 doesn't exist. 这是因为通常,如果找不到资源(虽然路由可能有效),例如, http://example.com/products/30 : http://example.com/products/30是有效路由,但产品#30不存在,我的控制器就会引发异常。

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. 理想情况下,我想分别捕获该异常,然后显示一个漂亮的页面,通知员工在5分钟内重试。

The code that runs my main request in application/bootstrap.php looks like this... 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;

Thanks for any help! 谢谢你的帮助!

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. 您可能首先需要检查一下MySQL系统属性max_connections当前设置为什么,并查看如何比较使用要求。 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. 也许调查连接池。

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 ps:我的引导程序未修改

In /etc/my.cnf under the [mysqld] section add: [mysqld]部分下的/etc/my.cnf ,添加:

max_connections = 500

Then either execute SET GLOBAL max_connections = 500; 然后执行SET GLOBAL max_connections = 500; in MySQL or restart MySQL. 在MySQL中或重新启动MySQL。

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

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