mysql服务器消失了(错误#2006)

时间:2012-02-02 16:36:35

标签: mysql django wsgi mysql-python

在apache 2.2.9 / Debian上使用wsgi运行Django 1.3,并使用mysql 5.0.51a我在部署的django安装和我们运行的两个开发服务器中都遇到了以下问题,使用了2个数据库。

对于每个用户,某个功能导致 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     beta@localhost 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-server。

重新启动mysql-server,没有结果。重启apache2和django开发服务器以及mysql-server,没有结果。半小时后,一切都很好......

我意识到我几乎没有给你任何东西。我想发布一些更多,但正如我所提到的,我关闭了Django错误页面。我无法重现这个错误,这有点可怕。这个错误至少持续了一刻钟,但也许是四分之三个小时。然后每件事情再次奏效,我不知道为什么。对于我未经训练的人来说,无法在apache2和数据库中改变那段时间的变化。

哦,是的,我用google搜索了这个,发现了一些关于wait_timeout的东西,发现它的值可能已经足够高了(默认),而且只有在很多闲置之后mysql会消失,而且刷新页面后一切都很好。

任何人都可以帮助我解决为什么会发生这种情况以及如何防范这个2006年的错误?

1 个答案:

答案 0 :(得分:2)

因此,在各个站点,我发现连接长时间打开时会发生此错误。我忽略了这一点,因为我认为这不适用于我的情况。经过深思熟虑后,我重写了代码,以便在发生此错误时自动重新连接然后重试,但它感觉不到正确。然后我发现我的代码已经长时间保持打开连接,而本身并没有必要。我用于数据库访问的对象在各种模块级别实例化,所以我猜它们只是在我重新启动apache时创建的。难怪连接在几天后就开始了......

所以现在我只在需要它们的函数内部创建数据库对象,并且因为错误2006年没有出现问题:D。问题出在椅子和键盘之间......