• How to install mysqldb (mysql-python) on MAMP

This is instructions how to setup so you can run mysql enabled python apps using MySQL on your MAMP installation. These instructions are written for installation of CMake, PIP, mysql-python (mysqldb), along with XCode command-line tools. Everything will be done in the terminal, except installation of XCode, the command-line tools, CMake and MAMP Components dmg fie.

This will be a bit long, as there is some issues that needs to be dealt with for it to work. It is straight forward though!

Tested on following setup

Mac OS X version: 10.7.5
MAMP version: 2.1.1
MySQL version: 5.5.9

I assume you already have MAMP installed.

1. XCode

Make sure that XCode is installed, and also that the command line tools has been installed (XCode > Preferences > Downloads).

2. Install CMake

We need CMake for compiling MySQL to get the right header files. It is very easy, go to http://www.cmake.org/cmake/resources/software.html and download the Mac OS X dmg file. Mount and install.

2b. Optional Create Backup

We are going to recompile mysql. If you want to be very safe, make a copy for /Applications/MAMP/Library/mysql so you are sure that the database will not be overwritten.

Remember to stop MAMP for running before doing this!

$ tar cf /Applications/MAMP/db/mysql_bak.tar /Applications/MAMP/db/mysql

3. MySQL build

Before we install mysql-python, you will need the header files and dynamic libraries for MySQL. They are not included by standard MAMP, so we will need to download the source, and make all the header files needed.

Download MAMP_components_2.0.2.dmg, and mount it. If you are using another version, take a look here.

$ cd /tmp
$ cp /Volumes/MAMP_components_2.0/mysql-5.5.9.tar.gz .
$ tar xzvf mysql-5.5.9.tar.gz
$ cd mysql-5.5.9

Now if you run CMake with MySQL 5.5.9, you will receive an error message. The config file has an error! Do the following:

$ nano +152 configure.cmake

You will be in line 152. Replace

  LIST(REMOVE_DUPLICATES CMAKE_REQUIRED_LIBRARIES)

with

  IF(CMAKE_REQUIRED_LIBRARIES)
    LIST(REMOVE_DUPLICATES CMAKE_REQUIRED_LIBRARIES)
  ENDIF()

Now run CMake, build the files, and move them to MAMP.

$ cmake . -DMYSQL_UNIX_ADDR=/Applications/MAMP/tmp/mysql/mysql.sock -DCMAKE_INSTALL_PREFIX=/Applications/MAMP/Library
$ make -j 2
$ cp libmysql/*.dylib /Applications/MAMP/Library/lib/
$ mkdir -p /Applications/MAMP/Library/include/mysql
$ cp -R include/* /Applications/MAMP/Library/include/mysql

4. Install PIP

If you haven’t installed PIP yet, do it. It makes installations much easier.

$ sudo easy_install pip

5. Install mysql-python (mysqldb)

Finally, now we can install mysql-python! We will have to include the right path, to install it correctly.

$ sudo env PATH=$PATH:/Applications/MAMP/Library/bin pip install mysql-python

The object file path is by default to the /tmp install folder so we will remove it, add the correct path, and then remove the mysql install directory to make sure that it works.

$ sudo install_name_tool -change /tmp/mysql-5.5.9/libmysql/libmysqlclient.16.dylib /Applications/MAMP/Library/lib/libmysqlclient.16.dylib /Library/Python/2.7/site-packages/_mysql.so
$ rm -R /tmp/mysql-5.5.9

Remember to remove the backup if everything works as it should.

Enjoy!

Sources:
Install necessary lib: http://yjsoon.com/2011/04/linking-mysql-gem-to-mamp
“Performing Test HAVE_PEERCRED - Failed”: https://github.com/mxcl/homebrew/issues/11754

Troubleshooting

You might experience an error similar to the following.

ImportError: dlopen(/Library/Python/2.7/site-packages/_mysql.so, 2): Library not loaded: libmysqlclient.16.dylib
   Referenced from: /Library/Python/2.7/site-packages/_mysql.so
   Reason: image not found

This means that the _mysql.so hasn’t been set with the correct paths. You can verify this by running

$ otool -L /Library/Python/2.7/site-packages/_mysql.so

Take care that you are using the correct paths throughout! In many cases the above commands will show the libmysqlclient dylib without any absolute path. To fix it you do the following:

sudo install_name_tool -change libmysqlclient.16.dylib /Applications/MAMP/Library/lib/libmysqlclient.16.dylib /Library/Python/2.7/site-packages/_mysql.so

Again be aware of the paths. libmysqlclient.16.dylib has to be changed to the exact path shown using otool.

The Author

Dan Schultzer is an active experienced entrepreneur, starting the Being Proactive groups, Dream Conception organization, among other things. You can find him at twitter

Like this post? More from Dan Schultzer

Comments? We would love to hear from you, write us at @dreamconception.