MySQL, Python on OS X – Snow Leopard


I was surprised at how much time I have spent on getting MySQL and Python to work on OS X Leopard. Here are the steps that worked for me.

  1. Install MySQL using mac port.
  2. Download the latest python mysqldb, build and install it.

To install latest MySQL (5.1.x) see this entry.

Now let me expand on these steps.

Install MySQL using mac port:

$ sudo port install mysql5
Password:
--->  Computing dependencies for mysql5
--->  Fetching mysql5
--->  Verifying checksum(s) for mysql5
--->  Extracting mysql5
--->  Applying patches to mysql5
--->  Configuring mysql5
--->  Building mysql5
--->  Staging mysql5 into destroot
--->  Installing mysql5 @5.0.86_0
--->  Activating mysql5 @5.0.86_0
--->  Cleaning mysql5

Next install the MySQL system tables:

$ sudo mysql_install_db5 --user=mysql
Installing MySQL system tables...
090926  9:34:59 [Warning] Setting lower_case_table_names=2 because file system for /opt/local/var/db/mysql5/ is case insensitive
OK
Filling help tables...
090926  9:34:59 [Warning] Setting lower_case_table_names=2 because file system for /opt/local/var/db/mysql5/ is case insensitive
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/opt/local/lib/mysql5/bin/mysqladmin -u root password 'new-password'
/opt/local/lib/mysql5/bin/mysqladmin -u root -h sj.local password 'new-password'

Alternatively you can run:
/opt/local/lib/mysql5/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd /opt/local ; /opt/local/lib/mysql5/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd mysql-test ; perl mysql-test-run.pl

Please report any problems with the /opt/local/lib/mysql5/bin/mysqlbug script!

The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com

Make sure that the MySQL data folder is owned by _mysql.

$ cd /opt/local/var/db
$ sudo chown -R _mysql:_mysql mysql5

Create aliases to easily start and stop MySQL. Add these to the ~/.bash_profile file.

alias mysqlstart='sudo /opt/local/bin/mysqld_safe5 --user=mysql&'
alias mysqlstop='sudo /opt/local/bin/mysqladmin5 -u root -p shutdown'

Now start MySQL and set the root password if you wish. Try using mysqlstart and mysqlstop. Add /opt/local/bin to your $PATH. But you must already have done this if you’ve been using mac port.

Check the properties of the _mysql user:

$ dscacheutil -q user -a name _mysql
name: _mysql
password: *
uid: 74
gid: 74
dir: /var/empty
shell: /usr/bin/false
gecos: MySQL Server

If the value of the gid is not the same as the value of uid, fix it so that gid is the same as uid, e.g.

$ dscl . -change /Users/_mysql PrimaryGroupID xx 74

Check to see the folder where the sockets are kept:

$ mysql_config5 --socket
/opt/local/var/run/mysql5/mysqld.sock

If you use the GUI installer for MySQL, the sockets are kept in /tmp. So if you use the MySQL GUI Query Browser or Administrator make sure that you set the location of the socket. Here is a screenshot:

Setting MySQL socket location

Setting MySQL socket location

OK, now you’re ready to install the MySQL Python connector. Download the latest version (1.2.3.c1). Check out this site for the excellent instructions, repeated next.

Build the connector:

$ ARCHFLAGS="-arch x86_64" /usr/bin/python setup.py build
running build
running build_py
copying MySQLdb/release.py -> build/lib.macosx-10.6-universal-2.6/MySQLdb
running build_ext

Now install the connector:

