19 Apr 2019, 23:59

Install MySQL and MySQLClient(Python) in MacOS

Connecting to MySQL from Python in MacOS is a very problemtic and painful process. In this post, we are going to see how to install MySQL and connect it from a Python Application using mysqlclient:

Step One: Install Brew

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 Twp: 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 opposit 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.

Related Posts:

---

Something wrong? Suggest an improvement or add a comment (see article history)