'Why libmysqlclient.21.dylib is needed on local with only mysql5.7 running?

On my local I've got [email protected] running. And I have an application which relies on mysql, but got interesting error:

    File "/xxx/lib/python3.7/site-packages/MySQLdb/__init__.py", line 18, in <module>
    from . import _mysql
    ImportError: dlopen(/xxx/lib/python3.7/site-packages/MySQLdb/_mysql.cpython-37m-darwin.so, 2): Library not loaded: /usr/local/opt/mysql/lib/libmysqlclient.21.dylib
    Referenced from: /xxx/lib/python3.7/site-packages/MySQLdb/_mysql.cpython-37m-darwin.so
    Reason: image not found
  1. Now only [email protected] runs on my local, so I can see I've got /usr/local/opt/[email protected]/lib/libmysqlclient.20.dylib, but I never have /usr/local/opt/mysql/lib/libmysqlclient.21.dylib. And why is it needed here? libmysqlclient.21.dylib is only provided by mysql8.0+

  2. What is site-packages/MySQLdb doing? Guess connect to my local mysql? Then why it's asking 21.dylib which doesn't exist?



Solution 1:[1]

MySQLdb is a Python 2 module implemented in C, and it links to the libmysqlclient. If you have a binary of MySQLdb that was compiled against a MySQL 8.0 client library, then it would have a fixed reference to libmysqlclient21.dylib, and if that library is not present, that would cause the error you saw.

MySQLdb is also not compatible with Python 3, and I would hope you use Python 3 by now.

You can use PyMySQL as a substitute for MySQLdb. PyMySQL is all-Python, so it should not reference any libmysqlclient.

Alternatively, use MySQL-Connector Python.

Solution 2:[2]

I just ran into the same issue (using python 3.8), and thanks to Bill Karwin's answer was able to fix it by reinstalling mysqlclient with —-no-cache option, to make sure the wheel gets rebuilt:

pip uninstall mysqlclient
pip install mysqlclient —-no-cache

Here is the sequence of events that led to the libmysqlclient.21.dylib error in my case:

  • installed mysql via Homebrew, which installed mysql@8
  • pip-installed a bunch of dependencies for a project, including mysqlclient
  • realized I actually needed [email protected] for the project, downgraded mysql
  • ran the project and saw the error about missing libmysqlclient.21.dylib because the mysqlclient wheel was built while mysql@8 was installed

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bill Karwin
Solution 2 mvpc