Setting up MySQL for Python (MySQLdb) on Mac OS X


MySQL is an excellent, very popular and open source database management system. A binary installation is available for Mac OS, and the process of installing MySQL on Mac OS is also very well documented. So just head over to MySQL.com, where you will find the binary installation, together with extensive installation, and usage documentation.

To connect to MySQL from Python use MySQL for Python (MySQLdb). Get MySQLdb from http://sourceforge.net/projects/mysql-python/.

  1. Download MySQL from mysql.com and install the DBMS as instructed in the documentation.

  2. Make sure “Library/Python/2.3”  does not have:
    • Directory “MySQL”
    • File “_mysql.pyd”
    • File “_mysql_exceptions.py”
    • File “_mysql_exceptions.pyc”

              Either uninstall MySQL for Python if a setup program was used to install it, otherwise manually delete them.

  3. Download and unpack MySQL for Python. At the time of writing this is MySQL-python-1.2.1_p2.tar.gz .

  4. Open Terminal and change to the directory where MySQLdb was unpacked to.

  5. Get rid of any previous builds that might interfere, by deleting the “build” directory if it exists.

  6. Edit the setup.py file, and change:  

        return popen(“mysql_config --%s” % what)
                    to          
        return popen(“/usr/local/mysql/bin/mysql_config --%s” % what)

  7. Cleanup any previous install attempts:

      python setup.py clean

  8. Build MySQLdb:

      python setup.py build

Viola! MySQL for Python is all setup, and ready for your Python-MySQL data access code. Enjoy!

 

Comments

# peter said:

one thing to note is that there is apparently an extra 'mysql' in the path that the shared object looks for the .dylib

you may get an error like this--

ImportError: dlopen(./_mysql.so, 2): Library not loaded: /usr/local/mysql/lib/mysql/libmysqlclient_r.15.dylib

 Referenced from: ./_mysql.so

 Reason: image not found

simple solution is to cd into the /usr/local/mysql/bin directory and enter this command:

ln -s /usr/local/mysql/bin mysql

which will make a symlink in the directory to itself and magically make everything available.  (saves the space of copying everything down a level.)

Monday, June 18, 2007 10:45 PM
# Priyesh Jain said:

I tried using changin the setup file in 1.2.2, but could not find the appropriate location and was too lazy for that :P

Thanks a lot though ! worked like a charm with 1.2.1

Cheers

Friday, July 06, 2007 9:33 AM
# Tom said:

The location for changing mysql_config is now in site.cfg, not in the popen command as described above.

Sunday, July 15, 2007 10:21 PM
# Victor said:

The mysql installation from fink doesn't seem to have mysql_config, neither in the client nor server install.

Monday, July 16, 2007 5:16 PM
# Victor said:

Ah. The mysql_config program is in the mysql_dev package.

Monday, July 16, 2007 5:42 PM
# David Preece said:

