MySQL, Python and MacOS X 10.6 (Snow Leopard)

This has been already mentioned in on a few blogs, but I thought it would be good to post here too. Note: this is not using MacPorts!

To get MySQL and Python going on MacOS X 10.6 you need the following:

Install MySQL using the tar ball and make sure you get it up and running.

Compile MySQL-python (leave out setting the $PATH when it’s already done):

$ PATH="/usr/local/mysql/bin:$PATH"
$ tar xzf MySQL-python-1.2.3c1.tar.gz
$ cd MySQL-python-1.2.3c1
$ ARCHFLAGS="-arch x86_64" /usr/bin/python setup.py build
$ /usr/bin/python setup.py install

I’m giving the full path for python to make sure it does not use the MacPorts one.

EDIT 2009-09-24: If it can’t find mysql_config, you did not set your path correctly, but you can update the setup_posix.py fine of MySQL-Python and change it to something like this (as seen on our forums):

mysql_config.path = "/usr/local/mysql-5.1.39-osx10.5-x86_64/bin/mysql_config"

Here a test script test_mysql.py:

import MySQLdb

if __name__ == "__main__":
  db = MySQLdb.connect(host="localhost",user="root",db="test")
  cursor = db.cursor()
  cursor.execute("SHOW ENGINES")

  for row in cursor.fetchall():
    print row

  cursor.close()
  db.close()

Run the above script like:

$ /usr/bin/python test_mysql.py

It should output the available storage engines.

Comments

Kenneth Anguish
You can edit the site.cfg file with mysql-python, and specify the mysqld-config.
note19
Great post. Very useful. Thank you!
Philip
Thanks for this post. When I try and run the sample script I get a mysqldb not defined error. The install didn't give any errors (I don't think it did..)
SAKrisT
Thanks!
red.is.the.new.black.
Says

gcc-4.2 -fno-strict-aliasing -fno-common -dynamic -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -pipe -Dversion_info=(1,2,3,'gamma',1) -Dversion=1.2.3c1 -I/usr/local/mysql/include -I/System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6 -c _mysql.c -o build/temp.macosx-10.6-universal-2.6/_mysql.o -g -Os -arch ppc64 -fno-common -D_P1003_1B_VISIBLE -DSIGNAL_WITH_VIO_CLOSE -DSIGNALS_DONT_BREAK_READ -DIGNORE_SIGHUP_SIGQUIT -DDONT_DECLARE_CXA_PURE_VIRTUAL
unable to execute gcc-4.2: No such file or directory
error: command 'gcc-4.2' failed with exit status 1

WHY??
Geert JM Vanderkelen
It's in the error message..
..unable to execute gcc-4.2: No such file or directory
error: command 'gcc-4.2' failed with exit status 1

Check X-Code installation..
Alex
I'm getting a similar error, but I've reinstalled xcode, not sure how to resolve this, any ideas?

Mine is complaining about a lot of missing files:

core:MySQL-python-1.2.3c1 ayoung$ ARCHFLAGS='-arch x86_64' /usr/bin/python setup.py build
running build
running build_py
copying MySQLdb/release.py -> build/lib.macosx-10.6-universal-2.6/MySQLdb
running build_ext
building '_mysql' extension
gcc-4.2 -fno-strict-aliasing -fno-common -dynamic -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -pipe -arch x86_64 -Dversion_info=(1,2,3,'gamma',1) -Dversion=1.2.3c1 -I/usr/include -I/System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6 -c _mysql.c -o build/temp.macosx-10.6-universal-2.6/_mysql.o -fno-omit-frame-pointer -pipe -D_P1003_1B_VISIBLE -DSIGNAL_WITH_VIO_CLOSE -DSIGNALS_DONT_BREAK_READ -DIGNORE_SIGHUP_SIGQUIT
_mysql.c:36:23: error: my_config.h: No such file or directory
_mysql.c:38:19: error: mysql.h: No such file or directory
_mysql.c:39:26: error: mysqld_error.h: No such file or directory
_mysql.c:40:20: error: errmsg.h: No such file or directory



