10

I downloaded and followed the install instructions for MySQL 5.5.8 (http://dev.mysql.com/downloads/mysql/) and for the MySQLdb python plugin. (http://sourceforge.net/projects/mysql-python/)

When I attempt to import MySQLdb to a python terminal, I get the below error:

Safira:~ yanigisawa$ python --version
Python 2.6.1
Safira:~ yanigisawa$ python -c "import MySQLdb"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "build/bdist.macosx-10.6-universal/egg/MySQLdb/__init__.py", line 19, in <module>
  File "build/bdist.macosx-10.6-universal/egg/_mysql.py", line 7, in <module>
  File "build/bdist.macosx-10.6-universal/egg/_mysql.py", line 6, in __bootstrap__
ImportError: dlopen(/Users/yanigisawa/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so, 2): Library not loaded: libmysqlclient.16.dylib
  Referenced from: /Users/yanigisawa/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so
  Reason: image not found
Safira:~ yanigisawa$ 

I found question 4559699 on stack overflow, and followed the instructions there:

Safira:~ yanigisawa$ echo $DYLD_LIBRARY_PATH
/usr/local/mysql/lib
Safira:~ yanigisawa$ ls -l /usr/local/mysql/lib
total 363224
-rwxr-xr-x   1 root  wheel   3787328 Jan 18 19:41 libmysqlclient.16.dylib
-rw-r--r--   1 root  wheel  10035336 Dec  3 13:26 libmysqlclient.a
lrwxr-xr-x   1 root  wheel        23 Jan 14 22:01 libmysqlclient.dylib -> libmysqlclient.16.dylib
lrwxr-xr-x   1 root  wheel        20 Jan 14 22:01 libmysqlclient_r.16.dylib -> libmysqlclient.dylib
lrwxr-xr-x   1 root  wheel        16 Jan 14 22:01 libmysqlclient_r.a -> libmysqlclient.a
lrwxr-xr-x   1 root  wheel        20 Jan 14 22:01 libmysqlclient_r.dylib -> libmysqlclient.dylib
-rw-r--r--   1 root  wheel  88033760 Dec  3 13:22 libmysqld-debug.a
-rw-r--r--   1 root  wheel  84075304 Dec  3 13:26 libmysqld.a
-rw-r--r--   1 root  wheel      8472 Dec  3 13:26 libmysqlservices.a
drwxr-xr-x  13 root  wheel       442 Dec  3 13:53 plugin
Safira:~ yanigisawa$ md5 /usr/local/mysql/lib/libmysqlclient.16.dylib 
MD5 (/usr/local/mysql/lib/libmysqlclient.16.dylib) = b8a78a93183720021b9dbe9ddad57111
Safira:~ yanigisawa$ 

And after cleaning all of the python egg files, and rebuilding, I still got the same error. Then I found bug 59006 (http://bugs.mysql.com/bug.php?id=59006) in MySQL 5.5.8 affecting Mac 10.6. I tried the work-around listed there, cleaned eggs and rebuilt, and I'm still getting this error.

Is there another work-around for this problem specific to python? Am I missing something else?

EDIT: Adding output from otool commands:

Safira:~ yanigisawa$ otool -L /Users/yanigisawa/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so
/Users/yanigisawa/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so:
    libmysqlclient.16.dylib (compatibility version 16.0.0, current version 16.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.1)
Safira:~ yanigisawa$ otool -DX /usr/local/mysql/lib/libmysqlclient.16.dylib
/usr/local/mysql/lib/libmysqlclient.16.dylib
Safira:~ yanigisawa$ 
Community
  • 1
  • 1
yanigisawa
  • 731
  • 1
  • 8
  • 19

3 Answers3

49

In my case, I solved by adding a couple of symbolic links as in http://ageekstory.blogspot.com/2011/04/installing-massive-coupon-on-mac-os-10.html

as following:

sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

sudo ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql

Community
  • 1
  • 1
Chris
  • 636
  • 1
  • 5
  • 6
  • 1
    +1 this is what finally solved my problem after many hours, even after Ned Deily's edit seemed to work but didn't – cerberos Aug 16 '11 at 16:43
  • this worked in solving the same problem seen in Eclipse/PyDev/Django/MySQL on macosx - thanks! – erikvw Mar 04 '12 at 19:31
11

You could try running otool to find out exactly what library paths the MySQLdb C extension, _mysql.so is looking for:

$ otool -L /Users/yanigisawa/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so

and then the installed library name of the MySQL library file:

$ otool -DX /usr/local/mysql/lib/libmysqlclient.16.dylib

But you should not have to resort to setting DYLD_LIBRARY_PATH (or using install_name_tool to modify an executable); the need to set it is almost always a sign of a component that was built or installed incorrectly. In my experience, the MySQL project does not have a very good track record for consistency in their OS X builds and installers, as that issue you cite tends to confirm.

Trying to get a working combination of Python, MySQLdb, and MySQL libraries in OS X 10.6 is a very common cause of frustration: lots of questions about it here on SO and elsewhere. There are a number reasons for that. My advice is to go with a complete solution from one of the third-party distributors that will build and install compatible versions of everything you need. And it should keep working if you need to upgrade components as time goes on. I've had good success over the years with MacPorts; others prefer Fink or the newer HomeBrew. With MacPorts, after installing the base files from the MacPorts .dmg, you can get everything built like so:

$ sudo port selfupdate  # ensure the port files are up-to-date
$ sudo port install py26-mysql

The MacPorts Python will be available at /opt/local/bin/python2.6. If you are looking to run Django, there's a port for that as well.

EDIT: With the updated output from otool, you can see that there is a mismatch of library path names. The MySQLdb extension is asking for a relative path name (no initial /), while the MySQL library advertises itself with an absolute path, normally what you want. It seems to be the reverse of the MySQL issue 59006 but, without knowing exactly what steps you've performed up until now, it is really hard to say how you got to this state. You may be able to use install_name_tool to patch the _mysql.so to have the absolute path name which would also eliminate any need to set DYLD_LIBRARY_PATH. It's worth trying:

$ cd ~/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp
$ sudo install_name_tool -change libmysqlclient.16.dylib \
                                 /usr/local/mysql/lib/libmysqlclient.16.dylib \
                         _mysql.so

But that should not leave you with a warm and fuzzy feeling that you can reproduce the results when you need to update something. There really is no virtue in trying to glue all these pieces from different sources into something that works.

Ned Deily
  • 83,389
  • 16
  • 128
  • 151
  • I'm still new to the Mac OS. (about 2 months in now) I had hoped to get a "trial by fire" so to speak and get everything working more or less the manual way. However, if I can't get this working, I'll resort to using the mac ports option. Thanks for the suggestion. – yanigisawa Jan 19 '11 at 04:10
  • The MacPorts solution eventually worked. It installed python 2.6.6 along side my 2.6.1 installation, and /usr/bin/python still pointed to 2.6.1, which caused the same error above to occur with my apache cgi scripts. I had to add a symlink to the 2.6.6 version, and everything started working fine. Thanks. – yanigisawa Jan 20 '11 at 13:57
  • Glad to hear. But beware: in general, it's a very bad idea to overwrite anything in `/usr`, other than `/usr/local`. That's all managed by Apple as part of OS X. I'm not sure if you meant you overwrote `/usr/bin/python`. Much better ways to handle multiple versions is to modify shell startup files to put `/opt/local/Library/Frameworks/Python.framework/Versions/2.6/bin` at the front of `$PATH` or use an absolute reference when referring to Python, `/opt/local/bin/python2.6'. – Ned Deily Jan 20 '11 at 17:09
  • Thx for the help. It turns out the bug is exactly MySQL bug 59006. It's a packaging bug. The solution at the top of that bug report works perfectly. You can actually `sudo install_name_tool -change libmysqlclient.xx.dylib /usr/local/mysql/lib/libmysqlclient.xx.dylib /usr/local/mysql/lib/libmysqlclient.xx.dylib` and DYLD_LIBRARY_PATH wouldn't be necessary anymore. – Y.H Wong Apr 08 '11 at 00:21
  • Hmm, I haven't tried it myself but shouldn't that be `sudo install_name_tool -id /usr/local/mysql/lib/libmysqlclient.xx.dylib /usr/local/mysql/lib/libmysqlclient.xx.dylib`? You want to be changing the shared library identification name of the client lib, no? And, of course, you want to first verify that the installed client lib name is wrong by using `otool -DX /usr/local/mysql/lib/libmysqlclient.xx.dylib`. Ugh, the MySQL project really does not have a good track record with OS X packaging. – Ned Deily Apr 08 '11 at 00:39
  • For this particular case, both works, don't know what the general case should be tho. – Y.H Wong Apr 08 '11 at 02:16
  • I've been banging my head against a brick wall for hours (and have done so a few time previously). Your post solved my problem, specifically the `install_name_tool` bit in the edit. Many many thanks. I can't believe how difficult it is to install mysql with the python bindings on Snow Leopard, it's crazy. – cerberos Aug 16 '11 at 16:26
1

I tried addressing the problem of an unfound dynamics library by compiling the MySQLdb module with static libraries (set static = True in site.cfg). But that generated the same error, as _mysql.o still was asking for the dynamic library. To fix that, I added libraries = [] to the block for if static in setup_posix.py.

Cosmin
  • 21,216
  • 5
  • 45
  • 60
Kevin
  • 11
  • 1