SQLSTATE [HY000]:一般错误:2006 MySQL服务器已经离开运行cron job magento

时间:2022-10-14 13:16:05

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 = 'myid@gmail.com';
    $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?

此代码可以工作几个小时然后就会出现此错误。错误是什么意思?

6 个解决方案

#1


25  

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
  • 从DB中获取一些数据
  • Do stuff, e.g. send emails (takes time longer than DB connection timeout)
  • 做的事情,例如发送电子邮件(花费的时间比数据库连接超时)
  • Query DB using same connection
  • 使用相同的连接查询DB
  • 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
  • 从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
  • 使用相同的连接查询DB
  • 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

#2


0  

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来重现这个错误到doctrine manager。

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连接的超时长度。然后,您可以在创建连接之间等待更长时间,然后再使用它。

#3


0  

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似乎有一些疯狂的反响。

#4


0  

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服务器消失”。

#5


0  

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.

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

#6


0  

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的共享主机),您必须关闭数据库连接。 Magento将在下次调用DB时创建新连接。

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

#1


25  

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
  • 从DB中获取一些数据
  • Do stuff, e.g. send emails (takes time longer than DB connection timeout)
  • 做的事情,例如发送电子邮件(花费的时间比数据库连接超时)
  • Query DB using same connection
  • 使用相同的连接查询DB
  • 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
  • 从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
  • 使用相同的连接查询DB
  • 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

#2


0  

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来重现这个错误到doctrine manager。

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连接的超时长度。然后,您可以在创建连接之间等待更长时间,然后再使用它。

#3


0  

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似乎有一些疯狂的反响。

#4


0  

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服务器消失”。

#5


0  

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.

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

#6


0  

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的共享主机),您必须关闭数据库连接。 Magento将在下次调用DB时创建新连接。

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