Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

>>> 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.
  • This code works through several iterations, then on a particular item it fails.
  • The exact query runs fine via SQLyog and yields four results.

The server error.log says "Aborted connection... (Got an error reading comminication packets)"

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?

like image 342
James Bradbury Avatar asked Dec 13 '13 11:12

James Bradbury


People also ask

How do I close a cursor connection in python?

close() Method. Use close() when you are done using a cursor. This method closes the cursor, resets all results, and ensures that the cursor object has no reference to its original connection object.

Is it necessary to close MySQL connection in python?

If you're accessing MySQL directly, like you do in your example code above, then yes, you'll need to disconnect explicitly.

What is MySQLdb in Python?

MySQLdb is an interface for connecting to a MySQL database server from Python. It implements the Python Database API v2. 0 and is built on top of the MySQL C API. Packages to Install. mysql-connector-python mysql-python.


1 Answers

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.

Full backtrace:

  • ...
  • execute_sql_query [op]
  • MySQLdb Connect [op]
  • MySQLdb super(...) [op]
  • _mysql.c ConnectionObject_Initialize [lower level pyhon module, written in C]
  • libmysql mysql_real_connect or mysql_options [probably the earlier]
  • 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
  • "query.log", is this local or remote log file? appears to be a windows path.
  • "Errorcode: 9" assuming windows (above), that is ERROR_INVALID_BLOCK "The storage control block address is invalid." 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. 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.

source code for given MySQLdb version

like image 77
Dima Tisnek Avatar answered Oct 09 '22 03:10

Dima Tisnek