_mysql.c:2421: error: initializer element is not constant
_mysql.c:2421: error: (near initialization for ‘_mysql_ResultObject_memberlist[0].offset’)
_mysql.c: In function ‘_mysql_ConnectionObject_getattr’:
_mysql.c:2443: error: ‘_mysql_ConnectionObject’ has no member named ‘open’
error: command 'gcc-4.2' failed with exit status 1
core:MySQL-python-1.2.3c1 ayoung$
Alex
Should have noted that I'm using os x server with MySQL built in, don't know if that may be the difference. (Maybe it needs mysql sources? Not sure how to go about adding them…)
Geert JM Vanderkelen
@Alex
Try to install the tar ball distribution of MySQL for Mac OS X in /usr/local. Unpack it there and make sure the /usr/local/mysql symlink is in place.
Then try again with that version.
Alex
Thanks Geert, I decided I really wanted to keep the Apple version of MySQL so updates / administration would be consistent as possible with the rest of the services installed.

I did find a solution to my problem, it's not as elegant as I would desire, but that would depend on Apple releasing MySQL libs for 10.6 server.

I followed the directions for the 10.5 library download and install on this Apple kb article: http://support.apple.com/kb/TA25017

That got me where I needed to be to get django running as desired with a MySQL backend.
dhatch
Thanks for this post. The install completes successfully but when I try to import MySQLLdb i get an error saying it does not exist. I noticed that the .egg file was installed in my /Library/Python/2.6/site-packages folder but my python installation is located in /Library/Frameworks/Python.framework/ and the .egg file is not in the site-packages folder in that directory. Could this be a problem?
xtfer
After a number of different attempts, I managed to set this up using these instructions and the current 10.6 binaries from mysql.com. Thanks.
Jimmy
If you get a compiler error with an exit status 1, about 8 lines above it will say there's an error with the Xcode install. Basically, you need to install the latest version of Xcode (or at least install it again, I'd just go find the latest version at Apple's Dev Central).
psico

I’m getting this error:

gcc-4.2 -fno-strict-aliasing -fno-common -dynamic -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -pipe -Dversion_info=(1,2,3,‘final’,0) -Dversion=1.2.3 -I/usr/local/mysql/include -I/System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6 -c _mysql.c -o build/temp.macosx-10.6-universal-2.6/_mysql.o -Os -g -fno-common -fno-strict-aliasing -arch x86_64 In file included from /usr/include/unistd.h:519, from /System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6/Python.h:44, from pymemcompat.h:10, from _mysql.c:29: /usr/include/sys/select.h:114: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before numeric constant In file included from _mysql.c:36: /usr/local/mysql/include/my_config.h:327:1: warning: “SIZEOF_SIZE_T” redefined In file included from /System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6/Python.h:9, from pymemcompat.h:10, from _mysql.c:29: /System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6/pymacconfig.h:33:1: warning: this is the location of the previous definition In file included from _mysql.c:36: /usr/local/mysql/include/my_config.h:420:1: warning: “HAVE_WCSCOLL” redefined In file included from /System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6/Python.h:8, from pymemcompat.h:10, from _mysql.c:29: /System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6/pyconfig.h:803:1: warning: this is the location of the previous definition error: command ‘gcc-4.2’ failed with exit status 1

Any ideas?

Geert Vanderkelen
@psico Use MySQL 64-bit.
leovitch

Sigh, how can this still be so hard in 2011? I downloaded the 64-bit version of MySQL:

http://ftp.iij.ad.jp/pub/db/mysql/Downloads/MySQL-5.5/mysql-5.5.14-osx10.6-x86_64.dmg

It’s all installed and running happily (not shown here but I can connect to db etc. too).

% set path=($path /usr/local/mysql/bin) % mysql_config … --cflags [-I/usr/local/mysql/include -Os -g -fno-common -fno-strict-aliasing -arch x86_64] --include [-I/usr/local/mysql/include] --libs [-L/usr/local/mysql/lib -lmysqlclient -lpthread] --libs_r [-L/usr/local/mysql/lib -lmysqlclient_r -lpthread] --version [5.5.14] --libmysqld-libs [-L/usr/local/mysql/lib -lmysqld -lpthread] …

The client library exists and is 64-bit:

% lipo /usr/local/mysql/lib/libmysqlclient.dylib -info Non-fat file: /usr/local/mysql/lib/libmysqlclient.dylib is architecture: x86_64

But even so, python can’t find the mysql client lib:

% python Python 2.6.1 (r261:67515, Aug 2 2010, 20:10:18) [GCC 4.2.1 (Apple Inc. build 5646)] on darwin Type “help”, “copyright”, “credits” or “license” for more information. >>> import MySQLdb Traceback (most recent call last): File “”, line 1, in File “build/bdist.macosx-10.6-universal/egg/MySQLdb/init.py”, line 19, in File “build/bdist.macosx-10.6-universal/egg/_mysql.py”, line 7, in File “build/bdist.macosx-10.6-universal/egg/_mysql.py”, line 6, in bootstrap ImportError: dlopen(/Users/leo.hourvitz/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib Referenced from: /Users/leo.hourvitz/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so Reason: image not found >>> ^D

Is Python really running 64-bit? Looks like it:

% python Python 2.6.1 (r261:67515, Aug 2 2010, 20:10:18) [GCC 4.2.1 (Apple Inc. build 5646)] on darwin Type “help”, “copyright”, “credits” or “license” for more information. >>> import platform >>> print platform.architecture() (‘64bit’, “) >>> ^D %

What happened when I compiled, you ask? Some warnings, but the link command in particular looks perfectly sensible. I did the build in bash just to be sure I’m following the instructions above:

bash-3.2$ pwd MySQL-python-1.2.3 bash-3.2$ PATH=“/usr/local/mysql/bin:$PATH” bash-3.2$ ARCHFLAGS=“-arch x86_64” python setup.py build running build running build_py creating build/lib.macosx-10.6-universal-2.6 copying _mysql_exceptions.py -> build/lib.macosx-10.6-universal-2.6 creating build/lib.macosx-10.6-universal-2.6/MySQLdb copying MySQLdb/init.py -> build/lib.macosx-10.6-universal-2.6/MySQLdb copying MySQLdb/converters.py -> build/lib.macosx-10.6-universal-2.6/MySQLdb copying MySQLdb/connections.py -> build/lib.macosx-10.6-universal-2.6/MySQLdb copying MySQLdb/cursors.py -> build/lib.macosx-10.6-universal-2.6/MySQLdb copying MySQLdb/release.py -> build/lib.macosx-10.6-universal-2.6/MySQLdb copying MySQLdb/times.py -> build/lib.macosx-10.6-universal-2.6/MySQLdb creating build/lib.macosx-10.6-universal-2.6/MySQLdb/constants copying MySQLdb/constants/init.py -> build/lib.macosx-10.6-universal-2.6/MySQLdb/constants copying MySQLdb/constants/CR.py -> build/lib.macosx-10.6-universal-2.6/MySQLdb/constants copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.macosx-10.6-universal-2.6/MySQLdb/constants copying MySQLdb/constants/ER.py -> build/lib.macosx-10.6-universal-2.6/MySQLdb/constants copying MySQLdb/constants/FLAG.py -> build/lib.macosx-10.6-universal-2.6/MySQLdb/constants copying MySQLdb/constants/REFRESH.py -> build/lib.macosx-10.6-universal-2.6/MySQLdb/constants copying MySQLdb/constants/CLIENT.py -> build/lib.macosx-10.6-universal-2.6/MySQLdb/constants running build_ext building ‘_mysql’ extension creating build/temp.macosx-10.6-universal-2.6 gcc-4.2 -fno-strict-aliasing -fno-common -dynamic -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -pipe -Dversion_info=(1,2,3,‘final’,0) -Dversion=1.2.3 -I/usr/local/mysql/include -I/System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6 -c _mysql.c -o build/temp.macosx-10.6-universal-2.6/_mysql.o -Os -g -fno-common -fno-strict-aliasing -arch x86_64 In file included from _mysql.c:36: /usr/local/mysql/include/my_config.h:330:1: warning: “SIZEOF_SIZE_T” redefined In file included from /System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6/Python.h:9, from pymemcompat.h:10, from _mysql.c:29: /System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6/pymacconfig.h:33:1: warning: this is the location of the previous definition In file included from _mysql.c:36: /usr/local/mysql/include/my_config.h:423:1: warning: “HAVE_WCSCOLL” redefined In file included from /System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6/Python.h:8, from pymemcompat.h:10, from _mysql.c:29: /System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6/pyconfig.h:803:1: warning: this is the location of the previous definition gcc-4.2 -Wl,-F. -bundle -undefined dynamic_lookup build/temp.macosx-10.6-universal-2.6/_mysql.o -L/usr/local/mysql/lib -lmysqlclient_r -lpthread -o build/lib.macosx-10.6-universal-2.6/_mysql.so -arch x86_64

bash-3.2$

The output from setup.py install was clean with no errors.

It seems like the MySQLdb .so is not correctly referencing the mysql client library via the full absolute path. But isn’t it supposed to do that by itself?

This machine has been nowhere near Mac Ports.

Thanks for any help,

Leo

Geert Vanderkelen

@leovitch To get you going, you can do like this: shell> DYLD_LIBRARY_PATH=“/usr/local/mysql/lib” python python> import MySQLdb

Not sure still what is wrong. Doing more steps than what I blogged is just annoyance. Maybe you can google to solution though? I’ll check later more.

leovitch

Geert,

Thanks, I knew there was some variable like that and the above does let me load MySQLdb for test purposes. Of course since I’m installing this for the sake of a web app and fiddling with DYLD_LIBRARY_PATH in my Apache is a bit sketchy, I’m trying to find the real solution. It seems like the crux of it must be the link command, huh?

Leo

leovitch

FWIW, otool does seem to show the built _mysql.so has a relative rather than absolute path in it:

% otool -L build/lib.macosx-10.6-universal-2.6/_mysql.so build/lib.macosx-10.6-universal-2.6/_mysql.so: libmysqlclient.18.dylib (compatibility version 18.0.0, current version 18.0.0) /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.11)

