Wednesday, 23 September 2009

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):

shell> PATH="/usr/local/mysql/bin:$PATH"
shell> tar xzf MySQL-python-1.2.3c1.tar.gz
shell> cd MySQL-python-1.2.3c1
shell> ARCHFLAGS="-arch x86_64" /usr/bin/python setup.py build
shell> /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:


shell> /usr/bin/python test_mysql.py

It should output the available storage engines.

13 comments:

Kenneth Anguish said...

You can edit the site.cfg file with mysql-python, and specify the mysqld-config.

note19 said...

Great post. Very useful. Thank you!

Philip said...

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 said...

Thanks!

red.is.the.new.black. said...

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) -D__version__=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 said...

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 said...

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) -D__version__=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 said...

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 said...

@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 said...

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 said...

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 said...

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 said...

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).