Library not loaded: /usr/local/opt/mysql/lib/libmysqlclient.21.dylib and Reason image not found
Asked Answered
M

5

10
Traceback (most recent call last):
  File "/Applications/MAMP/htdocs/Minor Project/xyz.py", line 5, in <module>
    import config
  File "/Applications/MAMP/htdocs/Minor Project/config.py", line 5, in <module>
    import MySQLdb
  File "/Users/brijeshlakkad/Library/Python/2.7/lib/python/site-packages/MySQLdb/__init__.py", line 19, in <module>
    import _mysql
ImportError: dlopen(/Users/brijeshlakkad/Library/Python/2.7/lib/python/site-packages/_mysql.so, 2): Library not loaded: /usr/local/opt/mysql/lib/libmysqlclient.21.dylib
  Referenced from: /Users/brijeshlakkad/Library/Python/2.7/lib/python/site-packages/_mysql.so
  **Reason: image not found**

When i install mysql using "brew install mysql", file will be executed. And when i executed brew install mysql, there are two different mysql both running separately. I executed this file, then al data are being stored in local mysql different from phpmyadmin mysql.

Mayonnaise answered 17/7, 2018 at 6:39 Comment(7)
Are you actually using the long-dead connector MySQL-python, aka MySQLdb? If so, that only works with MySQL 5.x (and was only ever fully tested with 5.0). And are you using it on purpose? Or are you using one of the modern connectors like pymysql or mysqlclient that can be configured to install a drop-in replacement for MySQLdb?Kraigkrait
Meanwhile, I don't understand the second half of your question at all, but if you've installed two different MySQL databases and you're connecting to the wrong one, that's probably an issue with your connect command, but without actually seeing the command you're using, and knowing how you set up the two databases, it's pretty much impossible to even begin to debug that.Kraigkrait
after i executed "brew install mysql", two different mysql are created: 1. Cellar/mysql 2. Mamp mysql phpmyadminMayonnaise
I don't know what "Mamp mysql phpadmin" means. But brew install mysql just installs one copy of MySQL, into Cellar/mysql, and then puts a bunch of symlinks into /usr/local/bin that link to that Cellar.Kraigkrait
pymysql solved my prooblem thank you sir.Mayonnaise
but it gives me Error of : raise errorclass(errno, errval) pymysql.err.OperationalError: (1045, u"Access denied for user 'root'@'localhost' (using password: YES)")Mayonnaise
IIRC, the brew install of MySQL is configured to not allow root connections by default. But I may be wrong— at any rate, that’s a completely different problem, which probably already has good answers here, so you should search for one, or write a new question about it.Kraigkrait
S
12

For me running this command, for my Ruby & Rails project, fixed the issue

gem pristine mysql2
Sitarski answered 5/1, 2024 at 18:6 Comment(1)
The question is about the Python libraryEthel
F
9

I just had this issue because it looks like brew recently updated the current version of mysql to 8.2.

If you update your mysql version, you might get problems installing the mysql2 gem. I think the mysql2 gem needs some upgrades to handle the newer mysql version.

Something like this should get you going if you've upgraded (Ruby & Rails project)

brew install [email protected]

brew link --overwrite [email protected]

Check the version

mysql --version

Should be something like mysql Ver 8.0.35 for macos14.0 on arm64 (Homebrew)

Fordone answered 11/1, 2024 at 18:33 Comment(1)
Same issue but using mysql-client: "brew remove mysql-client", "brew install [email protected]", "brew link --overwrite [email protected]"Cytoplasm
A
8

Upon investigating I found that libmysqlclient.21.dylib is updated to libmysqlclient.23.dylib, and now libmysqlclient.21.dylib is not found. This case was investigated in macOS. Mac did updated mysql and mysql-client and both have libmysqlclient..dylib latest version. To map the current libmysqlclient..dylib uninstall mysqlclient and install new mysqlclient

pip uninstall mysqlclient

pip install mysqlclient
Abrahan answered 20/2, 2024 at 7:3 Comment(0)
K
5

Your problem, in a nutshell, is that you're trying to use the ancient mysql-python library, aka MySQLdb. That library was abandoned around the MySQL 5.1 days, so it doesn't support MySQL 6 and later, and you presumably have MySQL 8.0, so the shared library it's looking for doesn't exist, hence the image not found error.

Homebrew does include a [email protected] package, which I think has a dylib with the right name, which might be old enough to work with mysql-python, if you really want to go that way, but it isn't going to be supported. Or you can download MySQL 5.0 or 5.1 source and build it yourself (there are no Mac binaries of anything older than 5.6 that will run on a current macOS), and that will definitely work.

But a better solution is to use a library that's still maintained. Unfortunately, there are many choices out there, but I get the impression that most people use one of the three big ones:

  • mysqlclient is a fork of mysql-python, which binds to current versions of the C libraries (brew install mysql or download the installer from the MySQL/Oracle website).
  • PyMySQL is a rewrite in pure Python, which doesn't even need C libraries.
  • MySQL Connector/Python is a new implementation written from scratch by MySQL/Oracle. (You probably want the Oracle installer with this one.)

PyMySQL is a much better drop-in replacement than mysqlclient, and it plays nice with everything else in the universe, and it's well-written code that's easy to debug if you have a problem… but it can be pretty slow. And unless you've got a whole mess of existing code to maintain, you usually don't need a perfect drop-in replacement, just something with a pretty-close connect call and support for all of DB-API 2, and mysqlclient covers that.

Kraigkrait answered 17/7, 2018 at 9:10 Comment(0)
I
2

Fix for MacOS 14 - Sonoma

After a lot of searching and a difficult time I was able to find a possible answer for any MacOS user. If you are using Sonoma MacOS 14, please check @nickgoodwind's answer on issue NameError: name '_mysql' is not defined after setting change to mysql where the same error happens

import _mysql
ImportError: dlopen(...)
Improbity answered 3/9, 2023 at 6:15 Comment(3)
I just ran into the same issue after upgrading my MacOS to Sonoma. Running gem pristine mysql2 fixed it for my Ruby on Rails app.Precarious
Thansk @tintin81! That fixed my issue after updating to Sonoma.Saberio
Running "gem pristine mysql2" did NOT fix it for me. Instead i got a "make failed" error trying to build it.Flam

© 2022 - 2025 — McMap. All rights reserved.