Thursday, October 2, 2008

MySQL and MySQLdb

Yet another post on this stuff, for my own reference...

A common issue with MySQL on the Mac is an error like "ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'". Help files suggest changing your /etc/my.cnf file which doesn't exist on a Mac. What I can do is use a ~/.my.cnf file instead. I can't remember now if I created this file or if it was created for me. I also can't remember if I put the '/var/lib/mysql/mysql.sock' path in for the mysql.sock file or not...

What MySQL did want was for the sock file path to be /tmp/mysql.sock, so I edited the section in my ~/.my.cnf file as such:

[client]
socket = /tmp/mysql.sock

That does the trick. I should note that ~/.my.cnf is the last of five locations MySQL will check for this configuration data. See here for more info.


Once I got that working again, I went to use mysqldb and got this error:

ImportError: No module named mysqldb

I think I had flushed out my mysqldb installs during a purge for a particularly nasty bug hunt. So, I go to reinstall it from a tarball. I use the classic "python setup.py install" and get this:

EnvironmentError: mysql_config not found

I've seen the errors about missing library files that you need to copy over or link to but this one was new... I'm thinking that the reason is that I was originally using my MySQL install directory for development work so it was on my path before. During the bug hunt, I had reset my path and eliminated the directory with the mysql_config file... Sure enough, that was it.

It turns out that I may have also deleted my mysql library files from /usr/local/mysql as that director no longer exists... May need to re-install MySQL...

...the next day after a bit of sleep...

Looks like the /usr/local/mysql wasn't the problem. I did a quick search on my puter for "mysqlclient_r" and lo and behold, there is an entry in the FAQ about this:

Build Errors
------------

ld: fatal: library -lmysqlclient_r: not found

mysqlclient_r is the thread-safe library. It's not available on
all platforms, or all installations, apparently. You'll need to
reconfigure site.cfg (in MySQLdb-1.2.1 and newer) to have
threadsafe = False.

As soon as I set threadsafe to False, it builds.

No comments: