Menu

DatabaseError: 'MySQL server has gone away'

Help
2005-04-01
2012-09-19
  • Luca Ferroni

    Luca Ferroni - 2005-04-01

    For I upgraded from version 0.9.2 to 1.2.0 I get the following very ugly error:

    DatabaseError: (2006, 'MySQL server has gone away')

    I mysql.log I see that as soon as I complete a "single" request (I mean a request that is not included in a "for" cycle), my client "Quit" the connection to db.

    Then I downgraded to previous version and it's all ok.
    Could this probably need to be fixed or is that my fault?

     
    • Frido Ferdinand

      Frido Ferdinand - 2006-02-04

      I had the same problem as you can see in the bug reports. This entailed creating a mysql connection before a fork was done, which is a bad idea generally. However the error was not caught in 4.x, and only showed up with 5.x AND compiling against mysqlclient_r.

       
    • Andy Dustman

      Andy Dustman - 2005-04-01

      It's most likely your fault. Hard to say without a test case. State your MySQL and Python versions when you post your example.

       
    • Luca Ferroni

      Luca Ferroni - 2005-04-01

      Sorry,
      my python version is 2.3.4,
      my MySQLdb version was 1.2.0

      This is the init code for my connector.

      from MySQLdb.connections import Connection

      settings = {
      'passwd': 'mypass',
      'host': '127.0.0.1', #i prefer tcp connection rather than local unix socket
      'user': 'myuser',
      'port': 3306
      }

      class DB(Connection):
      def init(self, name, settings):
      self.name = name
      Connection.init(self,
      settings)
      self.use_me()
      def
      use_me(self):
      try:
      self.execute_query("CREATE DATABASE %s " % self.name)
      except:
      print "Database %s already exists" % self.name
      self.execute_query("USE %s" % self.name)
      def execute_query(self, query):
      cu = self.cursor()
      cu.execute(query)
      return cu.fetchall()

       
      • Andy Dustman

        Andy Dustman - 2005-04-01

        What MySQL version are you using (client and server)?

        You're doing some strange stuff in there... Instead of trying to execute a USE query, you should probably use self.select_db(self.name) instead. Also, if you want to get rid of additional complexity, have your __use_me() method do this instead:

            self.execute_query("CREATE DATABASE %s IF NOT EXISTS" % self.name)
           self.select_db(self.name)
        

        Though I think your entire approach is a little weird, from what I've seen of it, but I don't know what your goals are.

         
    • Tom McCarthy

      Tom McCarthy - 2005-05-17

      Did you ever resolve this issue Luca? I'm having the same problem, which I didn't have with a previous version of MySQLdb.

       
    • Tom McCarthy

      Tom McCarthy - 2005-05-17

      I just realized that the error I'm receiving is slightly different:

      OperationalError: (2006, 'MySQL server has gone away')

      MySQLdb 1.2.0
      Python 2.3.5

      My application talks to several db's, it talks to these two types:
      mysql 4.0.24 gentoo (localhost)
      mysql 3.23.58-9.1 fedora core2 (remote)

      I'm having trouble isolating the error, as it only seems to happen every now and then. I'm using the Pool class that Andy wrote for my connections. This application is running under mod_python (3.1.3).

      It only seems to happen when I run the following query:
      "show table status"

      Now that I've typed all this and have been playing with it, and have put in a bunch of debug information, I can't get the error to happen again.

       
      • Andy Dustman

        Andy Dustman - 2005-05-18

        I suspect that, after periods of inactivity, some of the connections are timing out. After you get a connection from the Pool, you should first execute:

        connection.ping()

        This will reopen the connection if necessary.

        Also, make sure you use your Pool in non-blocking mode. Pool blocks by default, mainly because this is what the Queue does (Queue is part of the standard library). I probably should revisit Pool at some point.

         
        • Tom McCarthy

          Tom McCarthy - 2005-05-27

          connection.ping() seemed to be working well for a while, but a few of my users have gotten the following error output recently:

          ile "/usr/lib/python2.3/site-packages/tool/sqlhandler.py", line 39, in
          run
          pool_connection.ping()

          OperationalError: (2013, 'Lost connection to MySQL server during query')

          For now, I'm going to just catch the Exception and try to get a different connection from the Pool, but is there an easier way to deal with this?

          Another thought I had was sub-classing Pool with a class that would call ping() on a call to get(), and if there was an OperationalError, it would "discard" the connection, make a new one, put it in the pool, and send it to the caller of get().

          Anyone have any thoughts?

           
          • Andy Dustman

            Andy Dustman - 2005-05-27

            Subclassing Pool would be easy. Trivial even.

             
            • Eugene Lazutkin

              Eugene Lazutkin - 2005-09-05

              Andy,

              Was it resolved? Could you explain what to do?

              I am getting weird random errors mostly in a form of "OperationalError: (2013, 'Lost connection to MySQL server during query')". Amazingly sometimes I am getting them during connection.ping().

              My app uses Django (http://www.djangoproject.com/) and runs as FastCGI process. All Django's MySQL support is in one small file: http://code.djangoproject.com/browser/django/trunk/django/core/db/backends/mysql.py
              I added self.connection.ping() to methods of DatabaseWrapper class to combat a connection loss problem (I suspected that it was the culprit of 2013 error). But I am still getting them.

              Any insight is appreciated.

              Thanks,

              Eugene

               
              • Eugene Lazutkin

                Eugene Lazutkin - 2005-09-05

                I forgot to mention that I use MySQLdb (1, 2, 1, 'gamma', 2) with Python 2.3.5.

                 
    • Tom McCarthy

      Tom McCarthy - 2005-05-18

      Thanks Andy, I'll try that out. I'll post again if I run into the same error.

       
    • Luca Ferroni

      Luca Ferroni - 2005-05-20

      Unfortunately I couldn't spend much time in investigating this problem because of I'm working a lot and so I chose to use MySQLdb version 1.1.6 which comes with Ubuntu (my distro).

      This version seems to work well.

       
      • Andy Dustman

        Andy Dustman - 2005-05-20

        The difference between 1.1.6 and 1.2.0 is not large.

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.