$ sudo /usr/bin/python setup.py install
Password:
running install
running bdist_egg
running egg_info
writing MySQL_python.egg-info/PKG-INFO
writing top-level names to MySQL_python.egg-info/top_level.txt
writing dependency_links to MySQL_python.egg-info/dependency_links.txt
reading manifest file 'MySQL_python.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'MySQL_python.egg-info/SOURCES.txt'
installing library code to build/bdist.macosx-10.6-universal/egg
running install_lib
running build_py
copying MySQLdb/release.py -> build/lib.macosx-10.6-universal-2.6/MySQLdb
running build_ext
creating build/bdist.macosx-10.6-universal/egg
copying build/lib.macosx-10.6-universal-2.6/_mysql.so -> build/bdist.macosx-10.6-universal/egg
copying build/lib.macosx-10.6-universal-2.6/_mysql_exceptions.py -> build/bdist.macosx-10.6-universal/egg
creating build/bdist.macosx-10.6-universal/egg/MySQLdb
copying build/lib.macosx-10.6-universal-2.6/MySQLdb/__init__.py -> build/bdist.macosx-10.6-universal/egg/MySQLdb
copying build/lib.macosx-10.6-universal-2.6/MySQLdb/connections.py -> build/bdist.macosx-10.6-universal/egg/MySQLdb
creating build/bdist.macosx-10.6-universal/egg/MySQLdb/constants
copying build/lib.macosx-10.6-universal-2.6/MySQLdb/constants/__init__.py -> build/bdist.macosx-10.6-universal/egg/MySQLdb/constants
copying build/lib.macosx-10.6-universal-2.6/MySQLdb/constants/CLIENT.py -> build/bdist.macosx-10.6-universal/egg/MySQLdb/constants
copying build/lib.macosx-10.6-universal-2.6/MySQLdb/constants/CR.py -> build/bdist.macosx-10.6-universal/egg/MySQLdb/constants
copying build/lib.macosx-10.6-universal-2.6/MySQLdb/constants/ER.py -> build/bdist.macosx-10.6-universal/egg/MySQLdb/constants
copying build/lib.macosx-10.6-universal-2.6/MySQLdb/constants/FIELD_TYPE.py -> build/bdist.macosx-10.6-universal/egg/MySQLdb/constants
copying build/lib.macosx-10.6-universal-2.6/MySQLdb/constants/FLAG.py -> build/bdist.macosx-10.6-universal/egg/MySQLdb/constants
copying build/lib.macosx-10.6-universal-2.6/MySQLdb/constants/REFRESH.py -> build/bdist.macosx-10.6-universal/egg/MySQLdb/constants
copying build/lib.macosx-10.6-universal-2.6/MySQLdb/converters.py -> build/bdist.macosx-10.6-universal/egg/MySQLdb
copying build/lib.macosx-10.6-universal-2.6/MySQLdb/cursors.py -> build/bdist.macosx-10.6-universal/egg/MySQLdb
copying build/lib.macosx-10.6-universal-2.6/MySQLdb/release.py -> build/bdist.macosx-10.6-universal/egg/MySQLdb
copying build/lib.macosx-10.6-universal-2.6/MySQLdb/times.py -> build/bdist.macosx-10.6-universal/egg/MySQLdb
byte-compiling build/bdist.macosx-10.6-universal/egg/_mysql_exceptions.py to _mysql_exceptions.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/MySQLdb/__init__.py to __init__.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/MySQLdb/connections.py to connections.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/MySQLdb/constants/__init__.py to __init__.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/MySQLdb/constants/CLIENT.py to CLIENT.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/MySQLdb/constants/CR.py to CR.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/MySQLdb/constants/ER.py to ER.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/MySQLdb/constants/FIELD_TYPE.py to FIELD_TYPE.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/MySQLdb/constants/FLAG.py to FLAG.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/MySQLdb/constants/REFRESH.py to REFRESH.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/MySQLdb/converters.py to converters.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/MySQLdb/cursors.py to cursors.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/MySQLdb/release.py to release.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/MySQLdb/times.py to times.pyc
creating stub loader for _mysql.so
byte-compiling build/bdist.macosx-10.6-universal/egg/_mysql.py to _mysql.pyc
creating build/bdist.macosx-10.6-universal/egg/EGG-INFO
copying MySQL_python.egg-info/PKG-INFO -> build/bdist.macosx-10.6-universal/egg/EGG-INFO
copying MySQL_python.egg-info/SOURCES.txt -> build/bdist.macosx-10.6-universal/egg/EGG-INFO
copying MySQL_python.egg-info/dependency_links.txt -> build/bdist.macosx-10.6-universal/egg/EGG-INFO
copying MySQL_python.egg-info/top_level.txt -> build/bdist.macosx-10.6-universal/egg/EGG-INFO
writing build/bdist.macosx-10.6-universal/egg/EGG-INFO/native_libs.txt
zip_safe flag not set; analyzing archive contents...
creating 'dist/MySQL_python-1.2.3c1-py2.6-macosx-10.6-universal.egg' and adding 'build/bdist.macosx-10.6-universal/egg' to it
removing 'build/bdist.macosx-10.6-universal/egg' (and everything under it)
Processing MySQL_python-1.2.3c1-py2.6-macosx-10.6-universal.egg
Removing /Library/Python/2.6/site-packages/MySQL_python-1.2.3c1-py2.6-macosx-10.6-universal.egg
Copying MySQL_python-1.2.3c1-py2.6-macosx-10.6-universal.egg to /Library/Python/2.6/site-packages
MySQL-python 1.2.3c1 is already the active version in easy-install.pth

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

You should now be ready to try out Python with MySQL:

$ python
Python 2.6.2 (r262:71600, Sep 26 2009, 14:46:18)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>>

6 Responses to “MySQL, Python on OS X – Snow Leopard”

  1. Robin Boast Says:

    Dear Shahram,
    Thank you for this guide as I have been having a great deal of trouble installing MySQLdb on my 10.6 mac. I followed you instructions, but that I already have MySQL 5.1.37 installed. Following the last steps to install MySQL Python connector 1.2.3.c1, everything went fine, and I got the messages you show. However when I tried to import MySQLdb in python I got the following error:

    >>> import MySQLdb
    Traceback (most recent call last):
    File “”, line 1, in
    File “build/bdist.macosx-10.6-universal/egg/MySQLdb/__init__.py”, line 19, in
    File “build/bdist.macosx-10.6-universal/egg/_mysql.py”, line 7, in
    File “build/bdist.macosx-10.6-universal/egg/_mysql.py”, line 6, in __bootstrap__
    ImportError: dynamic module does not define init function (init_mysql)

    Could you let me know what I have done wrong?

    Many thanks,

  2. Frode Says:

    Hi.

    Thanks for laying out the process! I’ve spent hours trying to make MySQLdb work, and now it does 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: