2013-08-28 64 views
9

我工作的Magento的網站,我得到這個錯誤:SQLSTATE [HY000]:常規錯誤:2006年MySQL服務器已經走了上運行cron作業的Magento

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

我只是有時會出現此錯誤。

<?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); 
    } 
} 

此代碼工作幾個小時,然後它出現此錯誤。錯誤是什麼意思?

回答

25

如果您嘗試在打開連接後某個時間發送查詢,DB連接會有一個超時,這將導致此錯誤。通常的情形是:

  • 開放數據庫連接
  • 從DB
  • 做的東西,例如獲取一些數據發送電子郵件(需要時間長於DB連接超時)使用相同的連接
  • 查詢DB
  • 錯誤:MySQL服務器已消失

那麼 - 有什麼解決辦法?您可以簡單地增加超時時間,但這很難看,並且可能會導致網站流量增加時出現問題。最好的解決辦法是關閉數據庫連接,然後重新打開它,像這樣:

  • 開放數據庫連接
  • 從DB
  • 關閉數據庫連接
  • 做的東西,例如獲取一些數據發送電子郵件使用相同的連接
  • 打開新數據庫連接
  • 查詢DB(需要時間長於DB連接超時)
  • 關閉數據庫連接

下面是詳細信息: http://dev.mysql.com/doc/refman/5.0/en/gone-away.html

0

如果你用phpsh解釋器得到了這個錯誤。我可以重現這個錯誤與phpsh和一個新的外殼教條管理器。

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

隨着在phpsh解釋這個命令:

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

說明:

此錯誤是MySQL超時錯誤。無論是在創建連接和實際使用連接之間等待時間太長,或者您在使用某個命令時犯了錯誤,並且破壞了連接。最簡單的解決方案是停止,重新啓動所有內容,不要運行引發錯誤的命令,並快速執行。它應該工作。

解決方案

重新啓動解釋。不要提交錯誤,並通過口譯員發佈命令的速度更快。

您可以增加PHP的MySQL連接的超時長度。然後,您可以在創建連接之間等待更長時間,然後再使用它。

0

我沒有任何超時問題。

我從我的主文件移動了一個fclose(STDERR)行到一個包含文件,這開始發生。

SQLSTATE [HY000]:常規錯誤:2006年MySQL服務器已消失

我感動行回到原來的地方,問題就走了。

基本上,從包含文件關閉STDERR似乎有一些瘋狂的影響。

0

您也可以在使用共享主機時查看索引表大小。這可能需要很多空間,因爲這也可能導致「mysql服務器消失」。

0

我以前遇到過這個錯誤。對於我而言,這是因爲數據庫規模太大,5年數據超過18GB。

對我來說唯一的解決方案是轉儲所有這些數據並創建一個新的數據庫。

0

如果您有任何不能在Magento數據庫中運行超過20秒的操作(我遇到了使用wait_timeout = 20的共享主機),您必須關閉數據庫連接。 Magento將在下次調用數據庫時創建新的連接。

Mage::getSingleton('core/resource')->getConnection('read')->closeConnection(); 
相關問題