MySQL Connector C API (libmysqlclient) install and configure in MacOS

UPDATE 1

The latest version of mysql-connector-c++@1.1 you might don’t need to go Configure mysql_config section as I see that is already correctly configured.

But one file “my_config.h“ is missing, you will feel that when you are going to install “MySQL-Python“. Solve that issue I manually create a file named “my_config.h” at /usr/local/opt/mysql-client/include/mysql [that is my local machine location, you might have in different place] and put contents from here

Preface

I am the mad (maybe fool) guy who doesn’t prefer to install services (like MySQL, PostgreSQL and so on) into development machine, instead using docker containers, this way gives me a lot of flexibilities, for example, I could have multiple versions of MySQL servers (each version could be project-specific depends on requirements), besides no unnecessary processes are running in my development machine.

This writing for those concerns who don’t have MySQL server installed into MacOS powered development machine (like MacBook Pro) but need to work with MySQL connection through programming languages (i.e Python in my case). Bellows are focused on ´MySQL-Python´, ´mysqlclient´ installation problems and solutions. If you have MySQL server installed through Homebrew, we don’t expect you are facing any connection related problem.

Prerequisite

As a requirement of my solution, I assume you already have Homebrew installed on your machine, other than going to the official site about to installl it, should be pretty easy.

OpenSSL

Make sure you have OpenSSL installed and the path is added in bash and/or zsh profile. Try (which openssl) to check if already available. Other than installed and configure it.

brew install openssl

echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile 

echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.zshrc (if you are using zsh)

Install and Configure MySQL connector C API

Install through Homebrew

brew install mysql-connector-c

Configure mysql_config

  1. Find the file location by using the command ls -la `which mysql_config`
  2. Open the file with sudo privileges by any editor.
  3. Find #Create options and comment in libs="$libs -l "
  4. Add libs="$libs -lmysqlclient -lssl -lcrypto" and save

Update bash and/or zsh profile

echo 'export LDFLAGS="-L/usr/local/opt/openssl/lib"' >> ~/.bash_profile 

echo 'export CPPFLAGS="-I/usr/local/opt/openssl/include"' >> ~/.bash_profile 


# if you have zsh
echo 'export LDFLAGS="-L/usr/local/opt/openssl/lib"' >> ~/.zshrc 

echo 'export CPPFLAGS="-I/usr/local/opt/openssl/include"' >> ~/.zshrc

Some Common errors could be solved

Collecting mysqlclient   Using cached 
https://files.pythonhosted.org/packages/ec/fd/83329b9d3e14f7344d1cb31f128e6dbba70c5975c9e57896815dbb1988ad/mysqlclient-1.3.13.tar.gz
     Complete output from command python setup.py egg_info:     
Traceback (most recent call last):       File "<string>", line 1, in <module>       
File "/private/var/folders/h3/sff7td1d6pg5v5qsm5xf31q80000gn/T/pip-install-ki9z7ln9/mysqlclient/setup.py", line 18, in <module>         metadata, options = get_config()       

File "/private/var/folders/h3/sff7td1d6pg5v5qsm5xf31q80000gn/T/pip-install-ki9z7ln9/mysqlclient/setup_posix.py", line 60, in get_config         libraries = [dequote(i[2:]) for i in libs if i.startswith('-l')]       
File "/private/var/folders/h3/sff7td1d6pg5v5qsm5xf31q80000gn/T/pip-install-ki9z7ln9/mysqlclient/setup_posix.py", line 60, in <listcomp>         libraries = [dequote(i[2:]) for i in libs if i.startswith('-l')]       
File "/private/var/folders/h3/sff7td1d6pg5v5qsm5xf31q80000gn/T/pip-install-ki9z7ln9/mysqlclient/setup_posix.py", line 13, in dequote         
raise Exception("Wrong MySQL configuration: maybe https://bugs.mysql.com/bug.php?id=86971 ?")     
Exception: Wrong MySQL configuration: maybe https://bugs.mysql.com/bug.php?id=86971 ?

References

  1. MySQLClient instal error: “raise Exception(”Wrong MySQL configuration: maybe https://bugs.mysql.com/bug.php?id”
  2. pip3 install mysqlclient fails on macOS
  3. MySQL Bug #86971 mysql_config –libs of mysql-connector/c 6.1.0 on macOS is wrong