If you only need to connect to mysql and not run a server then the first line is sufficient.
Macports now (early 2013) will provide binary downloads for common combinations of OS a executable architecture, for others (and if you request it) it will build from source.
In general macports (or fink) help when there are complex libraries etc that need to be installed.
Python only code and if simple C dependencies can be set up via setuptools etc, but it begins to get complex if you mix the two.
Update for those using Python3:
You can simply use conda install mysqlclient to install the libraries required to use MySQLdb as it currently exists. The following SO question was a helpful clue: Python 3 ImportError: No module named 'ConfigParser' . Installing mysqlclient will install mysqlclient, mysql-connector, and llvmdev (at least, it installed these 3 libraries on my machine).
Here is the tale of my rambling experience with this problem. Would love to see it edited or generalised if you have better experience of the issue... apply a bit of that SO magic.
Note: Comments in next paragraph applied to Snow Leopard, but not to Lion, which appears to require 64-bit MySQL
First off, the author (still?) of MySQLdb says here that one of the most pernicious problems is that OS X comes installed with a 32 bit version of Python, but most average joes (myself included) probably jump to install the 64 bit version of MySQL. Bad move... remove the 64 bit version if you have installed it (instructions on this fiddly task are available on SO here), then download and install the 32 bit version (package here)
There are numerous step-by-steps on how to build and install the MySQLdb libraries. They often have subtle differences. This seemed the most popular to me, and provided the working solution. I've reproduced it with a couple of edits below
Step 0:
Before I start, I assume that you have MySQL, Python, and GCC installed on the mac.
Step 7:
In the same directory, rebuild your package (ignore the warnings that comes with it)
sudo python setup.py build
Step 8:
Install the package and you are done.
sudo python setup.py install
Step 9:
Test if it's working. It works if you can import MySQLdb.
python
>>> import MySQLdb
If upon trying to import you receive an error complaining that Library not loaded: libmysqlclient.18.dylib ending with: Reason: image not found you need to create one additional symlink which is:
You should then be able to import MySQLdb without any errors.
One final hiccup though is that if you start Python from the build directory you will get this error:
/Library/Python/2.5/site-packages/MySQL_python-1.2.3c1-py2.5-macosx-10.5-i386.egg/_mysql.py:3: UserWarning: Module _mysql was already imported from /Library/Python/2.5/site-packages/MySQL_python-1.2.3c1-py2.5-macosx-10.5-i386.egg/_mysql.pyc, but XXXX/MySQL-python-1.2.3c1 is being added to sys.path
This is pretty easy to Google, but to save you the trouble you will end up here (or maybe not... not a particularly future-proof URL) and figure out that you need to cd .. out of build directory and the error should disappear.
As I wrote at the top, I'd love to see this answer generalised, as there are numerous other specific experiences of this horrible problem out there. Edit away, or provide your own, better answer.
1 in mysql-XXXX above, XXX is the specific version you downloaded. (Probably /usr/local/mysql/ would also work since this is most likely an alias to the same, but I won't pretend to know your setup)
2 I have seen it suggested that ARCHFLAGS be set to '-arch i386 -arch x86_64' but specifying only x86_64 seemed to work better for me. (I can think of some reasons for this but they are not strictly relevant).
Install the beast!
easy_install MySQL-python
LAST STEP:
Permanently add the DYLD_LIBRARY_PATH!
You can add it to your bash_profile or similar.
This was the missing step for me, without which my system continued to insist on various errors finding _mysql.so and so on.
export DYLD_LIBRARY_PATH = /usr/local/mysql/lib/
@richard-boardman, just noticed your soft link solution, which may in effect be doing the same thing my PATH solution does...folks, whatever works best for you.
Python 2.7.13 |Continuum Analytics, Inc.| (default, Dec 20 2016, 23:05:08)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
This answer is an update, circa November 2019. None of the popular pip installs will give you a working setup on MacOS 10.13 (and likely other versions as well). Here is a simple way that I got things working:
brew install mysql
pip install mysqlclient
If you need help installing brew, see this site: https://brew.sh/
I ran into this issue and found that mysqlclient needs to know where to find openssl, and OSX hides this by default.
Locate openssl with brew info openssl, and then add the path to your openssl bin to your PATH:
export PATH="/usr/local/opt/openssl/bin:$PATH"
I recommend adding that to your .zshrc or .bashrc so you don't need to worry about it in the future. Then, with that variable exported (which may meed closing and re-opening your bash session), add two more env variables:
# in terminal
export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"