Install python-mysql (MySQLdb) on Mac OSX

April 18th, 2008 by Sadri Sahraoui | Print Install python-mysql (MySQLdb) on Mac OSX

To connect to a MySQL database from Django or a python script you have to install MySQLdb. The installation of the package on MacOSX is not straightforward, so here is a small howto.

  • Download and unpack the MySQLdb package
  • Edit the setup-posix.py file, and change the line below:
    mysql_config.path = "mysql_config"
    to
    mysql_config.path = "/usr/local/mysql/bin/mysql_config"
    

    Note: you have to specify the path to your mysql_config unless you have installed mysql.com package for mac

  • Build
     python setu.py clean
     python setup.py build
     sudo python setup.py install
    

    If you get this compile error

    /usr/include/sys/types.h:92: error: duplicate ‘unsigned’
    /usr/include/sys/types.h:92: error: two or more data types in declaration specifiers
    

    edit the _mysql.c file and comment this line

    #define uint unsigned int
    

, ,

 

11 Comments

  1. Thanks!

    This fix got me past my first build problem of the compile error.

    I’m building on an iMac G5 running 10.5 Leopard .
    I now have the wrong arch warning:

    ld: warning in /usr/local/mysql/lib/mysql/libmysqlclient_r.a, file is not of required architecture

    so my next step is to fix the arch parameters to support ppc64.

  2. Gene Warner

    b1t4qlhazo4fawez

  3. I hit the same warnings as Blogimus, on an 32-bit Intel Mac, but I went ahead and installed and everything worked okay.

    I’ve written up my experience here,
    http://openeducationresearch.blogspot.com/2008/12/mysql-with-python-on-mac.html

    and created a binary egg for easy_install,
    http://www.cs.cmu.edu/%7Etaleahma/projects/misc/MySQL_python-1.2.2-py2.5-macosx-10.5-i386.egg

    So you should be able to install this now with this simple command,

    easy_install http://www.cs.cmu.edu/%7Etaleahma/projects/misc/MySQL_python-1.2.2-py2.5-macosx-10.5-i386.egg

    Thanks for the fix!

  4. Sampath Girish Munagala

    Sampath:MySQL-python-1.2.2 venkatarampey$

    Sampath:MySQL-python-1.2.2 venkatarampey$ python setup.py clean
    running clean

    Sampath:MySQL-python-1.2.2 venkatarampey$ python setup.py build
    running build
    running build_py
    copying MySQLdb/release.py -> build/lib.macosx-10.5-i386-2.5/MySQLdb
    running build_ext
    building ‘_mysql’ extension
    creating build/temp.macosx-10.5-i386-2.5
    gcc -fno-strict-aliasing -Wno-long-double -no-cpp-precomp -mno-fused-madd -fno-common -dynamic -DNDEBUG -g -Os -Wall -Wstrict-prototypes -DMACOSX -I/usr/include/ffi -DENABLE_DTRACE -pipe -Dversion_info=(1,2,2,’final’,0) -D__version__=1.2.2 -I/usr/local/mysql/include -I/System/Library/Frameworks/Python.framework/Versions/2.5/include/python2.5 -c _mysql.c -o build/temp.macosx-10.5-i386-2.5/_mysql.o -g -Os -arch i386 -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: No such file or directory
    error: command ‘gcc’ failed with exit status 1

    Sampath:MySQL-python-1.2.2 venkatarampey$

    “I Got this error after following the above steps mentioned. Anyone can solve this will be very helpful to me. Advance thanks for ur reply”.

    Please post ur reply to girishmsampath@gmail.com,girishmsampath@in.com,girishmsampath@yahoo.co.in

    Pl keep subject as Solution for Build error in python.

    Regards,
    Sampath Girish.

  5. David

    I can’t seem to get around this problem on OS 10.4.11 …

    building ‘_mysql’ extension
    gcc -Wl,-F. -Wl,-F. -bundle -undefined dynamic_lookup build/temp.darwin-8.11.0-Power_Macintosh-2.3/_mysql.o -L/mysql/lib/mysql -lmysqlclient_r -lz -lm -lmygcc -o build/lib.darwin-8.11.0-Power_Macintosh-2.3/_mysql.so
    ld: -undefined: unknown argument: dynamic_lookup
    error: command ‘gcc’ failed with exit status 1

  6. Powderflask

    Turadg – brilliant!
    your egg installed perfectly – just needed to do one extra step:
    libmysqlclient_r.16.dylib is required in /usr/local/mysql/lib/
    I had a slightly older version of MySQL (r.15) – downloaded latest MySQL tar (5.1) – works perfect.

    Thanks very much for making this a truly easy install.

  7. Arthur

    Good egg!

  8. why not just use the ports system ?

    port install py-mysql +mysql5

  9. If you’re having trouble getting it installed the new version of mysql-python requires some different instructions, also if you’re using 64 or 32 bit versions of python or mysql will make a difference. There’s some information here:

    http://learninglamp.wordpress.com/2010/02/21/mysqldb-python-mysql-and-os-x-a-match-made-in-satans-bum/

  10. Hi Andy

    Thanks for the link

  11. ??????? ?? ??????? ????????

Trackbacks

Leave a Reply