Tag Archives: mamp

Access MAMP’s MySQL database from Python

I posted a while ago about running a locally hosted server using MAMP, and I have been successfully using a PHP script on this server to read and write to a database.

Then a friend suggested I could use Python instead of PHP, e.g. using “CherryPy” as a light-weight server.  I am a big fan of python, so I want to try this.

The basic overview is:

  • Use the CherryPy python library to run the server (though more recently I have switched to Django)
  • Use the MySQLdb python library to interface with MySQL
  • Use the MAMP-installed instance of MySQL

Here’s how I made it work:

    • Download CherryPy (or Django or any other Python-based framework) from the link above.  I had to use sudo in front of the python setup.py build command.
    • Download MySQLdb from the link above.
    • Check it works by going to the terminal, typing python and then import MySQLdb
    • It doesn’t work, for several reasons:
      • It can’t find the “mysql_config” file associated with MAMP.  To fix this, open site.cfg (in the directory you installed MySQLdb) and add the line below. Note this points to the MAMP installation of my_sql, not the default of /usr/local/bin:
          mysql_config = /Applications/MAMP/Library/bin/mysql_config
      • It couldn’t find llvm. To solve this, open XCode, go to Preferences, choose the Components tab, and install the command line tools.
      • MAMP does not include the required .h files.  This post describes how to get around this: I downloaded the 64bit MySQL from here, and copied files from its include directory into the new directory MAMP/Library/include, and from its lib directory into MAMP/Library/lib. (Don’t use the 32bit files.)
      • The final magic touch is you need to type
          export DYLD_LIBRARY_PATH=/Applications/MAMP/Library/lib
        into the terminal before you go into python. (But note – this will mess up git.  So when you need git, type export DYLD_LIBRARY_PATH=''. Urrggh!)
    • Now both import MySQLdb and import _mysql work in python.
    • To access MySQL from the terminal, open MAMP and start the servers.  Then type
        /Applications/MAMP/Library/bin/mysql --host localhost -uroot -proot
      You can now do things like show databases; to look at the databases you have available.
    • So far so good, but when I try to connect to a database, I got the error “Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2)” (error number 2002).  The solution is nicely explained in this post, and is:
        sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock
    • Strangely, when I came back to this a week later, I got an error: “Library not loaded: libmysqlclient.18.dylib”.  I found this works:
        sudo ln -s /Applications/MAMP/Library/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib
      I also found I had to repeat the previous point to recreate the /tmp/mysql.sock file.

To summarise the on-going usage:

  • Before using python, you need to type export DYLD_LIBRARY_PATH=/Applications/MAMP/Library/lib
  • In order to use MySQL in the terminal, you need to type /Applications/MAMP/Library/bin/mysql --host -localhost -uroot -proot

In Django, you will need to use the following DATABASE values in settings.py:

    'HOST': '/Applications/MAMP/tmp/mysql/mysql.sock',
    'PORT': '8888',

One more thing. When I write apps that interface with the MAMP localhost, I need to replace localhost with the local IP address (from the Mac Network Utility program), and append port :8888.  However the CherryPy localhost has the local IP address (and  :8080). Why are the two local IP addresses different?

From here:

Any thoughts?


Phone gap on a locally hosted server

Mark and I worked together today, and the aim was to get the camera working in the app, using code on the server.

  • Putting the phone gap javascript libraries (cordova_1.6.0.js) on the server was no problem.  We created a file camera.js on the server too and included it using the <script … src=’camera.js’></script> construct. We also had to remove all javascript from the index.html file in Xcode – somehow the code on the server was calling those routines in preference to the same-named routines on the server.  The on-success routine was getting properly called at one point in the day but somehow this stopped working by the end of the day.  We also have yet to work out how to use the resulting image. Any ideas?
  • I learned that Chrome has a “javascript console” which comes in handy for debugging!  Does Safari have one?

We also decided to host a local version of the server on my Mac, to streamline testing.  It was getting a bit cumbersome to use Filezilla to transfer changes to the server code back to the server every time we wanted to test a bug.  This was pretty tricky to get working with phone gap, so here’s a summary of how we did it:

  • We downloaded MAMP (Macintosh, Apache, PHP and MySQL packaged together).
  • We then needed to transfer both the server web pages and the server database to my Mac, and then point the local copy of the web pages and the phone gap shell to the local host.
  • Go to the server’s phpMyAdmin and export the relevant database.  We found that gzipping it did not work, but zipping it did.
  • On the MAMP local host homepage (localhost:8888) there is a link to phpMyAdmin, where you can import the above file.
  • MAMP looks for the webpages at Applications/MAMP/htdocs by default.  We chose to put all our local copy of the server webpages there (though in retrospect I should probably have put them in a subdirectory), and then add a new project in Eclipse using existing files in that directory (though the workspace remains elsewhere).
  • Note that files that start with a dot, like .htaccess, do not display by default in the Finder, so when copying files around, they can get left behind. I needed to recopy .htaccess into the htdocs directory.
  • In our local webpages, we changed the mysql_connect statement to use localhost:8889, with user root and password root.  Obviously we will not put this change on the server.
  • To run this on a physical device (whether it’s a phone gap app or a regular objective-c app), we cannot refer to ‘localhost’ but the actual local IP address. We looked this up in the terminal (although it is also in the System Preferences -> Network, or you can search the Mac for Network Utilities – it is not the same as you find at whatismyip.com) and added it to the whitelist, and referred to it in the source code instead of racingtadpole.com.  To complicate matters, this address has changed for me over time.

Voila!  Dinner is ready, so I must end there.