HowTo: Patch mysqldb and sqlobject so database doesn't "go away" all the time

14 views
Skip to first unread message

Sean McBride

unread,
Jul 26, 2006, 9:46:56 PM7/26/06
to pylons-discuss
We were continually having a problem on our production test server
where we would get random 500 errors. The underlying error was that
"MySQL server has gone away", and we searched around forever online
trying to find a solution. Finally we did. I figured that I'd post here
so that others could benefit.

Basically, the Python mysql library is missing an interface with the C
library's MYSQL_OPT_RECONNECT flag which instructs the client to
reconnect with the mysql server if the connection is lost or refused
for some reason (which I guess can happen frequently on a busy server.)
Thus, connections get dropped and you get 500 errors. Luckily, there's
a patch that a user submitted to the mysqldb sourceforge project.

Here's what to do:

1. First, go and download the patch from this location:
http://sourceforge.net/tracker/index.php?func=detail&aid=1483074&group_id=22307&atid=374934

2. Get the correct version of MySQLdb to apply the patch to:
svn co
https://svn.sourceforge.net/svnroot/mysql-python/tags/MySQLdb-1.2.1_p2
mysqldb-patched

3. Go into the mysqldb-patched/MySQLdb/ directory and apply the patch
to the install:
patch < mysql-patch

4. Build the patched library
python setup.py build
(If you have errors here, make sure you have all the dev packages you
need. Likely missing candidates are python-dev and
libmysqlclient15-dev)

5. Install the patched library
python setup.py install

6. Remove the bogus sets.py, sets.pyc, sets.pyo files.
(For some reason, this version of mysqldb has files that override the
default sets library. Just delete them all from your python
site-packages/MySQLdb/ directory.)

7. Try importing MySQLdb
(It should work now.)

8. Edit your SQLObject file:
(In your SQLObject folder in site-packages, edit the
mysql/mysqlconnection.py file and add reconnect=1 to the arguments
passed to the self.module.connect function in
MySQLConnection.makeConnection.)

Now you should have no more "mysql has gone away" errors. (If that was
your problem.)

Anybody else ever have to do this?

Reply all
Reply to author
Forward
0 new messages