Is that expected?

Leo

leovitch

I found a workaround although it still feels like something must be going wrong if this extra step is required.

After building, as mentioned before, the _mysql.o has a relative path:

% otool -L build/lib.macosx-10.6-universal-2.6/_mysql.so build/lib.macosx-10.6-universal-2.6/_mysql.so: libmysqlclient.18.dylib (compatibility version 18.0.0, current version 18.0.0) /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.11)

So I use install_name_tool to change it as suggested at http://lightyearsoftware.com/2011/02/mysql-5-5-on-mac-os-x/:

% install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.18.dylib build/lib.macosx-10.6-universal-2.6/_mysql.so

And sure enough it’s absolute afterwards:

% otool -L build/lib.macosx-10.6-universal-2.6/_mysql.so build/lib.macosx-10.6-universal-2.6/_mysql.so: /usr/local/mysql/lib/libmysqlclient.18.dylib (compatibility version 18.0.0, current version 18.0.0) /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.11)

And sure enough if I reinstall that, it works without DYLD_LIBRARY_PATH:

% python Python 2.6.1 (r261:67515, Aug 2 2010, 20:10:18) [GCC 4.2.1 (Apple Inc. build 5646)] on darwin Type “help”, “copyright”, “credits” or “license” for more information. >>> import MySQLdb /Library/Python/2.6/site-packages/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg/_mysql.py:3: UserWarning: Module _mysql was already imported from /Library/Python/2.6/site-packages/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg/_mysql.pyc, but /Users/leovitch/src/MySQL-python-1.2.3 is being added to sys.path >>> ^D

I’m not sure what the origin of the warning message is -- why should my build directory be added to sys.path? -- but it seems like this at least gets the module installed correctly.