我在本地计算机上有一个正在运行的Django博客,其中包含sqlite3 db。我想要的是
在进入第一步之前,我跳到了第二个第一步。我遵循了this web page(在MacOS上)。我在根用户上创建了名为djangolocaldb
的数据库,并在/etc/mysql/my.cnf
中具有如下信息:
# /etc/mysql/my.cnf
[client]
database=djangolocaldb
user=root
password=ROOTPASSWORD
default-character-set=utf8
我当然创建了数据库,但没有创建表。
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| djangolocaldb |
| employees |
| information_schema |
| mydatabase |
| mysql |
| performance_schema |
| sys |
+--------------------+
7 rows in set (0.00 sec)
我像建议的网页一样更改了settings.py
。方法如下:
# settings.py
...
# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
#'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'OPTIONS' : {
'read_default_file': '/etc/mysql/my.cnf',
}
}
}
...
现在,当我在激活python manage.py runserver
的情况下运行venv
时,我得到了这样的残酷回溯(我先运行了python manage.py migrate
,回溯看起来几乎一样):< / p>
(.venv) ➜ django-local-blog git:(master) ✗ python manage.py runserver
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/MySQLdb/__init__.py", line 18, in <module>
from . import _mysql
ImportError: dlopen(/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/MySQLdb/_mysql.cpython-37m-darwin.so, 2): Library not loaded: @rpath/libmysqlclient.21.dylib
Referenced from: /Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/MySQLdb/_mysql.cpython-37m-darwin.so
Reason: image not found
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/gwanghyeongim/.pyenv/versions/3.7.6/lib/python3.7/threading.py", line 926, in _bootstrap_inner
self.run()
File "/Users/gwanghyeongim/.pyenv/versions/3.7.6/lib/python3.7/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/django/utils/autoreload.py", line 76, in raise_last_exception
raise _exception[1]
File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 357, in execute
autoreload.check_errors(django.setup)()
File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/django/apps/config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "/Users/gwanghyeongim/.pyenv/versions/3.7.6/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/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/django/contrib/auth/models.py", line 2, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/django/contrib/auth/base_user.py", line 47, in <module>
class AbstractBaseUser(models.Model):
File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/django/db/models/base.py", line 121, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/django/db/models/base.py", line 325, in add_to_class
value.contribute_to_class(cls, name)
File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/django/db/models/options.py", line 208, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/django/db/__init__.py", line 28, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/django/db/utils.py", line 207, in __getitem__
backend = load_backend(db['ENGINE'])
File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/django/db/utils.py", line 111, in load_backend
return import_module('%s.base' % backend_name)
File "/Users/gwanghyeongim/.pyenv/versions/3.7.6/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 16, in <module>
import MySQLdb as Database
File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/MySQLdb/__init__.py", line 24, in <module>
version_info, _mysql.version_info, _mysql.__file__
NameError: name '_mysql' is not defined
所以这是NameError: name '_mysql' is not defined
的问题。我以前安装过mysqlclient
,更改过settings.py
,在mysql中制作了数据库,但是没有任何步骤对此有所帮助。
我注意到,即使我将settings.py
改回sqlite3,我的博客也吐出了相同的 _mysql notdefined error 。因此,我最终恢复了提交,现在回到了sqlite3(至少我的博客正在运行它)。
我猜可能是我没有先转换数据,但我不确定它是100%可靠。
任何建议将不胜感激。预先谢谢你!
如果以某种方式运行_mysql not defined
时仍然看到python manage.py migrate
错误,请检查以下两个设置。
mysqlclient
时看到pip freeze
,(.venv) ➜ SqlDjango git:(master) ✗ pip freeze
...
Django==3.0.8
mysqlclient==2.0.1
...
确保在您的python虚拟环境中安装了mysqlclient
,而不是mysql-client
。前者支持python3,而后者支持python2,我认为这可能不是您想要的。
~/.zshrc
,对于bash等是~/.bashrc
或~/.bash_profile
。)export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/
此后,请通过运行source ~/.your_shell_config_file
应用更改。请参阅python manage.py migrate
的作品。
答案 0 :(得分:9)
这对我有用:
将其添加到 PATH:
export DYLD_LIBRARY_PATH="/usr/local/mysql/lib:$PATH"
答案 1 :(得分:5)
所以,我在回答我自己的问题。由于我的博客具有数据库,因此我尝试了一下,使另一个没有db的项目重新开始。
我注意到在导入MySQLdb
模块(mysqlclient的子模块)时出现了一个问题:Library not loaded: @rpath/libmysqlclient.21.dylib
。
浏览了几个小时,我意识到由于某种原因,Mac安全设置阻止了此设置的正确导入。
在mysqlclient
库github上,我发现one issue报告与我的报告相同。建议我运行cp -r /usr/local/mysql/lib/* /usr/local/lib/
。在此之后,我为mysql设置settings.py,运行python manage.py migration并成功了。因此对于空数据库,这可能是一个解决方案。仍在努力与数据库一搏。
我用
答案 2 :(得分:4)
我在 MacOS (Big Sur) 上遇到了同样的问题,我通过这样做修复了它
cp -r /usr/local/mysql/lib/* /usr/local/lib/
答案 3 :(得分:3)
作为完整答案:
如果使用python软件包mysqlclient,则仍然需要从Oracle / MySQL安装mysql客户端。它包含python包使用的C库。让事情变得更加混乱:python包实际上是用C编写的,以提高速度。要在MacOS上安装此库,请执行以下操作:
% brew install mysql-client
还有pure python package,具有更有吸引力的MIT许可证,如果您的公司或客户不允许GPL,则可以作为解决方案。但是,它不受官方支持,并且在两个发行版之间可能会发生一些细微的错误。 YMMV。
答案 4 :(得分:3)
这对我有用!只需安装 libmysqlclient-dev(sudo apt-get install libmysqlclient-dev
用于 Ubuntu)。有时,即使您刚刚安装了 mysql,lib 文件也会丢失。 :)
答案 5 :(得分:1)
这为我解决了这个问题:
由于Python3无法通过mysqldb与Python连接,因此您需要安装其他模块来修复问题。安装mysqlclient导致我遇到相同的NameError: : name '_mysql' is not defined
问题。
但是,通过使用pymysql
并添加代码行
pymysql.install_as_MySQLdb()
在我的Flask
应用的顶部,我设法使其正常运行!
有关mysql modules的更多信息
答案 6 :(得分:1)
我只是遇到了类似的问题,数小时都找不到解决方法
>>> import MySQLdb
Traceback (most recent call last):
File "/{path-to-venv}/lib/python3.7/site-packages/MySQLdb/__init__.py", line 18, in <module>
from . import _mysql
ImportError: /{path-to-venv}/lib/python3.7/site-packages/MySQLdb/_mysql.cpython-37m-arm-linux-gnueabihf.so: failed to map segment from shared object
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/{path-to-venv}/lib/python3.7/site-packages/MySQLdb/__init__.py", line 24, in <module>
version_info, _mysql.version_info, _mysql.__file__
NameError: name '_mysql' is not defined
因此,如果这里有人像我一样,并且将virtualenv安装在已安装的分区/磁盘上,则必须使用 exec 进行安装,这就是整个问题。
按照https://askubuntu.com/questions/311438/how-to-make-tmp-executable中的说明使用可执行权限重新安装分区。
如果使用fstab安装驱动器,请参见:https://askubuntu.com/questions/678857/fstab-doesnt-mount-with-exec。
(嗯,这是经过10个小时的尝试和调试才能花掉大声笑)
答案 7 :(得分:0)
我同意梅尔文的观点。
您可以通过键入以下内容来查看您的 MySQL 库链接:
(quantum) chaiyudeMacBook-Pro:quantum chaiyu$ python
Python 3.8.7 (v3.8.7:6503f05dd5, Dec 21 2020, 12:45:15)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb as Database
Traceback (most recent call last):
File "/Users/chaiyu/Envs/quantum/lib/python3.8/site-packages/MySQLdb/__init__.py", line 18, in <module>
from . import _mysql
ImportError: dlopen(/Users/chaiyu/Envs/quantum/lib/python3.8/site-packages/MySQLdb/_mysql.cpython-38-darwin.so, 2): Library not loaded: /usr/local/opt/mysql/lib/libmysqlclient.21.dylib
Referenced from: /Users/chaiyu/Envs/quantum/lib/python3.8/site-packages/MySQLdb/_mysql.cpython-38-darwin.so
Reason: image not found
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/chaiyu/Envs/quantum/lib/python3.8/site-packages/MySQLdb/__init__.py", line 24, in <module>
version_info, _mysql.version_info, _mysql.__file__
NameError: name '_mysql' is not defined
然后输入:
(quantum) chaiyudeMacBook-Pro:quantum chaiyu$ otool -L /Users/chaiyu/Envs/quantum/lib/python3.8/site-packages/MySQLdb/_mysql.cpython-38-darwin.so
/Users/chaiyu/Envs/quantum/lib/python3.8/site-packages/MySQLdb/_mysql.cpython-38-darwin.so:
/usr/local/opt/mysql/lib/libmysqlclient.21.dylib (compatibility version 21.0.0, current version 21.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.250.1)
(quantum) chaiyudeMacBook-Pro:quantum chaiyu$ ls -l /usr/local/opt/mysql/lib/libmysqlclient.21.dylib
ls: /usr/local/opt/mysql/lib/libmysqlclient.21.dylib: No such file or directory
然后,我发现 MySQL 链接的库不存在。
答案 8 :(得分:0)
我遇到了同样的错误,它已经工作了一段时间。我在 MacOS BigSur 上进行了更新,然后它因此错误而停止工作。
为了解决这个问题,只需重新安装 django 和 mysqlclient。
仅卸载/重新安装 mysqlclient 本身并没有解决问题。该命令也可能有所帮助。以下是我按顺序执行的命令:
pip uninstall mysqlclient
pip uninstall django
pip install django
pip install mysqlclient
注意:这将安装最新版本,因此如果您有特定版本,请确保安装这些版本。
答案 9 :(得分:-1)