Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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?

like image 888
Mahmood Rehman Avatar asked Aug 28 '13 10:08

Mahmood Rehman


1 Answers

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

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

like image 141
SlappyTheFish Avatar answered Oct 02 '22 21:10

SlappyTheFish