By using our website, you agree to our privacy policy

Ruddra.com

Install MySQL and MySQLClient(Python) in MacOS

Install MySQL and MySQLClient(Python) in MacOS

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

Then according to mysqlclient’s documentation, you need to put a bugfix at mysql_config. For that first type mysql_config in terminal.:

>> mysql_config
Usage: /usr/local/bin/mysql_config [OPTIONS]
Compiler: ...

It will show where you need to find mysql_config. Then you can use any of the editor of your liking and change the following lines inside the mysql_config:

Change

# on macOS, on or about line 112:
# Create options
libs="-L$pkglibdir"
libs="$libs -l "

To

# Create options
libs="-L$pkglibdir"
libs="$libs -lmysqlclient -lssl -lcrypto"

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/

You need to unlink mysql and link mysql-connector-c:

brew unlink mysql
brew link --overwrite mysql-connector-c

Step seven: install mysqlclient

You should be able to use pip install mysqlclient without any errors.

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: May 27, 2020

  • x8

x8

Share Your Thoughts
M ↓   Markdown

rsunbabu
rsunbabu
Friday, August 2, 2019

The above steps worked when I uninstalled the MySQL dowloaded from https://dev.mysql.com/downloads/file/?id=487977 here

Rebecca Luong
Rebecca Luong
Friday, August 2, 2019

thank you!!

iapilgrim
iapilgrim
Friday, August 2, 2019

Thanks - it works for me

Mark
Mark
Thursday, November 14, 2019

Thank you so much!

Kathy Mather
Kathy Mather
Sunday, November 17, 2019

This worked splendidly. Thanks so much for taking the time to write it out!

Shamal Sandeep
Shamal Sandeep
Thursday, November 21, 2019

Thanks for posting a comprehensive installation guide that actually works.

Michael Barton
Michael Barton
Wednesday, November 27, 2019

You’re my hero. Thank you.

liiy
liiy
Friday, November 29, 2019

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

ruddra
Friday, November 29, 2019

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

Cem
Cem
Sunday, December 1, 2019

It worked for me after adding –force in the step 6 (brew link –overwrite mysql-connector-c)

Willy
Willy
Sunday, December 22, 2019

Thanks worked !

Nicholas Portalupi
Nicholas Portalupi
Friday, March 6, 2020

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.

Ruddra
Friday, March 6, 2020

Hi Nicholas, Hopefully you will find solution in this GitHub issue. You can also use alternative package installers like anaconda.

fullStackChris
fullStackChris
Monday, March 9, 2020

thanks a bunch, this was the first tutorial (after many) that finally worked for me!

Zeo
Zeo
Wednesday, March 18, 2020

Thanks you! It save me lots of time.

Bernardo
Bernardo
Tuesday, March 24, 2020

Thanks a lot!

Bernardo
Bernardo
Tuesday, March 24, 2020

Thanks a lot!

Setiaki
Setiaki
Thursday, March 26, 2020

Thanks man! this is so helpful! May God give u happiness all the time!

M2Golden
M2Golden
Wednesday, April 15, 2020

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’

Arnab Kumar Shil
Wednesday, April 15, 2020

As per mysqlclient documentation, you need to import MySQLdb like this:

from MySQLdb import _mysql
db=_mysql.connect()

# OR

db = MySQLdb.connect()

Sean Conkie
Sean Conkie
Saturday, May 2, 2020

i had to run mysql.start before mysql_secure_installation but other than that an excellent guide, thank you