简体   繁体   English

Python mysqldb错误关闭连接

[英]Python mysqldb error closing connection

I'm having a problem when closing a connection as follows: 关闭连接时出现问题,如下所示:

   database = 'sed_database'   
   conn = MySQLdb.Connect(host='remote_host', user='default',
                          passwd='pass', db=database)    
   try:
      try:
         cursor = conn.cursor()
         cursor.execute(sql_str)
         results = cursor.fetchall()
      except MySQLdb.Error, e:
         print "MySQL/Server Error using query: %s" % sql_str
         print "Using database: %s" % database
         raise e
   finally:
      if cursor:
         cursor.close()
      if conn:
         conn.close()

This gives: 这给出:

 Traceback (most recent call last):
  File "trass.py", line 579, in ?
    main(sys.argv)
  File "trass.py", line 555, in main
    old_rows, changes_list = auto_analyse_test(f, args.build, args.quiet, args.debug)
  File "trass.py", line 352, in auto_analyse_test
    last_analysed_build = get_sed_baseline_ref(test_file_name, old_delivery_stream)
  File "trass.py", line 151, in get_sed_baseline_ref
    results = execute_sql_query(sql, delivery_stream)
  File "trass.py", line 197, in execute_sql_query
    passwd='pass', db=database)
  File "C:\Python24\Lib\site-packages\MySQLdb\__init__.py", line 75, in Connect
    return Connection(*args, **kwargs)
  File "C:\Python24\Lib\site-packages\MySQLdb\connections.py", line 164, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.InternalError: (3, "Error writing file 'D:\\MySQL_Datafiles\\Logfiles\\query.
log' (Errcode: 9)")

Python's MySQLDB library info is as follows: Python的MySQLDB库信息如下:

>>> print MySQLdb.get_client_info()
4.1.18
>>> print MySQLdb.__version__
1.2.1_p2
>>> print MySQLdb.__revision__
410

What is strange is that: 奇怪的是:

  • I've checked on the server and query.log exists and is being written to by other processes. 我已经检查了服务器,query.log是否存在并且正在被其他进程写入。
  • This code works through several iterations, then on a particular item it fails. 此代码经过多次迭代,然后在特定项目上失败。
  • The exact query runs fine via SQLyog and yields four results. 确切的查询可以通过SQLyog正常运行,并产生四个结果。

The server error.log says "Aborted connection... (Got an error reading comminication packets)" 服务器error.log中显示“连接中断...(读取通讯包时出错)”

While the Traceback appears to show the error being associated with the connection creation, it doesn't occur until the connection is closed (or the function ends, which I guess closes it by default). 虽然“回溯”似乎显示与连接创建相关的错误,但直到关闭连接(或函数结束,我猜默认情况下会关闭),该错误才会发生。 I've tried putting extra output or pauses between open and close. 我尝试在打开和关闭之间放置额外的输出或暂停。 Every time the exception occurs on the close. 每当关闭时发生异常。 So what could cause this error on closing the connection? 那么关闭连接时可能导致此错误的原因是什么?

Here's what I found so far. 这是我到目前为止发现的。

It appears that error is triggered when opening a connection, at MySQLdb.Connect(...) , 2nd line in pasted code, not when closing a connection. 似乎在MySQLdb.Connect(...)粘贴代码中的第二行MySQLdb.Connect(...)打开连接时触发了错误,而不是在关闭连接时触发了错误。

Full backtrace: 完整回溯:

  • ... ...
  • execute_sql_query [op] execute_sql_query [op]
  • MySQLdb Connect [op] MySQLdb Connect [操作]
  • MySQLdb super(...) [op] MySQLdb超级(...)[op]
  • _mysql.c ConnectionObject_Initialize [lower level pyhon module, written in C] _mysql.c ConnectionObject_Initialize [较低层的pyhon模块,用C编写]
  • libmysql mysql_real_connect or mysql_options [probably the earlier] libmysql mysql_real_connect或mysql_options [可能是较早的]
  • fails, exception is set 失败,设置了异常

Let's decode the exception 让我们解码异常

InternalError:
    (3,
     "Error writing file 'D:\\MySQL_Datafiles\\Logfiles\\query.log'
                         (Errcode: 9)")
  • "3" older mysql mysys_err.h EE_WRITE 3 “ 3”旧的mysql mysys_err.h EE_WRITE 3
  • "query.log", is this local or remote log file? “ query.log”,这是本地还是远程日志文件? appears to be a windows path. 似乎是Windows路径。
  • "Errorcode: 9" assuming windows (above), that is ERROR_INVALID_BLOCK "The storage control block address is invalid." “”错误代码:9“假定为Windows(上方),即ERROR_INVALID_BLOCK ”存储控制块地址无效。“ Quite cryptic, but it'd go and check if this file exist, if it is writeable, and if it may be subject to logrotate or similar. 相当神秘,但是它会去检查该文件是否存在,是否可写以及是否可能经过logrotate或类似操作。 Check disk space, for a good measure, do a disk check as well. 检查磁盘空间,作为一项很好的措施,请同时进行磁盘检查。

It appears to be a client-side error. 它似乎是客户端错误。 Please check your client-side my.cnf , [client] section. 请检查您的客户端my.cnf [client]部分。

source code for given MySQLdb version 给定MySQLdb版本的源代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM