2011-09-01 14 views
3

我希望有人在這裏可以幫助解決我在網絡服務器上遇到的問題。目前,該服務器託管着我們爲求助服務而購買的Web應用程序。當有人瀏覽到他們將與此錯誤呈現的頁面,每8小時:[警告]:PDO :: __構造():MySQL服務器已經消失 - wait_timeout不是原因?

[Warning]: PDO::__construct(): MySQL server has gone away (Database/class.SWIFT_Database.php:334) 

我已經嘗試設置WAIT_TIMEOUT在/etc/my.cnf文件裏即最大值;

wait_timeout=31536000 

另外從MySQL我已經設置了全局的wait_timeout到這個值,並設置會話wait_time出來相同。

我注意到一些奇怪的行爲,當你重新啓動mysql服務時,儘管設置在my.cnf中,等待超時重置爲28800默認值。我找不到任何其他的mysql配置文件,但很高興看看有人能指出我的方向。

另外今天早上當我登錄服務器並運行以下命令時,會話wait_timeout值已恢復!

mysql> select @@global.wait_timeout, @@session.wait_timeout; 
+-----------------------+------------------------+ 
| @@global.wait_timeout | @@session.wait_timeout | 
+-----------------------+------------------------+ 
|    31536000 |     28800 | 
+-----------------------+------------------------+ 
1 row in set (0.00 sec) 

我看到,這是很多人的一個問題在線與MySQL的運行各種web應用,但似乎沒有人有一個修復。網上有很多建議指向了wait_timeout,但它似乎並沒有改變我所看到的錯誤。我已經嘗試了關於這個問題的MySQL手冊的修復,但仍然沒有運氣(鏈接:http://dev.mysql.com/doc/refman/5.0/en/gone-away.html

任何意見將不勝感激。服務器細節和產品版本如下:

服務器:OpenSUSE 11.4中 MySQL版本:53年5月1日

許多在此先感謝!

回答

1

This article上Drupal.org可能是您的情況有所幫助:

MySQL comes with a default configuration of the resources it is going to use, specified in "my.cnf" (Linux) or "my.ini" (Windows) during the installation of MySQL.

In Linux this file is located at /etc/my.cnf to set global options, or /usr/local/var/mysql-data-dir/my.cnf to set server-specific options.

In Windows this file is located by default at C:\Program Files\MySQL\MySQL Server X.Y\my.ini .

Resources allowed by the default configuration are normally insufficient to run a resource-intensive application. You must modify the following resource specifications if they are available in your original configuration file, or add them to the configuration file if they are not already specified (because some are not present by default) :

Important: Remember to keep backup files before you do anything! You will also have to reload the MySQL service after making changes to these configuration files.

MyISAM specifications:

[mysqld] 
port = 3306 
socket = /tmp/mysql.sock 
skip-external-locking 
key_buffer = 384M 
max_allowed_packet = 64M 
table_cache = 4096 
sort_buffer_size = 2M 
read_buffer_size = 2M 
read_rnd_buffer_size = 64M 
myisam_sort_buffer_size = 64M 
thread_cache_size = 8 
query_cache_size = 32M 

InnoDB specifications:

innodb_buffer_pool_size = 384M 
innodb_additional_mem_pool_size = 20M 
innodb_log_file_size = 10M 
innodb_log_buffer_size = 64M 
innodb_flush_log_at_trx_commit = 1 
innodb_lock_wait_timeout = 180 

Note: It is assumed here that you are using the InnoDB database tables, as Drupal is a resource intensive application. If you are not using the InnoDB database tables try to change this, in view of the fact that you are getting the Warning: MySQL server has gone away - apparently meaning that your setup is resource intensive.

5

雖然不是很優雅,下面的代碼片段,幫助我擺脫超時並保持持續連接。它會拋出異常,如果連接失敗連續$ limit次,但如果問題超時,最多隻需要1次重試。

$db = null; 
$limit = 10; 
$counter = 0; 
while (true) { 
    try { 
     $db = new PDO('mysql:host=' . db_host . ';dbname=' . db_name, db_user, db_pass); 
     $db->exec("SET CHARACTER SET utf8"); 
     $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); 
     $db->setAttribute(PDO::ATTR_PERSISTENT, true); 
     break; 
    } 
    catch (Exception $e) { 
     $db = null; 
     $counter++; 
     if ($counter == $limit) 
      throw $e; 
    } 
} 
+0

,實際工作!必須堅持持續的連接。儘管如此,我仍然在這裏使用'while()'循環。任何關於利弊的任何想法? – Hafenkranich

1

有一個連接被關閉的各種原因。

參考: https://dev.mysql.com/doc/refman/5.0/en/gone-away.html

我也遇到了類似的問題上使用PDO由主導管理員殺死連接,如果它睡覺超過一分鐘。因此,我想出了自己的類,它將包裝PDO類。這將檢測連接是否關閉,並嘗試重新連接查詢執行。

回答以下

PDO: MySQL server has gone away

+1

雖然這個鏈接可能回答這個問題,但最好在這裏包含答案的重要部分,並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 – MZaragoza

+0

爲什麼downvote。我已經回答過類似的問題。 – mysticmo