I've been using MAMP and it does not appear that you need to muck about with the path for mysql_config. However, I still can't get it to build - GCC complains "error: my_config.h: No such file or directory" then it just snowballs from there :(

Thursday, July 19, 2007 6:49 AM
# Łukasz Adamczak said:

I'm also using MAMP and trying to build MySQLdb. Apparently the include files for mysql don't come with MAMP.

mysql-config --include

returns -I/Applications/MAMP/Library/include/mysql, but this path doesn't exist.

Will probably need to install the official version.

Thursday, July 19, 2007 11:15 PM
# Martin Byröd said:

I installed the lastest mysql version from mysql.com and I also cant get it to build. For me the problem is a missing "sys/types.h". Does anyone know what that means?

Tuesday, August 14, 2007 5:29 PM
# Toni Menzel said:

Correction to the "ln -s .." approach metioned about by Peter: there is a typo using bin instead of lib.

So, you have to switch to /usr/local/mysql/lib

create a symlink like this:

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

cheers,

Toni

Tuesday, September 11, 2007 9:11 PM
# Lisa Friedland said:

I made the mistake of upgrading to the newest Python version, 2.5.1, when trying to install mysqldb, and then I had a series of (varying) errors trying to build mysqldb.  Save yourself the trouble!  Use MacPython 2.4, then use the installer for mysqldb found at pythonmac.org/.../index.html.  (Of course, this advice only holds as long as there's no installer for mysqldb under Python 2.5.)

Monday, September 17, 2007 9:28 PM
# Venkatesh said:

---------------

"Correction to the "ln -s .." approach metioned about by Peter: there is a typo using bin instead of lib.

So, you have to switch to /usr/local/mysql/lib

create a symlink like this:

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

-----------------

Thank you Tony and also Peter!! It fixed my problem.

Sunday, September 30, 2007 9:54 AM
# Jakob Løkke Madsen said:

____________________

Correction to the "ln -s .." approach metioned about by Peter: there is a typo using bin instead of lib.

So, you have to switch to /usr/local/mysql/lib

create a symlink like this:

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

cheers,

Toni

_____________

Thanks!!! Now it works for me as well! Wonderful!

Tuesday, October 23, 2007 6:04 PM
# Alessandro Censoni said:

I trie to install mysql-python 1.2.2 but I have a lot of errors. I have install MySQL 5.0.45 and python 2.5.1.I use Mac OS X 10.4.9 and when I trie to build mysqldb i have the following error:

>sh: line 1: mysql_config: command not found

>Traceback (most recent call last):

 >File "setup.py", line 16, in <module>

   >metadata, options = get_config()

 >File "/Users/alessandrocensoni/Desktop/MySQL-python-1.2.2/setup_posix.py", line 43, in get_config

   >libs = mysql_config("libs_r")

 >File "/Users/alessandrocensoni/Desktop/MySQL-python-1.2.2/setup_posix.py", >line 24, in mysql_config

   >raise EnvironmentError, "%s not found" % mysql_config.path

>EnvironmentError: mysql_config not found

If i trie to install the mysqldb, however I have the following error:  

>sh: line 1: /usr/local/bin/mysql_config: No such file or directory

>Traceback (most recent call last):

>File "setup.py", line 16, in <module>

>metadata, options = get_config()

>File "/Users/alessandrocensoni/Desktop/MySQL-python-1.2.2/setup_posix.py", line 43, in get_config

>libs = mysql_config("libs_r")

>File "/Users/alessandrocensoni/Desktop/MySQL-python-1.2.2/setup_posix.py", line 24, in mysql_config

 >raise EnvironmentError, "%s not found" % mysql_config.path

>EnvironmentError: /usr/local/bin/mysql_config not found

Infatti, ho semplicemente 'decommentato' una riga del file dove c'era  mysql_config = /usr/local/bin/mysql_config.

I don't know where I have to do the change suggested in the guide and relative comments. In the site.cfg I don't know what I have to change.  I also trie to edit the setup.py of mysqlpython-1.2.1_p2 but I have the following error:

>File "setup.py", line 26

>    f = popen(“/usr/local/mysql/bin/mysql_config --%s” % what)

>              ^

>SyntaxError: invalid syntax

Can somebody help me?

Thanks a lot

Tuesday, October 30, 2007 11:36 AM
# neoprolog » Mac OS X Leopard (10.5), Django ve Subversion üzerine notlar said:

Pingback from  neoprolog &raquo; Mac OS X Leopard (10.5), Django ve Subversion üzerine notlar

Saturday, November 17, 2007 10:40 PM
# BenDog said:

trying to compile 1.2.2 on osx 10.5.1

with current mysql compiled

i'm getting the following errors.

any ideas?

amelie:MySQL-python-1.2.2 bendog$ sudo python setup.py clean

running clean

removing 'build/temp.macosx-10.5-i386-2.5' (and everything under it)

amelie:MySQL-python-1.2.2 bendog$ sudo 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 -arch i386 -arch ppc -pipe -Dversion_info=(1,2,2,'final',0) -D__version__=1.2.2 -I/usr/local/mysql/include/mysql -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 -fno-omit-frame-pointer

In file included from /usr/local/mysql/include/mysql/mysql.h:43,

                from _mysql.c:40:

/usr/include/sys/types.h:92: error: duplicate ‘unsigned’

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

In file included from /usr/local/mysql/include/mysql/mysql.h:43,

                from _mysql.c:40:

/usr/include/sys/types.h:92: error: duplicate ‘unsigned’

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

lipo: can't open input file: /var/tmp//ccxjJ8sy.out (No such file or directory)

error: command 'gcc' failed with exit status 1

amelie:MySQL-python-1.2.2 bendog$

Thursday, December 20, 2007 12:51 AM
# BenDog said:

i found the solution on this site.

thought i'd let people know if they were having the same issue.

http://www.keningle.com/?p=11

Thursday, December 20, 2007 1:04 AM
# cindy mason said:

For Lisa,

Thanks for a possibly simple solution.  I downloaded the

installer pkg, but it gave me a weird error during install:

When I needed to select my hard drive to do the install, it had a

big red exclamation mark, and it said

You cannot install MySQL-Python 1.2.1_p2 on this volume.

MySQL-Python requires System Python 2.4 to install.

FOOey

Saturday, February 23, 2008 5:42 AM
# zxevil163 said:

PItvAg Hi from Russia!

Sunday, March 16, 2008 11:08 PM
# zxevil170 said:

qfY9Az [url=http://enkhnuha1.110mb.com/]enkhnuha1[/url],

<a href="http://enkhnuha1.110mb.com/">enchnuha</a>.

[url=enkhnuha1.freewebpages.org]enkhnuha1[/url],

<a href="enkhnuha1.freewebpages.org">enchnuha</a>.

Sunday, March 23, 2008 12:16 PM
# dot net installer switches said:

Pingback from  dot net installer switches

Saturday, May 03, 2008 3:05 AM

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: