2012-02-02 90 views
2

在Apache 2.2.9/Debian上使用wsgi運行Django 1.3,並使用mysql 5.0.51a在部署的django安裝和我們的兩個開發服務器中都遇到了以下問題運行,使用2個數據庫。mysql服務器消失了(錯誤#2006)

對於每個用戶,某個功能導致了一個2006:服務器已經消失錯誤。 去年函數接下來的事情就叫做之前開始出問題是在一個線程,如:

t = threading.Thread(target = logic.report, 
        args = [proj_info, userdata] 
        ) 
t.setDaemon(True) 
t.start() 

這件事情在後臺發生的事情,而GUI(Django的網站)仍然是可用的。這種情況一直持續好幾十次,但今天下午失敗了。

返回Django的(通過瀏覽器,當然我忘了讓該標籤頁中打開,瞭解更多信息)指出,在事情發生的14時15分18秒,所以這裏的錯誤是一些mysql.log

120202 14:15:18 3449 Connect  [email protected] on beta2db 
      3449 Query  SET NAMES utf8 
      3449 Query  set autocommit=0 
      3449 Query  SELECT `auth_user`.`id`, `auth_user`.`username`, `auth_user`.`first_name`, `auth_user`.`last_name`, `auth_user`.`email`, `auth_user`.`password`, `auth_user`.`is_staff`, `auth_user`.`is_active`, `auth_user`.`is_superuser`, `auth_user`.`last_login`, `auth_user`.`date_joined` FROM `auth_user` WHERE `auth_user`.`id` = 3 
      3449 Query  SELECT `insight_test_run`.`id`, `insight_test_run`.`rawfile`, `insight_test_run`.`koekfile`, `insight_test_run`.`measure_date`, `insight_test_run`.`test_id`, `insight_test_run`.`subject_id`, `insight_test_run`.`quality` FROM `insight_test_run` WHERE `insight_test_run`.`id` = 514 
      3449 Query  SELECT `insight_project`.`id`, `insight_project`.`client`, `insight_project`.`description`, `insight_project`.`directory` FROM `insight_project` INNER JOIN `insight_project_test_run` ON (`insight_project`.`id` = `insight_project_test_run`.`project_id`) WHERE `insight_project_test_run`.`test_run_id` = 514 
      3449 Query  SELECT `auth_user`.`id`, `auth_user`.`username`, `auth_user`.`first_name`, `auth_user`.`last_name`, `auth_user`.`email`, `auth_user`.`password`, `auth_user`.`is_staff`, `auth_user`.`is_active`, `auth_user`.`is_superuser`, `auth_user`.`last_login`, `auth_user`.`date_joined` FROM `auth_user` INNER JOIN `insight_project_user` ON (`auth_user`.`id` = `insight_project_user`.`user_id`) WHERE `insight_project_user`.`project_id` = 6 
      3449 Query  SELECT `django_content_type`.`app_label`, `auth_permission`.`codename` FROM `auth_permission` INNER JOIN `auth_group_permissions` ON (`auth_permission`.`id` = `auth_group_permissions`.`permission_id`) INNER JOIN `auth_group` ON (`auth_group_permissions`.`group_id` = `auth_group`.`id`) INNER JOIN `auth_user_groups` ON (`auth_group`.`id` = `auth_user_groups`.`group_id`) INNER JOIN `django_content_type` ON (`auth_permission`.`content_type_id` = `django_content_type`.`id`) WHERE `auth_user_groups`.`user_id` = 3 
      3449 Query  rollback 
      3449 Query  SELECT `insight_subject`.`id`, `insight_subject`.`name`, `insight_subject`.`nice_name`, `insight_subject`.`handedness`, `insight_subject`.`birthday`, `insight_subject`.`gender`, `insight_subject`.`education`, `insight_subject`.`eyecorrection`, `insight_subject`.`extra` FROM `insight_subject` WHERE `insight_subject`.`id` = 10000456 
      3449 Query  SELECT `insight_test`.`id`, `insight_test`.`description`, `insight_test`.`level_id`, `insight_test`.`name` FROM `insight_test` WHERE `insight_test`.`id` = 1 
      3449 Query  SELECT `insight_test_level`.`id`, `insight_test_level`.`description`, `insight_test_level`.`official_name` FROM `insight_test_level` WHERE `insight_test_level`.`id` = 1 
      3449 Quit  

這裏有點Apache日誌(/var/log/apache2/error.log)的:

[Thu Feb 02 14:17:00 2012] [error] Exception in thread Thread-2: 
[Thu Feb 02 14:17:00 2012] [error] Traceback (most recent call last): 
[Thu Feb 02 14:17:00 2012] [error] File "/usr/local/lib/python2.7/threading.py", line 530, in __bootstrap_inner 
[Thu Feb 02 14:17:00 2012] [error]  self.run() 
[Thu Feb 02 14:17:00 2012] [error] File "/usr/local/lib/python2.7/threading.py", line 483, in run 
[Thu Feb 02 14:17:00 2012] [error]  self.__target(*self.__args, **self.__kwargs) 
[Thu Feb 02 14:17:00 2012] [error] File "/usr/local/www/wsgi-scripts/portal/ovl_webinterface/insight/logic.py", line 50, in report 
[Thu Feb 02 14:17:00 2012] [error]  reportfile = ovl.report.make_report(django_test_run_id = j, img = trunfo['img'], text_style = trunfo['style'], anonymous = anon) 
[Thu Feb 02 14:17:00 2012] [error] File "/usr/local/lib/python2.7/site-packages/ovl_analystT/reporting.py", line 90, in make_report 
[Thu Feb 02 14:17:00 2012] [error]  info = dbman.gather_all_test_run_info(django_test_run_id = django_test_run_id) 
[Thu Feb 02 14:17:00 2012] [error] File "/usr/local/lib/python2.7/site-packages/ovl_analystT/dbconnector.py", line 802, in gather_all_test_run_info 
[Thu Feb 02 14:17:00 2012] [error]  test_run = self.get_table_rows2(table = 'test_run', convert_to_one = True, django_id = django_test_run_id) 
[Thu Feb 02 14:17:00 2012] [error] File "/usr/local/lib/python2.7/site-packages/warehouseT/manager.py", line 213, in get_table_rows2 
[Thu Feb 02 14:17:00 2012] [error]  cols = kwargs.pop('columns',self.get_table_column_names(table))#if columns are specified (using keyword 'columns') it only loads those. 
[Thu Feb 02 14:17:00 2012] [error] File "/usr/local/lib/python2.7/site-packages/warehouseT/manager.py", line 116, in get_table_column_names 
[Thu Feb 02 14:17:00 2012] [error]  res = self.execute('''DESCRIBE %s'''%(table))#this function is so basic.. it should present no trouble, so no debug stuff. 
[Thu Feb 02 14:17:00 2012] [error] File "/usr/local/lib/python2.7/site-packages/warehouseT/manager.py", line 91, in execute 
[Thu Feb 02 14:17:00 2012] [error]  self.cursor.execute(cmd)#this may raise warnings which we need to catch 
[Thu Feb 02 14:17:00 2012] [error] File "build/bdist.linux-x86_64/egg/MySQLdb/cursors.py", line 174, in execute 
[Thu Feb 02 14:17:00 2012] [error]  self.errorhandler(self, exc, value) 
[Thu Feb 02 14:17:00 2012] [error] File "build/bdist.linux-x86_64/egg/MySQLdb/connections.py", line 36, in defaulterrorhandler 
[Thu Feb 02 14:17:00 2012] [error]  raise errorclass, errorvalue 
[Thu Feb 02 14:17:00 2012] [error] OperationalError: (2006, 'MySQL server has gone away') 

(在這片日誌的時間戳看,我猜Django的錯誤,指出現場到請求完成的那一刻,而不是錯誤發生的時間)

所以基本上這是我自己的一段代碼中的一個錯誤(即,再次,無數次地工作了很多很多次),它被mysql停止了。

我認爲mysql日誌看起來很正常。在我發佈的內容之前,我已經查看過了,但我沒有認出任何奇怪的東西,但是我沒有每天閱讀它們。 使用root用戶以及不同的用戶在本地登錄到mysql-sever並沒有任何問題。我期望能夠做的所有事情都能做到。我沒有嘗試任何用戶www-data,因爲我坦率地不知道如何登錄。另外,django使用另一個用戶名登錄到mysql服務器。

重新啓動mysql服務器,沒有結果。重新啓動apache2和django開發服務器以及mysql-server,沒有結果。半小時後,一切都很好..

我意識到我給你幾乎沒有什麼可以繼續下去。我想發佈更多,但正如我所提到的,我關閉了Django錯誤頁面。而且我不能重現這個有點令人恐懼的錯誤。這個錯誤至少持續了一刻鐘,但也許是四分之三個小時。然後,每件事都再次奏效,我不知道爲什麼。對於我的未經訓練的眼睛,沒有線索,當時在apache2和數據庫中發生了什麼變化..

哦,是啊我google了這個,找到了一些關於wait_timeout,發現它的值可能足夠高(默認) ,而且只有在MySQL閒聊之後纔會離開,而且在刷新頁面之後一切都很好。

任何人都可以幫助我爲什麼發生這種情況,以及如何防止這個2006年的錯誤?

回答

2

因此,在各個網站,我發現當連接長時間打開時會發生此錯誤。我忽略了這一點,因爲我認爲這不適用於我的情況。經過深思熟慮之後,我重寫了代碼,以便在發生此錯誤時自動重新連接,然後重試,但感覺不到。然後我發現我的代碼確實長時間保持開放連接,而本身並不是必需的。我用於數據庫訪問的對象在各個模塊級別實例化,所以我猜想它們只有在重新啓動apache時才創建。難怪幾天之後連接就開始了......

所以現在我只在需要它們的函數中創建數據庫對象,並且因爲錯誤2006而沒有問題:D。問題發生在椅子和鍵盤之間......

相關問題