当从Django默认使用的默认SQLite更改为MySQL数据库时,我在尝试运行python manage.py migrate --database mysql
时遇到了这个棘手的错误:
Traceback (most recent call last):
File "/Users/xoadra/Programming/Syntax/Python/Environments/pfitzer/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 15, in <module>
import MySQLdb as Database
File "/Users/xoadra/Programming/Syntax/Python/Environments/pfitzer/lib/python3.7/site-packages/MySQLdb/__init__.py", line 18, in <module>
import _mysql
ImportError: dlopen(/Users/xoadra/Programming/Syntax/Python/Environments/pfitzer/lib/python3.7/site-packages/_mysql.cpython-37m-darwin.so, 2): Library not loaded: @rpath/libmysqlclient.21.dylib
Referenced from: /Users/xoadra/Programming/Syntax/Python/Environments/pfitzer/lib/python3.7/site-packages/_mysql.cpython-37m-darwin.so
Reason: image not found
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 20, in <module>
execute_from_command_line( sys.argv )
File "/Users/xoadra/Programming/Syntax/Python/Environments/pfitzer/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/Users/xoadra/Programming/Syntax/Python/Environments/pfitzer/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/xoadra/Programming/Syntax/Python/Environments/pfitzer/lib/python3.7/site-packages/django/core/management/base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/xoadra/Programming/Syntax/Python/Environments/pfitzer/lib/python3.7/site-packages/django/core/management/base.py", line 350, in execute
self.check()
File "/Users/xoadra/Programming/Syntax/Python/Environments/pfitzer/lib/python3.7/site-packages/django/core/management/base.py", line 379, in check
include_deployment_checks=include_deployment_checks,
File "/Users/xoadra/Programming/Syntax/Python/Environments/pfitzer/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 59, in _run_checks
issues = run_checks(tags=[Tags.database])
File "/Users/xoadra/Programming/Syntax/Python/Environments/pfitzer/lib/python3.7/site-packages/django/core/checks/registry.py", line 71, in run_checks
new_errors = check(app_configs=app_configs)
File "/Users/xoadra/Programming/Syntax/Python/Environments/pfitzer/lib/python3.7/site-packages/django/core/checks/database.py", line 9, in check_database_backends
for conn in connections.all():
File "/Users/xoadra/Programming/Syntax/Python/Environments/pfitzer/lib/python3.7/site-packages/django/db/utils.py", line 217, in all
return [self[alias] for alias in self]
File "/Users/xoadra/Programming/Syntax/Python/Environments/pfitzer/lib/python3.7/site-packages/django/db/utils.py", line 217, in <listcomp>
return [self[alias] for alias in self]
File "/Users/xoadra/Programming/Syntax/Python/Environments/pfitzer/lib/python3.7/site-packages/django/db/utils.py", line 202, in __getitem__
backend = load_backend(db['ENGINE'])
File "/Users/xoadra/Programming/Syntax/Python/Environments/pfitzer/lib/python3.7/site-packages/django/db/utils.py", line 110, in load_backend
return import_module('%s.base' % backend_name)
File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Users/xoadra/Programming/Syntax/Python/Environments/pfitzer/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 20, in <module>
) from err
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?
我已经通过pip install mysqlclient
通过它安装了mysqlclient并且通过brew install mysql-connector-c
通过Homebrew安装了它的连接器,所以收到了这个错误,所以这是一个令人费解的错误。通过修改连接器的bin文件夹中的mysql_config
文件Even after following the connector instructions,我仍然遇到了这个问题。鉴于此连接器的已知错误的文档,这似乎与我正在运行macOS至少有某种联系。
进一步检查堆栈跟踪信息时,我注意到顶部附近出现Library not loaded: @rpath/libmysqlclient.21.dylib
错误。不久之后,我发现了另一个可能的线索:Reason: image not found
,但我不太确定那是什么意思。
我确实在/usr/local/Cellar/mysql-connector-c
中四处查看,以确定我安装的连接器是否可能丢失任何东西,而且可以肯定的是,libmysqlclient.21.dylib
文件丢失了。我假设它是文件的较早版本,因为它名为libmysqlclient.18.dylib
。出于好奇,我复制了MySQL安装随附的.dylib
,并将该副本以相同的名称移动到连接器的文件夹和/usr/local/lib
中,在这里我还找到了另一个libmysqlclient.18.dylib
文件。但是,我的迁移仍然失败,但是我得到了稍微不同的堆栈跟踪:
Traceback (most recent call last):
File "/Users/xoadra/Programming/Syntax/Python/Environments/pfitzer/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 15, in <module>
import MySQLdb as Database
File "/Users/xoadra/Programming/Syntax/Python/Environments/pfitzer/lib/python3.7/site-packages/MySQLdb/__init__.py", line 18, in <module>
import _mysql
ImportError: dlopen(/Users/xoadra/Programming/Syntax/Python/Environments/pfitzer/lib/python3.7/site-packages/_mysql.cpython-37m-darwin.so, 2): Library not loaded: @rpath/libmysqlclient.21.dylib
Referenced from: /Users/xoadra/Programming/Syntax/Python/Environments/pfitzer/lib/python3.7/site-packages/_mysql.cpython-37m-darwin.so
Reason: no suitable image found. Did find:
/usr/local/lib/libmysqlclient.21.dylib: file too short
/usr/local/lib/libmysqlclient.21.dylib: file too short
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 20, in <module>
execute_from_command_line( sys.argv )
File "/Users/xoadra/Programming/Syntax/Python/Environments/pfitzer/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/Users/xoadra/Programming/Syntax/Python/Environments/pfitzer/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/xoadra/Programming/Syntax/Python/Environments/pfitzer/lib/python3.7/site-packages/django/core/management/base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/xoadra/Programming/Syntax/Python/Environments/pfitzer/lib/python3.7/site-packages/django/core/management/base.py", line 350, in execute
self.check()
File "/Users/xoadra/Programming/Syntax/Python/Environments/pfitzer/lib/python3.7/site-packages/django/core/management/base.py", line 379, in check
include_deployment_checks=include_deployment_checks,
File "/Users/xoadra/Programming/Syntax/Python/Environments/pfitzer/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 59, in _run_checks
issues = run_checks(tags=[Tags.database])
File "/Users/xoadra/Programming/Syntax/Python/Environments/pfitzer/lib/python3.7/site-packages/django/core/checks/registry.py", line 71, in run_checks
new_errors = check(app_configs=app_configs)
File "/Users/xoadra/Programming/Syntax/Python/Environments/pfitzer/lib/python3.7/site-packages/django/core/checks/database.py", line 9, in check_database_backends
for conn in connections.all():
File "/Users/xoadra/Programming/Syntax/Python/Environments/pfitzer/lib/python3.7/site-packages/django/db/utils.py", line 217, in all
return [self[alias] for alias in self]
File "/Users/xoadra/Programming/Syntax/Python/Environments/pfitzer/lib/python3.7/site-packages/django/db/utils.py", line 217, in <listcomp>
return [self[alias] for alias in self]
File "/Users/xoadra/Programming/Syntax/Python/Environments/pfitzer/lib/python3.7/site-packages/django/db/utils.py", line 202, in __getitem__
backend = load_backend(db['ENGINE'])
File "/Users/xoadra/Programming/Syntax/Python/Environments/pfitzer/lib/python3.7/site-packages/django/db/utils.py", line 110, in load_backend
return import_module('%s.base' % backend_name)
File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Users/xoadra/Programming/Syntax/Python/Environments/pfitzer/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 20, in <module>
) from err
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?
这次我得到以下错误:
Reason: no suitable image found. Did find:
/usr/local/lib/libmysqlclient.21.dylib: file too short
/usr/local/lib/libmysqlclient.21.dylib: file too short
因此,我怀疑问题可能与安装我的MySQL连接器的.dylib
版本有关,但我不确定如何确切地解决此问题。我确实很好奇,只是简单地复制文件就可以稍微改变我的堆栈跟踪记录,所以我想我应该对此进行更多研究。有想法吗?
答案 0 :(得分:4)
我最近在尝试在MacBook Pro(MacOS Catallina)上安装Django和mod_wsgi时遇到此问题。设置LD_LIBRARY_PATH,DYLD_LIBRARY_PATH,添加符号链接并将libmysqlclient.21.dylib移至/ usr / lib /无效。 我不得不奋斗了几天,但最终我得到了这个解决方案。问题是我必须修改 libmysqlclient.21.dylib 中的库路径。幸运的是,我们有一个工具可以做到这一点。罪魁祸首是 @ rpath / libmysqlclient.21.dylib 。 看来 @rpath 无效。
好的,您可以使用 otool 检查.so文件中引用的路径。它带有Xcode。这是在mysql .so文件(应在站点软件包下的MySQLdb目录中)上运行它时得到的。
$ otool -L _mysql.cpython-38-darwin.so
_mysql.cpython-38-darwin.so:
@rpath/libmysqlclient.21.dylib (compatibility version 21.0.0, current version 21.0.0)
libssl.1.1.dylib (compatibility version 1.1.0, current version 1.1.0)
libcrypto.1.1.dylib (compatibility version 1.1.0, current version 1.1.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1281.100.1)
因此,我们修改了_mysql.cpython-38-darwin.so中的文件路径。有一个工具。它是安装名称工具。我运行了这些命令。基本上,我将@rpath更改为绝对路径,也将libssl和licrypto路径更改为绝对路径。
$ install_name_tool -change @rpath/libmysqlclient.21.dylib /usr/local/mysql/lib/libmysqlclient.21.dylib _mysql.cpython-38-darwin.so
$ install_name_tool -change libssl.1.1.dylib /usr/local/mysql/lib/libssl.1.1.dylib _mysql.cpython-38-darwin.so
$ install_name_tool -change libcrypto.1.1.dylib /usr/local/mysql/lib/libcrypto.1.1.dylib _mysql.cpython-38-darwin.so
这些更改后,我的Django表现出色。
答案 1 :(得分:1)
只需在/ usr / local / lib中创建符号链接
sudo ln -s /usr/local/mysql/lib/libmysqlclient.21.dylib /usr/local/lib/libmysqlclient.21.dylib
答案 2 :(得分:1)
我的Python 3.6项目正在寻找libmysqlclient.21.dylib。我安装了brew install mysql-client。它安装了mysql-client 8.0。它具有libmysqlclient.21.dylib。如我想使用mysql@5.6的地方。所以我将libmysqlclient.21.dylib从/usr/local/Cellar/mysql-client/8.0.19/lib复制到/ usr / local / lib /
sudo ln -s /usr/local/Cellar/mysql-client/8.0.19/lib/libmysqlclient.21.dylib /usr/local/lib/libmysqlclient.21.dylib
答案 3 :(得分:1)
symlink解决方案对我有用,但是在链接第一个文件后,对于不同的文件名,我得到了相同的错误消息。总共,我不得不符号链接三个不同的文件。
sudo ln -s /usr/local/mysql/lib/libmysqlclient.21.dylib /usr/local/lib/libmysqlclient.21.dylib
sudo ln -s /usr/local/mysql/lib/libssl.1.1.dylib /usr/local/lib/libssl.1.1.dylib
sudo ln -s /usr/local/mysql/lib/libcrypto.1.1.dylib /usr/local/lib/libcrypto.1.1.dylib
答案 4 :(得分:0)
因此,尽管有些不合常规,但我还是设法解决了我的问题。如果其他人将mysqlclient与Python 3和MySQL 8版本一起使用,请尝试一下,看看是否有帮助! :)
我只是制作了libmysqlclient.21.dylib
文件的副本,该文件位于我安装的/usr/local/mysql/lib
中的MySQL 8.0.13中,并将该副本以相同的名称移动到/usr/lib
。 / p>
但是,您将需要暂时禁用Mac上的安全完整性保护,因为您将没有权限或无法更改/usr/lib
中的任何权限而没有禁用它。您可以通过启动进入恢复系统,在顶部的菜单上单击实用程序,然后打开终端并在终端中输入csrutil disable
来执行此操作。只记得在完成此操作后重新打开安全完整性保护!与上述过程的唯一区别是您改为运行csrutil enable
。
完成此操作后,我像以前一样在新数据库上运行了迁移,该迁移成功了!老实说,我不知道这真的有什么好的解决方案,但是到目前为止,自从我更改了连接器使用的.dylib
的副本以来,一切工作都很好。我希望这对您也有帮助!
您可以阅读有关如何禁用和启用macOS的安全完整性保护here的更多信息。