How do I get MySQLdb working on Mac OS X?
14 Answers
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 1: Download the latest MySQL for Python adapter from SourceForge.
Step 2: Extract your downloaded package:
tar xzvf MySQL-python-1.2.2.tar.gz
Step 3: Inside the folder, clean the package:
sudo python setup.py clean
COUPLE OF EXTRA STEPS, (from this comment)
Step 3b: Remove everything under your MySQL-python-1.2.2/build/* directory -- don't trust the "python setup.py clean" to do it for you
Step 3c: Remove the egg under Users/$USER/.python-eggs
Step 4: Originally required editing _mysql.c, but is now NO LONGER NECESSARY. MySQLdb community seem to have fixed this bug now.
Step 5: Create a symbolic link under lib to point to a sub-directory called mysql. This is where it looks for during compilation.
sudo ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql
Step 6: Edit the setup_posix.py and change the following
mysql_config.path = "mysql_config"
to
mysql_config.path = "/usr/local/mysql/bin/mysql_config"
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
Step 10:
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:
sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib
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.

- 73
- 1
- 4

- 3,065
- 3
- 21
- 23
-
1Note that the Apple-supplied Python on 10.6 (Snow Leopard) prefers to run 64-bit (it's 32-bit/64-bit universal). There are other pitfalls, too. For a while in the mysql 5.1.x series, the OS X tarballs packaged by mysql were faulty (claimed to be universal but weren't). The data base adapter for Python, MySQLdb, has had its own issues. This is one case where you are well advised to just use MacPorts to build everything you need automatically. It's just too easy for something to go wrong. – Ned Deily Sep 19 '09 at 18:24
-
I'm going to leave this here in case it helps anybody else. I also had to modify site.cfg to set the mysql_config to the right path. I don't think it had a path initially, so was defaulting to /opt/..., but I needed to point it to /usr/local/mysql/... – tchaymore Apr 19 '11 at 16:57
-
8I also had to run: `sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib` – David Underhill Dec 05 '12 at 22:54
-
1I am running on Lion as well, and I had massive problems getting this to work. I finally found this great SO question and answer and followed it 100%. It did not work for me until I added, exactly as David Underhill noted above: `sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib`. Once I ran this, it imported as expected...finally. Wheewwww. Just an fyi for those in a similar position. – Friendly King Feb 07 '13 at 18:03
-
I am also on Lion and had to do the same as David Underhill and JohnZ. It would be nice to update the answer. – joshcartme Mar 25 '13 at 16:53
-
This answer works for me, with a little tweak. If you're using mysql with MAMP, ignore Step 5. In Step 6, the `mysql_config.path` should be `/Applications/MAMP/Library/bin/mysql_config`. Then follow [this guide on how to install mysqldb-python with MAMP](http://dreamconception.com/tech/how-to-install-mysqldb-mysql-python-on-mamp/) from Step 1-3. – Tri Nguyen May 21 '13 at 18:19
-
This worked for me with one change: I needed to perform step 6, mysql_config first in order to get 'clean' to work – Raj Jan 28 '14 at 04:18
-
thannks a lot! that worked on Mac oS lion like a charm – ArisRe82 Feb 11 '14 at 22:30
-
I am installing mysqldb in Mac OSX 10.9.2 , and while I run `sudo python setup.py build`, it is throwing this error:`clang: error: unknown argument: '-mno-fused-madd' [-Wunused-command-line-argument-hard-error-in-future] clang: note: this will be a hard error (cannot be downgraded to a warning) in the future error: command 'cc' failed with exit status 1` And I solve it like this: `sudo ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future python setup.py build ` then `sudo python setup.py install` – jianpx May 06 '14 at 03:45
-
This helped me allot after upgrading to Mavericks... Step 10 was needed to get the mysql server running again! – nelsonvarela Jul 25 '14 at 10:06
-
You top man I managed to get everything into place, only have a mod_wsgi issue with ascii characters to solve now. – AKFourSeven Jan 29 '15 at 16:42
-
2It turns out I had mysql libraries but only not linked properly. Mac OS ElCapitan does not allow linking into /usr/lib. So just this was enough for me: sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/local/lib/libmysqlclient.18.dylib – Maruthi Jul 26 '17 at 07:23
-
worked for me. just step 6 should be before step3. working on mac os Sierra – wizneel Aug 03 '17 at 11:23
A quick and easy way for Mac OS X 10.8 (Mountain Lion), 10.9 (Mavericks), 10.10 (Yosemite), and 10.11 (El Capitan):
I assume you have XCode, its command line tools, Python, and MySQL installed.
Install PIP:
sudo easy_install pip
Edit ~/.profile: (Might not be necessary in Mac OS X 10.10)
nano ~/.profile
Copy and paste the following two line
export PATH=/usr/local/mysql/bin:$PATH export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/
Save and exit. Afterwords execute the following command:
source ~/.profile
Install MySQLdb
sudo pip install MySQL-python
To test if everything works fine just try
python -c "import MySQLdb"
It worked like a charm for me.
If you encounter an error regarding a missing library: Library not loaded: libmysqlclient.18.dylib then you have to symlink it to /usr/lib
like so:
sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

- 20,030
- 7
- 43
- 238

- 12,481
- 10
- 60
- 72
-
2
-
2This worked great for me (Yosemite public beta; Mysql 5; Py2.7), but I needed to do the ``sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib`` command from below. – Michael Scott Asato Cuthbert Aug 17 '14 at 21:42
-
Good answer. I already had other definitions of DYLD_LIBRARY_PATH in my .bash_profile, so I added it this way `export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:/usr/local/mysql/lib/` – beroe Nov 25 '14 at 06:37
-
1Just verified that this works with 10.10.4. Also, using mysql from homebrew, the .profile change was not required to make this work. – Isotopp Jul 09 '15 at 16:13
-
@Isotopp you're right, I think the profile change might only be required in 10.8. Thanks for testing it in 10.10.4! – F Lekschas Jul 09 '15 at 16:27
-
I needed the profile change with 10.10.4 for it to build. Then, still get: "ImportError: dlopen(/Library/Python/2.7/site-packages/_mysql.so, 2): no suitable image found. Did find: /Library/Python/2.7/site-packages/_mysql.so: mach-o, but wrong architecture" – TextGeek Jul 21 '15 at 16:50
-
On Mac OSX 10.10.5, after editing the ~/.profile file, what worked for me was 1.) $brew install py26-mysql 2.) $ pip install MYSQL-python – AdjunctProfessorFalcon Sep 17 '15 at 22:27
-
Awesome answer. Highly recommend not to skip adding the mysql binaries to PATH and DYLD_LIBRARY_PATH, otherwise `pip install` will fail. – Tuxdude Nov 12 '16 at 02:54
-
Perfect - installed Homebrew to get XCode and command line tools via https://coolestguidesontheplanet.com/installing-homebrew-on-os-x-el-capitan-10-11-package-manager-for-unix-apps/ – wbdarby Feb 02 '17 at 21:05
-
-
In my case I got a slightly different error: ImportError: dlopen(/Library/Python/2.7/site-packages/_mysql.so, 2): Library not loaded: @rpath/libssl.1.0.0.dylib If I run ln -s /usr/local/mysql/lib/libssl.1.0.0.dylib /usr/lib/libssl.1.0.0.dylib it says ln: /usr/lib/libssl.1.0.0.dylib: Operation not permitted running OS 10.12.6 – Giacomo Aug 07 '17 at 16:48
-
1After installing MySQL with `brew install mysql`, I didn't have to go through the 2nd step. – dav.garcia Jan 18 '18 at 08:36
-
I just had to update `DYLID_LIBRARY_PATH`. I am using python installed from brew. I am not using the default one that comes with OSX – yaswanth Jun 08 '18 at 10:42
Install mysql and python via Macports The porters have done all the difficult work.
sudo port install py26-mysql
sudo port install mysql5-server
should install what you need. (see Stack overflow for comments re mysql server)
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.
-
3Note that when starting use MacPorts, make sure that your path prefers /opt/local/bin over /usr/bin as MacPorts installs things under /opt/local. – Teemu Kurppa Sep 19 '09 at 12:40
-
4Yes! And skip the second line if you just need to connect to an existing MySQL server. Unfortunately, with the number of dependencies involved, trying to use python with mysql on OS X is one case where it is easier to let MacPorts install an additional python instance rather than trying to play package manager yourself. – Ned Deily Sep 19 '09 at 18:13
-
As a sidenote, if you are getting a deprecation warning, the following page has the fix: https://bugs.launchpad.net/python-mysqldb/+bug/341943 – Jay Taylor Oct 13 '10 at 16:48
-
4FYI, if you just do the first line (with py27-mysql at least), it'll end up installing mysql5 anyway. By downloading and compiling the source. Probably not what you want. Grr. – Ben Hardy Jul 05 '12 at 17:40
-
@BenHardy - it gets all installed which is what you do want and exactly what I meant- also now lion and Snow Leopard executables are centrally built by macports so won't do a local compile in most cases. – mmmmmm Jul 05 '12 at 22:55
-
Note that this installs MySQLdb (import MySQLdb), vs. other mysql connectors. – Joshua Richardson Oct 15 '13 at 17:54
Install pip:
sudo easy_install pip
Install brew:
ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
Install mysql:
brew install mysql
Install MySQLdb
sudo pip install MySQL-python
If you have compilation problems, try editing the ~/.profile file like in one of the answers here.

- 41
- 8

- 3,852
- 3
- 30
- 34
-
-
curl: (22) The requested URL returned error: 404 Not Found – Thirupathi Thangavel Mar 13 '18 at 10:11
-
Here's another step I had to go through, after receiving an error on completing Step 9:
ImportError: dlopen(/Users/rick/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib
sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib
Reference: Thanks! http://ageekstory.blogspot.com/2011_04_01_archive.html

- 27,479
- 9
- 75
- 76

- 1,268
- 15
- 12
As stated on Installing MySQL-python on mac :
pip uninstall MySQL-python
brew install mysql
pip install MySQL-python
Then test it :
python -c "import MySQLdb"

- 3,938
- 2
- 36
- 48
You could try using the pure-python pymysql
:
sudo easy_install pymysql
(Or use pip
if you have it installed.) Then, add this before you import MySQLdb
in your code:
try:
import pymysql
pymysql.install_as_MySQLdb()
except ImportError:
pass

- 14,787
- 6
- 68
- 57
Just had this problem (again!) after getting a new Lion box.
Best solution I've found (still not 100% optimal, but working):
make sure you have 64-bit python. How to check if a library is 32bit/64bit built on Mac OS X?
install easy_install if you don't have it. http://packages.python.org/distribute/easy_install.html
install GCC if you don't have it.
you can get it by downloading XCode/Dev Tools from Apple - this is a big download -
... but instead I recommend this github which has what you need (and does not have XCode): https://github.com/kennethreitz/osx-gcc-installer
I downloaded their prebuilt PKG for lion, https://github.com/downloads/kennethreitz/osx-gcc-installer/GCC-10.7-v2.pkg
make sure you have downloaded a 64-BIT version of MYSQL Community. (The DMG install is an easy route) http://dev.mysql.com/downloads/mysql/
Set paths as follows:
export PATH=$PATH:/usr/local/mysql-XXXX
export DYLD_LIBRARY_PATH = /usr/local/mysql/lib/
export ARCHFLAGS='-arch x86_64'
NOTE THAT:
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.
Best reference: http://activeintelligence.org/blog/archive/mysql-python-aka-mysqldb-on-osx-lion/
Or simple try:
> sudo easy_install MySQL-python
If it gives a error like below:
EnvironmentError: mysql_config not found
, then just run this
> export PATH=$PATH:/usr/local/mysql/bin

- 7,752
- 11
- 48
- 82

- 712
- 8
- 14
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/

- 950
- 2
- 21
- 31
If you are using 64 bit MySQL, using ARCHFLAGS to specify your cpu architecture while building mysql-python libraries would do the trick:
ARCHFLAGS='-arch x86_64' python setup.py build
ARCHFLAGS='-arch x86_64' python setup.py install

- 3,093
- 1
- 22
- 21
export PATH=$PATH:/usr/local/mysql/bin/
should fix the issue for you as the system is not able to find the mysql_config file.

- 4,326
- 32
- 46
On macos Sierra this work for me, where python is managed by anaconda:
anaconda search -t conda mysql-python
anaconda show CEFCA/mysql-python
conda install --channel https://conda.anaconda.org/CEFCA mysql-python
The to use with SQLAlchemy:
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
>>> from sqlalchemy import *
>>>dbengine = create_engine('mysql://....')

- 638
- 13
- 31
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"
Then, finally, run:
pipenv install mysqlclient
and it should install just fine.
Source: https://medium.com/@shandou/pipenv-install-mysqlclient-on-macosx-7c253b0112f2

- 668
- 6
- 12