SQLSTATE[HY000]:一般错误:2006 MySQL 服务器在运行 cron 作业 magento 时已经消失

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18486023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 18:41:28  来源:igfitidea点击:

SQLSTATE[HY000]: General error: 2006 MySQL server has gone away on running cron job magento

mysqlmagentocronobservers

提问by Mahmood Rehman

I am working on Magento site and I get this error:

我正在 Magento 网站上工作,但出现此错误:

SQLSTATE[HY000]: General error: 2006 MySQL server has gone away on running 
cron job magento

I only get this error sometimes.

我有时只会收到此错误。

<?php
class Namespace_Module_Model_Observer 
{
  public function importemails(Varien_Event_Observer $observer)
  {
    echo "Hi Dear";exit();

    /* connect to gmail */
    $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
    $username = '[email protected]';
    $password = 'mypass';

    /* try to connect */
    $inbox = imap_open($hostname,$username,$password) 
        or die('Cannot connect to Gmail: ' . imap_last_error());

    /* grab emails */
    $emails = imap_search($inbox,'ALL');

    /* if emails are returned, cycle through each... */
    if($emails) {

      /* begin output var */
      $output = '';

      /* put the newest emails on top */
      rsort($emails);

      /* for every email... */
      foreach($emails as $email_number) {

        /* get information specific to this email */
        $overview = imap_fetch_overview($inbox,$email_number,0);
        $message = imap_fetchbody($inbox,$email_number,2);

        /* output the email header information */
        $output.= 
          '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">';
        $output.= '<span class="subject">'.$overview[0]->subject.'</span> ';
        $output.= '<span class="from">'.$overview[0]->from.'</span>';
        $output.= '<span class="date">on '.$overview[0]->date.'</span>';
        $output.= '</div>';

        /* output the email body */
        $output.= '<div class="body">'.$message.'</div>';
      }
      echo $output;
    } 

    /* close the connection */
    imap_close($inbox);
  }  
}

This code works for several hours then it gives this error. What does the error mean?

这段代码工作了几个小时,然后它给出了这个错误。错误是什么意思?

回答by SlappyTheFish

DB Connections have a timeout which will cause this error if you try to send a query sometime after opening the connection. The usual scenario is:

数据库连接有一个超时,如果您尝试在打开连接后的某个时间发送查询,则会导致此错误。通常的场景是:

  • Open DB connection
  • Fetch some data from DB
  • Do stuff, e.g. send emails (takes time longer than DB connection timeout)
  • Query DB using same connection
  • Error: MySQL server has gone away
  • 打开数据库连接
  • 从数据库中获取一些数据
  • 做一些事情,例如发送电子邮件(比数据库连接超时花费的时间更长)
  • 使用相同的连接查询数据库
  • 错误:MySQL 服务器已消失

So - what's the solution? You could simply increase the timeout, but that's ugly and could cause problems when traffic to your site increases. The best solution would be to close your DB connection and then re-open it like this:

那么 - 解决方案是什么?您可以简单地增加超时时间,但这很丑陋,并且在您网站的流量增加时可能会导致问题。最好的解决方案是关闭您的数据库连接,然后像这样重新打开它:

  • Open DB connection
  • Fetch some data from DB
  • Close DB connection
  • Do stuff, e.g. send emails (takes time longer than DB connection timeout)
  • Open new DB connection
  • Query DB using same connection
  • Close DB connection
  • 打开数据库连接
  • 从数据库中获取一些数据
  • 关闭数据库连接
  • 做一些事情,例如发送电子邮件(比数据库连接超时花费的时间更长)
  • 打开新的数据库连接
  • 使用相同的连接查询数据库
  • 关闭数据库连接

Here's more information: http://dev.mysql.com/doc/refman/5.0/en/gone-away.html

这里有更多信息:http: //dev.mysql.com/doc/refman/5.0/en/gone-away.html

回答by Eric Leschinski

If you got this error with the phpsh interpreter.I am able to reproduce this error with phpsh and a new shell to doctrine manager.

如果您在使用 phpsh 解释器时遇到此错误。我可以使用 phpsh 和一个新的 shell 来重现这个错误。

SQLSTATE[HY000]: General error: 2006 MySQL server has gone away 

With this command in the phpsh interpreter:

在 phpsh 解释器中使用此命令:

php> $result = $conn->query('select psetid from psetproblems')->fetchAll();

Explanation:

解释:

This error is the MySQL timeout error. Either you waited too long in between creating your connection and then actually using it, or you made an error with one of your commands and you ruined the connection. The simplest solution is to stop, restart everything and don't run the command that throws an error, and do it quickly. It should work.

此错误是 MySQL 超时错误。您在创建连接和实际使用它之间等待的时间太长,或者您的命令之一出错并破坏了连接。最简单的解决方案是停止,重新启动一切,不要运行抛出错误的命令,并快速执行。它应该工作。

Solution

解决方案

Restart your interpreter. Don't submit errors and be faster in issuing your commands through your interpreter.

重新启动您的解释器。不要提交错误,并通过解释器更快地发出命令。

You could increase the timeout length of your MySQL connection for PHP. Then you can wait longer between creating a connection, then using it.

您可以为 PHP 增加 MySQL 连接的超时长度。然后您可以在创建连接和使用它之间等待更长时间。

回答by Peter Smith

I don't have any timeout issues.

我没有任何超时问题。

I moved a fclose(STDERR) line from my main file to a included file and this started happening.

我将 fclose(STDERR) 行从主文件移动到包含文件,这开始发生。

SQLSTATE[HY000]: General error: 2006 MySQL server has gone away

SQLSTATE[HY000]:一般错误:2006 MySQL 服务器已经消失

I moved the line back to its original place and problem went away.

我将线路移回原来的位置,问题就消失了。

Basically, closing STDERR from an included file seems to have some crazy repercussions.

基本上,从包含的文件中关闭 STDERR 似乎会产生一些疯狂的影响。

回答by Jenish Patel

You may also look into index table size when using shared hosting. It may take much space because of that also you may get "mysql server gone away".

使用共享主机时,您还可以查看索引表大小。它可能会占用很多空间,因此您也可能会得到“mysql 服务器消失”。

回答by Fadli Saad

I've encountered this error before. For my case it was due the database size is too large, more than 18gb for 5 years data.

我以前遇到过这个错误。就我而言,这是由于数据库大小太大,5 年数据超过 18GB。

The only solution that is working for me was to dump all those data and create a new database.

对我有用的唯一解决方案是转储所有这些数据并创建一个新数据库。

回答by Rostislav Sabischenko

If you have any actions which do not operate with Magento DB for more than 20 seconds (I met shared hosting with such wait_timeout=20), you have to close DB connection. Magento will create new connection on next call to DB.

如果您有任何操作在 Magento DB 上运行超过 20 秒(我遇到过这样的共享主机,wait_timeout=20),您必须关闭 DB 连接。Magento 将在下次调用 DB 时创建新连接。

    Mage::getSingleton('core/resource')->getConnection('read')->closeConnection();