Install MySQL and MySQLClient (Python) in MacOS
Apr 19, 2019 · 2 Min Read · 27 Likes · 32 Comments
Connecting to MySQL from Python in MacOS is a very problematic and painful process. In this post, we are going to see how to install MySQL and connect a Python application to it using mysqlclient.
Step one: install Homebrew
You need to install Homebrew in you local machine. You can do it by:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Step two: install MySQL
Using Homebrew you can install mysql simply by:
brew install mysql
Then setup the credentials in MySQL server using the following command:
mysql_secure_installation
Finally if you want to start at login and as a background service, run this:
brew services start mysql
Else
mysql.server start
Step three: install MySQL-Connector-C
For connecting any other application to MySQL, you need to install a connector. You can do it like this:
brew install mysql-connector-c
Step four: install XCode-Select
You can do this by:
xcode-select --install
Step five: install OpenSSL
Please run the following command:
brew install openssl
Then add its path to environment using following line:
export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/
Step six: unlink MySQL and link MySQL-Connector-C
You need to unlink mysql and link mysql-connector-c:
brew unlink mysql
brew link --overwrite mysql-connector-c --force
Step seven: install mysqlclient
You should be able to use pip install mysqlclient
without any errors.
Step eight: link MySQL back again
Now, you need to do the opposite of Step six:
brew unlink mysql-connector-c
brew link --overwrite mysql
Hopefully now mysqlclient
should work fine and will have no problem connecting your application from mysql to python.
Step nine: add mysqlclient in virtual env(optional)
if you have mysqlclient installed in the system, then you can use --system-site-packages
when creating virtual environment:
python3 -m venv venv_name --system-site-packages
Or
virtualenv venv_name --system-site-packages
But, if you do not want to include all the system packages, and you still face error installing mysqlclient
in virtual environment, then try following steps:
source venv/bin/activate
brew unlink mysql
brew link --overwrite mysql-connector-c
pip install mysqlclient
brew unlink mysql-connector-c
brew link --overwrite mysql
In conclusion
In this article, we have seen installing MySQL from Homebrew. I never used MySQL installed from Oracle website, so don’t know how to fix it. Better if you uninstall that and install MySQL from scratch using Homebrew.
Last updated: Mar 07, 2025
The above steps worked when I uninstalled the MySQL dowloaded from https://dev.mysql.com/downloads/file/?id=487977 here
thank you!!
Thanks - it works for me
Thank you so much!
This worked splendidly. Thanks so much for taking the time to write it out!
Thanks for posting a comprehensive installation guide that actually works.
You’re my hero. Thank you.
I can install mysqlclient success according your step But when I excute “pip install mysql-python " it show _mysql.c:44:10: fatal error: ‘my_config.h’ file not found #include “my_config.h” how can i fix it ? Thank you
Probably you need to downgrade your MySQL to make it work. More information can be found in this SO link: https://stackoverflow.com/a/52262477/2696165
It worked for me after adding –force in the step 6 (brew link –overwrite mysql-connector-c)
Thanks worked !
Please excuse my n00bness in advance. I followed the steps up until step 7, when I get the following error:
There was a problem confirming the ssl certificate: HTTPSConnectionPool(host=‘pypi.org’, port=443): Max retries exceeded with url: /simple/pycrypto/ (Caused by SSLError(“Can’t connect to HTTPS URL because the SSL module is not available.”)) - skipping
Also, my jupyter notebooks don’t work now. I won’t distract from my main question with that error at this time though. Thank you for posting this fix, and hopefully someone can help me fix the fix.
Hi Nicholas, Hopefully you will find solution in this GitHub issue. You can also use alternative package installers like anaconda.
thanks a bunch, this was the first tutorial (after many) that finally worked for me!
Thanks you! It save me lots of time.
Thanks a lot!
Thanks a lot!
Thanks man! this is so helpful! May God give u happiness all the time!
I did all of the above, and also downloaded MySQLWorkbench.app. After running a file with code line: import mysql.connector in python, I am getting the following error: import mysql.connector ModuleNotFoundError: No module named ‘mysql’
As per mysqlclient documentation, you need to import
MySQLdb
like this:i had to run mysql.start before mysql_secure_installation but other than that an excellent guide, thank you
I can’t thank you enough! I was tearing my hair out over this!
Only extra was adding –force in step 6 (which the error messages pretty much told me to do and is mentioned by one of the other commenters)
You are welcome 😄 and thanks for the information, I will update the blog accordingly.
Running Step 9 in my virtual environment somehow stopped pip from working (just within the virtual environment, pip works when I deactivate the environment). For example when I run ‘pip freeze’ in my existing venv, I get the error:
ModuleNotFoundError: No module named ‘pip’
Any idea why this happened or how I can fix this?
You can use ‘get_pip.py’ for installing pip. This article might help.
Please Help me I facing a lot of trouble while installing mysql using brew. I always run the command ’ brew install mysql ‘,
but it gives me an error :
‘Warning: No available formula with the name “mysql”. ==> Searching for similarly named formulae… Error: No similarly named formulae found. ==> Searching for a previously deleted formula (in the last month)… Error: No previously deleted formula found. ==> Searching taps on GitHub… Error: No formulae found in taps.’
Probably you should run
brew update
first to see if the homebrew repositories are updated. Also check your internet connection and proxy setup as well.I have a problem After applied step # 6 carlosinfante@Air-van-Carlos ~ % brew unlink mysql brew link –overwrite mysql-connector-c –force Unlinking /usr/local/Cellar/mysql/8.0.32… ==> Downloading https://formulae.brew.sh/api/formula.json ######################################################################## 100.0% 0 symlinks removed. ==> Downloading https://formulae.brew.sh/api/cask.json
Error: No such keg: /usr/local/Cellar/mysql-connector-c carlosinfante@Air-van-Carlos ~ %
I think either you did not install
mysql-connector-c
or installed a versioned package such asmysql-connector-c@version-number
. In the first, please see if you have installed the connector, otherwise install it using brew. For the second case, I think this Stack Overflow answer might help: https://stackoverflow.com/a/54367240/2696165. If neither of them helps, then I suggest clean up the system by uninstalling and pruning mysql/mysql-connector-c and initiate the mysql installation process from the beginning. This Stack Overflow answer might help regarding cleanup: https://stackoverflow.com/questions/28225045/no-such-keg-usr-local-cellar-git (instead of git, it will be mysql for your case). Thanks.Thanks very much for these instructions. Unfortunately mysql-connector-c is not valid anymore:
It hs bee replaced by mysql-client apparently: https://formulae.brew.sh/formula/mysql-client#default if I replace mysql-connector-c by mysql-client in the instructions above then trying to pip install mysqlclient fails with the following error
Any idea how I could solve this? Thanks in advance for your help
I found a solution here: https://github.com/PyMySQL/mysqlclient/discussions/624#discussioncomment-6277044
The problem comes from mysqlclient v2.2.0 which now requires pkg-config. I finally solved the issue by replacing
mysql-connector-c
withmysql-client
and installing pkg-config along with it:brew install mysql-client pkg-config
Then using:
export PKG_CONFIG_PATH="/usr/local/opt/mysql-client/lib/pkgconfig
Now
pip install mysqlclient
works straight away without unlinking/linking mysql.Hi Mickael, thank you for looking into this and providing a solution. I will investigate a little bit and update the content accordingly.