0
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.

bfontaine
  • 18,169
  • 13
  • 73
  • 107
Brijesh Lakkad
  • 611
  • 10
  • 13
  • 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`? – abarnert Jul 17 '18 at 06:46
  • 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. – abarnert Jul 17 '18 at 06:47
  • after i executed "brew install mysql", two different mysql are created: 1. Cellar/mysql 2. Mamp mysql phpmyadmin – Brijesh Lakkad Jul 17 '18 at 06:56
  • 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. – abarnert Jul 17 '18 at 07:01
  • pymysql solved my prooblem thank you sir. – Brijesh Lakkad Jul 17 '18 at 07:10
  • but it gives me Error of : raise errorclass(errno, errval) pymysql.err.OperationalError: (1045, u"Access denied for user 'root'@'localhost' (using password: YES)") – Brijesh Lakkad Jul 17 '18 at 07:11
  • 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. – abarnert Jul 17 '18 at 08:38

2 Answers2

3

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 mysql@5.5 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.

abarnert
  • 354,177
  • 51
  • 601
  • 671
0

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(...)