97 Comments

  1. Jacob
    ·

    Thank you very much you saved me ungodly amounts of heartache

  2. Lyle
    ·

    Wow, thanks a million! Or at least 997,412. I had no trouble installing this years ago, but, alas, can’t remember how! I sound like a quote from your article.

    Anyway, the reason people work so hard at getting this to work is, of course, how much you can do once you get it working! I’ve got dozens of little python scripts that whale on my mySQL databases and do marvelous things.

    Thanks again!


  3. ·

    Thank you thank you thank you so much!!! All the other recipes out there weren’t working for me, this was perfect.


  4. ·

    I’ve installed Mysql through mac ports – and it works fine.
    Will i need to uninstall this? I really want to get Django and Mysql working…


  5. ·

    Gareth – If you can start a python shell and type “import MySQLdb” with no errors, and your database is working fine, then no reason to go back. These instructions are really for people who have already set up the official mysql and don’t want to have to switch to the macports one just to get the connector working.

  6. Gareth
    ·

    Thanks, works pefecto! :)

  7. Lennart
    ·

    Thanks! Kept me busy for three hours but this worked !

  8. zynth
    ·

    Thanks so much…process was very smooth. This was a real timesaver for me….no more headbanging.


  9. ·

    This post may have just saved my life. Or at least my career. Definitely my sanity.

    I spent two days trying to get Python/Django/MySQL to work on my Mac the first time around — and then spent two long nights trying to put it all back together again after an ill-fated attempt to install PostgresSQL broke everything. (If you could ever write a SQL sequel on how to get psycopg to work on a Mac, I guarantee it would be an even bigger hit.)

    Anyway, on behalf of data journalism, struggling writers-turned-programmers and Mac lovers everywhere, thankyouthankyouthankyou.


  10. ·

    I don’t do postgres, but glad you found this useful. Judging by the comments, I can see that I’m not alone in my feelings about this screwed up situation. Just amazing that the problem has lasted this long.


  11. ·

    Thank you so much. Finally can import MySQLdb.
    But, I cannot get a connection:
    _mysql_exceptions.OperationalError: (2002, “Can’t connect to local MySQL server through socket ‘/opt/local/var/run/mysql5/mysqld.sock’ (2)”)

    Any suggestions?


  12. ·

    Peter, did you see the end of the tip in the original post?


  13. ·

    Again, thanks so much for the post – but I have no immediate interest in “Django” so using the “settings.py” to set the socket is irrelevant as is setting DATABASE_HOST.
    Furthermore I cannot simply copy /tmp/mysql.sock to /opt/local/var/run/mysql5/mysqld.sock (I get “Operation not supported on socket”).
    Any ideas?


  14. ·

    Peter – I’m not saying you need to be using Django, I’m saying that whatever app or util you’re using to connect to mysql must have a DATABASE_HOST or HOST setting somewhere…


  15. ·

    After looking at _mysql.c, I used the following with success:

    conn=MySQLdb.connect(host=’localhost’, user=’whatever’,
    passwd=’whatever’, unix_socket=’/tmp/mysql.sock’)

    So, thanks much again.

  16. Chris
    ·

    You are an absolute lifesaver. I spent several hours doing everything I thought I could do… and finally your solution worked.

    THANK YOU.


  17. ·

    This is so cool. Thanks so much for writing this. I am *so* bookmarking this.

  18. Jerry
    ·

    I was getting that same init_mysql error. I finally realized I had installed the 64-bit version of MySQL. Once I had the 32-bit version of MySQL installed, I ran python setup.py clean –all to clean my last build then I reran all the MySQL_python install commands. Everything worked fine after that.

    I’m new to Python so I’m uncertain if it is 64-bit ready.

    You can check your installation running these two commands in Terminal:
    cd /usr/local/mysql/lib
    file *

    If you see any libraries listed as “Mach-O 64-bit dynamically linked shared library x86_64″ then you have the 64 bit version.


  19. ·

    Great tips Jerry – thanks. Not sure if that was my issue though – I don’t show any trace of 64-bit libs, but still had the problem.

  20. Guy
    ·

    You’re the Master!
    It seams impossible to do it without your explanation.
    Thanks


  21. ·

    Scot,
    I’m running 10.6, and I’m downloading the MySQL package to install it now.

    I was able to use the easy_install . command, as my MacPorts MySQL binaries were on my ZSH path (if they’re on your BASH path, it should work too) — which was at /opt/local/lib/mysql5/bin.

    I’ll comment back once I test the MySQL installer (I’m on 10.6 but I’m downloading the 10.5 installer; they don’t seem to have a package for 10.6 yet).


  22. ·

    On 10.6, I’ve got /usr/local/mysql/bin on my PATH, and I was able to run the easy_install . command, as it could find mysql_config without a problem.

    It’s wrapped itself up in a nice little egg in my Python site-packages directory.
    Processing .
    Running setup.py -q bdist_egg --dist-dir /Users/ben/Downloads/MySQL-python-1.2.3c1/egg-dist-tmp-_9FIBu
    zip_safe flag not set; analyzing archive contents...
    Adding MySQL-python 1.2.3c1 to easy-install.pth file

    Installed /Library/Python/2.6/site-packages/MySQL_python-1.2.3c1-py2.6-macosx-10.6-universal.egg
    Processing dependencies for MySQL-python==1.2.3c1
    Finished processing dependencies for MySQL-python==1.2.3c1

    After that, I pulled up one of my Django projects that uses MySQL, and synced it. Works like a charm.


  23. ·

    Benjamin that’s *great* news – thanks for that news. Makes me feel less squeamish about upgrading.


  24. ·

    Here’s another solution from the SourceForge page for the project:

    export ARCHFLAGS=’-arch i386 -arch x86_64′
    cd MySQL-python-1.2.3c1
    python setup.py build
    sudo python setup.py install

    Seems to work fine.

  25. duh
    ·

    Whoa.

    Thanks for the information. However, I get now the following error when trying to import MySQLdb:

    Python 2.5.4 (r254:67917, Dec 23 2008, 14:57:27)
    [GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin
    Type “help”, “copyright”, “credits” or “license” for more information.
    >>> import MySQLdb
    Traceback (most recent call last):
    File “”, line 1, in
    File “/opt/local/lib/python2.5/site-packages/MySQLdb/__init__.py”, line 19, in
    import _mysql
    ImportError: dlopen(./_mysql.so, 2): no suitable image found. Did find:
    ./_mysql.so: mach-o, but wrong architecture
    >>>

    Any ideas?

  26. duh
    ·

    Yeah, I investigated this issue a bit further and concluded that the _mysql.so library does get compiled as “Mach-O 64-bit bundle x86_64″ and my Python is 32-bit = problem.

    Is there some way to force that i386 architecture would be used instead? I found out that MacPorts has a configuration file in /opt/local/etc/macports/macports.conf that has a switch for determining the build architecture. It defaults to x86_64 in Snow Leopard but I forced it to i386. However, it didn’t affect at all, that library still appears to be 64-bit :(

    I wonder if I am the only one here with Snow Leopard having the problem, is everybody else using 64-bit python?


  27. ·

    duh – I haven’t tried this on Snow Leopard yet but my understanding is that SL comes with Python 2.6. Your trace above shows Python 2.5.4, indicating that you’re using using a different (MacPorts?) version of Python.

    I’ll update these notes when I get SL, but if my reading is correct, the binding source compiles pretty clean. For now I recommend you search for sites that document the process for Snow Leopard.

  28. Greek_kid
    ·

    Thank you master!


  29. ·

    I followed the instructions to the letter…I even had to muck about with the stdarg.h file per this link:

    http://packetcloud.net/tag/mysql/

    and I finally (FINALLY!) got build and install to get all the way through, only to hit the following break:

    brandon-watsons-macbook-pro:MySQL-python-1.2.3c1 blwatson$ python
    Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39)
    [GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin
    Type “help”, “copyright”, “credits” or “license” for more information.
    >>> import MySQLdb
    /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/MySQL_python-1.2.3c1-py2.6-macosx-10.3-fat64.egg/_mysql.py:3: UserWarning: Module _mysql was already imported from /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/MySQL_python-1.2.3c1-py2.6-macosx-10.3-fat64.egg/_mysql.pyc, but /Users/blwatson/Downloads/MySQL-python-1.2.3c1 is being added to sys.path
    Traceback (most recent call last):
    File “”, line 1, in
    File “MySQLdb/__init__.py”, line 19, in
    import _mysql
    File “build/bdist.macosx-10.3-fat64/egg/_mysql.py”, line 7, in
    File “build/bdist.macosx-10.3-fat64/egg/_mysql.py”, line 6, in __bootstrap__
    ImportError: dlopen(/Users/blwatson/.python-eggs/MySQL_python-1.2.3c1-py2.6-macosx-10.3-fat64.egg-tmp/_mysql.so, 2): no suitable image found. Did find:
    /Users/blwatson/.python-eggs/MySQL_python-1.2.3c1-py2.6-macosx-10.3-fat64.egg-tmp/_mysql.so: mach-o, but wrong architecture

    So I am at a loss…I have been wrestling with getting MySQL and Django working for days now. Your info has been immensely helpful in getting me to this stage, so thank you VERY much. However, I am at a roadblock…hopefully you know how to parse the above error.


  30. ·

    Ugh, I tried reinstalling and am getting the dreaded mysql.sock error.

    I have installed the 64bit version of MySQL onto Snow Leopard. I went to:

    http://dev.mysql.com/downloads/mysql/5.1.html#macosx-dmg

    and got:

    Mac OS X 10.5 (x86_64)

    and ran:

    mysql-5.1.39-osx10.5-x86_64.pkg

    once it was installed. I then ran:

    $ sudo ./MySQLCOM start
    Starting MySQL database server

    but then get:

    $ mysql
    ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/mysql/mysql.sock’ (2)

    the mysql dir in /var is empty. I have also looked in /tmp. Nothing. I then ran:

    $sudo find / -name mysql.sock

    and nothing was found on the disk.

    Lastly, I tried:

    $ sudo mysqld_safe
    Password:
    091001 01:18:42 mysqld_safe Logging to ‘/usr/local/mysql/data/brandon-watsons-macbook-pro.local.err’.
    091001 01:18:42 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
    091001 01:18:44 mysqld_safe mysqld from pid file /usr/local/mysql/data/brandon-watsons-macbook-pro.local.pid ended

    What do I do now?


  31. ·

    Brandon, sorry to have not gotten back to you sooner. Life just got flooded. Did you get this solved? One thing I notice above is that you mention:

    mysql-5.1.39-osx10.5

    but you’re talking about installing on Snow Leopard.

  32. Mariano
    ·

    You rock man! Thanks so much!
    I’ve been trying many times to get it working and you just explained it great.

    Thanks!
    Mariano.


  33. ·

    Thanks, you just saved my butt. I thought this was one of those issues I’d spend hours on.

  34. cinaglia
    ·

    Thanks. This really helped me out. I had spent two hours, and I supposed that if I hadn’t stumbled upon this, it would keep giving some trouble for awhile.

  35. Cyrus
    ·

    thanks++. This was really helpful.


  36. ·

    I solved the problem “Error loading MySQLdb module: dynamic module does not define init function (init_mysql)” as follows:

    – I listed all the contents from py26-mysql doing the following

    # port contents py26-mysql

    – and I added the following entry to /etc/profile

    export PYTHONPATH=”/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/”

    Good post!. I think you can add this as an alternative way :).

    L.

  37. brianz
    ·

    I spent the good part of a day battling this as well. Everytime I get a new Mac, I seem to stumble through this.

    I was getting the “wrong architecture” error from the _mysql.so which was being built, even thought I was passing the 64-bit build args. I ended up switching to 32-bit MySql, and built MySQLdb with “-arch i386″. Voila…everything works….finally.

    I think the reason why this has been so painful for so long is that the author considers OS X a “fringe operating system“. :>)


  38. ·

    Heh – Well, he apparently considers Windows a “fringe operating system” too :)

  39. znice
    ·

    Hey, thanks! I spent about an hour and a half beating my head against the wall trying to wrangle the release version up on sourceforge into working. Almost just went with another database I was so frustrated. Props and gratitude!

  40. Fabian
    ·

    DATABASE_HOST = ‘/tmp/mysql.sock’

    Thanks so much for this. After much frustration, this was able to get everything working.


  41. ·
  42. Cheeky
    ·

    For heaven’s sake man, put the tip jar back out!


  43. ·

    Incredible that this is still such a clusterf**k after so long. Do most python/django developers use Postgres, or something?


  44. ·

    pjm, I agree that it’s incredible. But nope, most python/django devs do use MySQL. We just wrestle through this and then hope the next OS update doesn’t break everything. File under “puts hairs on your chest.”


  45. ·

    What can I say, except I was in a world of pain there for many evenings going round in circles, uninstalling, reinstalling completely removing mysql and the python mysql a number of times until I found this.
    Thank you!!! This worked I just launched django successfully connected to mysql rather than mysqlite for the first time, wow thank you again!

  46. Daniel
    ·

    Hi,

    I have tried all possible solutions but this is still not working for me and I have no idea why. The last solution I tried was the SNOW LEOPARD one. I did all the steps. Erased my previous mysql (MAMP), reinstall the 64-bit architecture mysql (working fine now!) and run the xcode installer.

    Then I did the steps you suggest but when I do: ARCHFLAGS=’-arch x86_64′ python setup.py build I get a huge list of errors:
    running build
    running build_py
    copying MySQLdb/release.py -> build/lib.macosx-10.5-fat64-2.6/MySQLdb
    running build_ext
    building ‘_mysql’ extension
    gcc -L/sw/lib -bundle -L/sw/lib/python2.6/config -lpython2.6 -arch x86_64 build/temp.macosx-10.5-fat64-2.6/_mysql.o -L/usr/local/mysql/lib -lmysqlclient_r -lz -lm -lmygcc -o build/lib.macosx-10.5-fat64-2.6/_mysql.so
    ld: warning: in /sw/lib/python2.6/config/libpython2.6.dylib, file is not of required architecture
    Undefined symbols:
    “_PyMember_SetOne”, referenced from:

    …. (a lot more stuff here)…
    and at the end
    __mysql_server_init in _mysql.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status
    error: command ‘gcc’ failed with exit status 1

    Please I have spent two days trying to solve this issue and no luck so far… any help will be strongly appreciated :-)


  47. ·

    Daniel – Are you actually ON Snow Leopard, or did you try that technique on Leopard?

  48. Daniel
    ·

    No, i am ON snow leopard… mac os X version 10.6.2 build 10C540 and when i run python i get Python 2.6.2 (r262:71600, Apr 28 2009, 20:43:17)
    [GCC 4.0.1 (Apple Inc. build 5490)] on darwin
    and gcc –version
    i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5646) (dot 1)
    Copyright (C) 2007 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions. There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

    not sure if that helps you but at this point I am quite hopeless :-(… thanks a lot shacker


  49. ·

    Hmm… I wonder how you ended up with version 2.6.2 of Python. I’m on SL and have 2.6.1. I’d hate to think such a minor version difference would prevent compilation, but maybe there’s something non-standard in your Python install?

  50. Daniel
    ·

    hmmmm not sure either… do you know a good way to uninstall python and install the version you have? where can I download the python version you are suggesting? i could try that and see if it works, at this point i’m clueless of what’s going on so may be worth trying…


  51. ·

    If you type:

    $ which python

    what do you get? I get:

    /usr/bin/python

    And if you run:

    /usr/bin/python

    does it then show 2.6.1?

  52. Daniel
    ·

    mmm if i type which python i get:
    /sw/bin/python
    and there is nothing in the folder usr/bin/python

  53. Daniel
    ·

    oooops sorry yes, you are right

  54. Daniel
    ·

    if i run usr/bin/python I run Python 2.6.1 (r261:67515, Jul 7 2009, 23:51:51)


  55. ·

    OK I think that’s your problem. You’ve installed a non-apple copy of python in /sw/bin – maybe from fink or ports? You should be able to find a line in your ~/.profile or or ~/.bash_profile that’s setting a shell path that finds that version before it finds the native version. Comment out that line, restart terminal, and try “which python” again. When it returns the correct path, go back and try compiling the driver from source again (you might have to “make clean” or just start with a fresh tarball).

  56. Daniel
    ·

    wow! no comments!!!!! you are a life savior! :-) thanks you sooooooooo much…

  57. JImmy Gitonga
    ·

    Hi Shacker,

    From the above comments, I can only what the joy must be for solution.
    I am happy to see the last email comment was but 10 days ago.

    Here is the beef:
    I’m running:
    Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29)
    [GCC 4.2.1 (Apple Inc. build 5646)] on darwin
    on
    Mac OS X 10.6.3 Build 10D573 with Xcode 3.2.1 for Mac OS X 10.6 Snow Leopard.

    I have mysql-5.1.45-osx10.6-x86_64 installed with its prefpane. “-$ which mysql” gives me “/usr/local/mysql/bin/mysql”. Works on the Command line as well as throughthe Workbench.

    I have a number of test DBs that I want to pull from using Django.
    So I download the MySQL-python-1.2.3c1 into ~/Downloads/Applications and navigate to this and run :-$ python setup.py build.

    To which it tells me:
    “running build
    running build_py
    creating build
    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/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/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,’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 x86_64 -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

    [… trimmed …]

    Apart from understanding what the goblyguk above represents, what am I doing wrong?


  58. ·

    Jimmy – I truncated your comment a bit. “No gcc” means you haven’t installed a compiler on your system. You need to download the developer tools from Apple so you have a compiler! Yes, it’s a 1GB download but it will come in handy for all kinds of things.

  59. JImmy Gitonga
    ·

    Shacker, you were right – again ;).

    It was the Xcode installer. Any one who upgrades to Snow Leopard should upgrade Xcode, period.

    This site is the best resource for this problem. Thanks again.


  60. ·

    Great to hear it Jimmy! Now go rock the Django.


  61. ·

    I managed to get this to work by bypassing macports, and using the XTools gnu C libraries. It’s the same when trying to install 3rd party modules on top of perl, which was 5 weeks of banging my head against a brick wall. I eventually had the epiphany that would have saved the day, but too late, I’d already ruined the leopard C libraries beyond repair. Alas, I divulge.
    Before running python setup.py, add the two following environment variables (assuming you have XCode installed). In Terminal:-
    export LDFLAGS="-L/Developer/SDKs/MacOSX10.6.sdk/usr/lib"
    export CFLAGS="-I/Developer/SDKS/MacOSX10.6.sdk/usr/include"

    Personally, I find it tidier to add symbolic links in /usr/lib and /usr/include pointing to these directories, and then point CFLAGS AND LDFLAGS to these symbolic links.
    i.e.

    $ sudo ln -s /Developer/SDKs/MacOSX10.6.sdk/usr/lib /usr/lib/xtools
    $ sudo ln -s /Developer/SDKs/MacOSX10.6.sdk/usr/include /usr/include/xtools
    $ export LDFLAGS="-L/usr/lib/xtools"
    $ export CFLAGS="-L/usr/include/xtools"

    I think it’s shocking that I never read this recommendation on any forum anywhere. I just found many recommendations saying to install xcode, but they never said how to link the xcode C libraries for compiling programs from source in the terminal. I think this is the missing key for easily compiling source code consistently in Macs, without the use of a MacPorts.
    I hope this works well for you too ;-)

  62. splendorsine
    ·

    I’m in a similar boat to a lot of people I guess. Here’s what happens when I try to run python manage.py syncdb:

    Traceback (most recent call last):
    File “manage.py”, line 11, in
    execute_manager(settings)
    File “/Library/Python/2.6/site-packages/django/core/management/__init__.py”, line 438, in execute_manager
    utility.execute()
    File “/Library/Python/2.6/site-packages/django/core/management/__init__.py”, line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
    File “/Library/Python/2.6/site-packages/django/core/management/__init__.py”, line 257, in fetch_command
    klass = load_command_class(app_name, subcommand)
    File “/Library/Python/2.6/site-packages/django/core/management/__init__.py”, line 67, in load_command_class
    module = import_module(‘%s.management.commands.%s’ % (app_name, name))
    File “/Library/Python/2.6/site-packages/django/utils/importlib.py”, line 35, in import_module
    __import__(name)
    File “/Library/Python/2.6/site-packages/django/core/management/commands/syncdb.py”, line 7, in
    from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal
    File “/Library/Python/2.6/site-packages/django/core/management/sql.py”, line 5, in
    from django.contrib.contenttypes import generic
    File “/Library/Python/2.6/site-packages/django/contrib/contenttypes/generic.py”, line 6, in
    from django.db import connection
    File “/Library/Python/2.6/site-packages/django/db/__init__.py”, line 75, in
    connection = connections[DEFAULT_DB_ALIAS]
    File “/Library/Python/2.6/site-packages/django/db/utils.py”, line 91, in __getitem__
    backend = load_backend(db[‘ENGINE’])
    File “/Library/Python/2.6/site-packages/django/db/utils.py”, line 32, in load_backend
    return import_module(‘.base’, backend_name)
    File “/Library/Python/2.6/site-packages/django/utils/importlib.py”, line 35, in import_module
    __import__(name)
    File “/Library/Python/2.6/site-packages/django/db/backends/mysql/base.py”, line 14, in
    raise ImproperlyConfigured(“Error loading MySQLdb module: %s” % e)
    django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: dlopen(/Users/matthewmarcus/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so, 2): no suitable image found. Did find:
    /Users/matthewmarcus/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so: mach-o, but wrong architecture

    That mach-o, but wrong architecture issue seems to be coming up for quite a few people. I’ve pored through the comments but I can’t work it out: has anyone come up with a simple fix for the problem?

  63. dk
    ·

    This shouldn’t be this difficult !@$^!

    I am running osx 10.6.4. I am using the python version that installed with the OS (which reports as 2.5). Downloaded 64 bit mysql v. 5.1 and have successfully used this with other projects (php).

    When attempting to build the mysql driver gcc reports the following error.

    cc1: error: unrecognized command line option “-Wno-long-double”
    the -arch x86_64 option seems to be part of the build instructions, but adding it explicitly gets the same result. … I am still stuck.

  64. Ash
    ·

    Help, 2 days of grief trying to get this to work. I’m at exactly the same point as what this guy posted above:

    Brandon Watson says:
    September 30, 2009 at 10:47 pm

    And following your instructions here:
    shacker says:
    March 19, 2010 at 10:14 pm

    I get
    /usr/bin/python
    and
    Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29)

    the only element that isnt mentioned above is that i have mamp on my system, and i was hoping to use its mysql. From what I’ve gathered I understand that its 32bit, so i have to import MySQLdb against a 64bit MySQL installation.

    Any help would be sooooo much appreciated!


  65. ·

    Splendorsine, dk, ash – Because things have changed so much between Leopard and Snow Leopard, please specify which OS you’re on – it matters a lot.

    I haven’t personally encountered the “wrong architecture” problem so can’t really help with that one.

    But I can say this: I upgraded to Snow Leopard on two machines. On one, I had previously existing bindings and libs installed, and things were a big pain. The other was a new machine and I was amazed to find that everything installed and compiled perfectly, with no issues whatsoever. This confirms my suspicion that most of these issues are caused by incompatibilities with previously installed libs and versions, and that your best bet is to back up and then wipe out lingering python libs from older Mac OS version and start fresh.

    Not only that, but the MySQL site now offers an “official” python/mysql binding that worked for me on the new machine on the first try.

    Ash, I’ll bet that applies to you. Trying to keep stuff from MAMP and combine it with new installs could be pretty tricky.

  66. Jimmy Gitonga
    ·

    Splendorsine, dk, ash – I concur with Shacker on this. 2 Weeks ago, my co-Django-ist had on his MacBook Pro, 10.5.x and Py 2.4.x. He jumped through all the hoops for a week, MAMP, MacPorts, MySQL 32bit, then 64 bit … you get the gist.

    We backed up, clean installed 10.6.0, upgraded with 10.6.4 combo and followed the instructions with the latest everything.

    Smooth as butter.

    MAMP s good for PHP/MySQL but for Django its best to install onto the Mac OS installs. Its a good exercise and it prepares you mentally for the next level.

    And that level is the jump from the Development environment to the Production environment which includes virtual servers, “Nix” OSs, web servers, modules, etc.

    Happy Django-ing

  67. Ash
    ·

    Cheers guys, I was wondering the value of MAMP for learning django, as I’ve been using it to learn ruby on rails, but don’t actually do any PHP, so will try without it. I’ll keep you posted. I’ll definitely check out the MySQL site for that binding.


  68. ·

    Thank Shacks and others. @spleach hit the nail on the head for me. I think I have a pretty clean system with standard python install (2.6 universal binary), Xcode installed and mysql.com distribution of MySQL 5.1. Yet I could not get a successful compile.

    # file /usr/bin/python
    /usr/bin/python: Mach-O universal binary with 3 architectures
    /usr/bin/python (for architecture x86_64): Mach-O 64-bit executable x86_64
    /usr/bin/python (for architecture i386): Mach-O executable i386
    /usr/bin/python (for architecture ppc7400): Mach-O executable ppc

    When I tried to compile using the standard configs like ARCHFLAGS='-arch x86_64' python setup.py build I would get an error looking for Python.h.

    pymemcompat.h:10:20: error: Python.h: No such file or directory
    [ snip huge tail ]

    So I modified the include paths for gcc using the CFLAGS env var. The complete recipe was:

    export LDFLAGS=”-L/Developer/SDKs/MacOSX10.6.sdk/usr/lib”
    export CFLAGS=”-I/Developer/SDKS/MacOSX10.6.sdk/usr/include -I/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6″
    export PATH=$PATH:/usr/local/mysql/bin:/Developer/usr/bin
    ARCHFLAGS=’-arch x86_64′ python setup.py build
    ARCHFLAGS=’-arch x86_64′ python setup.py install


  69. ·

    Mandric – You’re on Snow Leopard, right? And you realize that MySQL now provides an official python-mysql binding that should work out-of-the-box?

    IOTW you shouldn’t need to be compiling this anymore – just get all of your manually installed libs out of the way and use the “official” dists – on Snow Leopard you should be good.

  70. spleach
    ·

    @Milan
    Yea I made a typo with what I wrote before, in one of the most critical yet subtle ways, but it looks like you figured it out. You shouldn’t need to add the python library to the CFLAGs environment variable. Python usually picks them up as is.

    Anyway, original line I typo’d:-
    $ export CFLAGS=”-L/usr/include/xtools”
    The L is wrong. Should be:
    export CFLAGS=”-I/usr/include/xtools” # or in your case:
    export CFLAGS=”-I/Developer/SDKS/MacOSX10.6.sdk/usr/include”

    Python should usually find everything it needs. I haven’t had problems with that. You might need to ensure that pymysqldb is import-able from python, and if not find it, and add the directory to the PYTHONPATH environment variable.. Worth a try perhaps. Good luck!

  71. Jim
    ·

    I get an error “bad value (apple) for -march= switch”. What does that mean? I’m running Snow Leopard w/ 32-bit mysql v5.5.

    My terminal says when I build it:
    running build
    running build_py
    copying MySQLdb/release.py -> build/lib.macosx-10.3-fat-2.6/MySQLdb
    running build_ext
    building ‘_mysql’ extension
    creating build/temp.macosx-10.3-fat-2.6
    gcc-4.0 -fno-strict-aliasing -fno-common -dynamic -DNDEBUG -g -O3 -Dversion_info=(1,2,3,’final’,0) -D__version__=1.2.3 -I/usr/local/mysql/include -I/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6 -c _mysql.c -o build/temp.macosx-10.3-fat-2.6/_mysql.o -Os -g -fno-common -fno-strict-aliasing -arch i386
    _mysql.c:1: error: bad value (apple) for -march= switch
    error: command ‘gcc-4.0′ failed with exit status 1

  72. Jim
    ·

    No, I’m running OSX 10.6.6 to be exact. Don’t know why the message says 10.3, but I got the problem solved. :) I needed MySQLdb to run on Eclipse. I had friend help me change the path to on run it and it works. Don’t have a logical reason why it works but it does. Appreciate the concern.


  73. ·

    Ah – I had just noticed 10.3 strings in your output above. But glad you got it solved.

  74. t
    ·

    when i invent the next facebook, i’m coming back to pay you a million dollars for this. thank you sir.

  75. Luca72
    ·

    I get this error any idea?

    Referenced from: /Users/lucabertolotti/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so
    Reason: image not found

    Thanks Luca

  76. Luca72
    ·

    I have solved with :
    export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/:${DYLD_LIBRARY_PATH}

  77. Richard Morrison
    ·

    Superb – hugely grateful for this; it heralded the end of a frustrating couple of hours. THANK YOU!

  78. ruffyleaf
    ·

    This works as of Jun 17 2011 gmt+8

    I’m using osx10.6.7.

    Had to install setuptools and mysql

    Due to my previously using MAMP, I didn’t require mysql to be installed.

    Required to export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/ as well to .profile


  79. ·

    thank you so much after two days of headaches, the solution here solved my problem.

  80. Katie Merth
    ·

    Your update for Snow Leopard finally helped me overcome my hurdles! Thank you for taking the time to share.

    FYI… I’m using an install of mySQL packaged with XAMPP. So, my path to mysql_config in site.cfg is a little different (Applications/XAMPP/xamppfiles/bin/mysql_config). And I had to go back to XAMPP and install the development package to get the development header files.

  81. joely
    ·

    Thank you for this tour-de-force of expository writing! I chickened-out on doing the install – I’m using an SQL Batch file instead – but if I ever get drunk and dangerous and try to install MySQLdb, this is where I’ll start :-)

    “A shipwreck on the beach is a lighthouse to the sea” – Old Dutch Proverb

  82. Jim Tuckers
    ·

    Wow. I am having a beer in your honor right now.
    And then I’m having a beer to thank you for keeping me from smashing my computer.
    Thank you.

  83. daniel
    ·

    great tutorial thanks! i’m able to successfully import MySQLdb but when i try to create a connection i get the following error:

    OperationalError: (2002, “Can’t connect to local MySQL server through socket ‘/opt/local/var/run/mysql5/mysqld.sock’ (2)”)

    any ideas?

    thanks


  84. ·

    Daniel, have you verified that that’s the actual path to mysql.sock on your system? On my Mac it’s:

    ‘HOST': ‘/tmp/mysql.sock’,

  85. daniell
    ·

    Im using mamp so the path should be :

    /Applications/MAMP/tmp/mysql/mysql.sock

    right?

    how do i change the path in order to get it working with python? I tried

    sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock

    but it still won’t connect

    thanks in advance


  86. ·

    I’ve never tried MAMP (that approach has nothing to do with this tutorial), but you should be able to put that path in your Django settings.py, in the db section. You wouldn’t need a symlink then.

  87. daniell
    ·

    Hi, yes you are right sorry to be talking about MAMP, its a bit off topic but I was getting desperate for a solution. I somehow managed to to modify the connection to make it work for the location of the socket so thank you so much for your help!


  88. ·

    This absolutely saved me, mainly the part about copying the macports build of mysqldb, I had been struggling with the i386 that pip was giving me.

    Thank you!

Leave a Reply