简体   繁体   English

无法将 Django 连接到 M1 mac 上的现有 MySQL 数据库

[英]Cannot connect Django to existing MySQL database on M1 mac

UPDATE : I found a solution and possible cause;更新:我找到了解决方案和可能的原因; posted an answer with steps to make it work below.发布了一个答案,其中包含使其在下面工作的步骤。

So, I am having trouble connecting Django to an existing MySQL database (created from a dump for dev purposes).因此,我无法将 Django 连接到现有的 MySQL 数据库(出于开发目的从转储创建)。 Here is my steps and all the terminal output:这是我的步骤和所有终端 output: 在此处输入图像描述

Get database ready准备好数据库

  1. Install MySql 5.7 安装 MySql 5.7

  2. Start MySQL brew services start mysql@5.7启动MySQL brew services start mysql@5.7

  3. Open MySQL Workbench and connect via password打开 MySQL Workbench 并通过密码连接

  4. Create new schema创建新架构在此处输入图像描述

  5. Open MySQL Database dump;打开 MySQL 数据库转储; insert use climate at the top of file, run script -> Database is ready在文件顶部插入use climate ,运行脚本 -> 数据库准备就绪

Setting up Django设置 Django

  • create new python venv python3 -m venv good-env创建新的 python venv python3 -m venv good-env
  • Activate venv source good-env/bin/activate激活venv source good-env/bin/activate
  • Installing django python -m pip install Django安装 django python -m pip install Django
Downloading Django-3.2.2-py3-none-any.whl (7.9 MB)
|████████████████████████████████| 7.9 MB 3.8 MB/s 
Collecting sqlparse>=0.2.2
 Using cached sqlparse-0.4.1-py3-none-any.whl (42 kB)
Collecting asgiref<4,>=3.3.2
 Using cached asgiref-3.3.4-py3-none-any.whl (22 kB)
Collecting pytz
 Using cached pytz-2021.1-py2.py3-none-any.whl (510 kB)
Installing collected packages: sqlparse, asgiref, pytz, Django
Successfully installed Django-3.2.2 asgiref-3.3.4 pytz-2021.1 sqlparse-0.4.1
WARNING: You are using pip version 20.1.1; however, version 21.1.1 is available.
You should consider upgrading via the '/Users/aaronkurz/Dev/2/good-env/bin/python -m pip install --upgrade pip' command.
  • Creating a project django-admin startproject climatevisual ;创建一个项目django-admin startproject climatevisual cd climatevisual

Connecting Django with the database将 Django 与数据库连接

  • Installing required drivers (I know only mysqlclient should suffice, but that led to the same result so I started trying to install both...)安装所需的驱动程序(我知道只有 mysqlclient 就足够了,但这导致了相同的结果,所以我开始尝试安装两者......)
    • pip install mysqlclient
Installing collected packages: mysqlclient
Successfully installed mysqlclient-2.0.3
WARNING: You are using pip version 20.1.1; however, version 21.1.1 is available.
You should consider upgrading via the '/Users/aaronkurz/Dev/2/good-env/bin/python3 -m pip install --upgrade pip' command.
  • pip install mysql-connector-python
Collecting mysql-connector-python
  Using cached mysql_connector_python-8.0.24-py2.py3-none-any.whl (319 kB)
Collecting protobuf>=3.0.0
  Using cached protobuf-3.15.8-cp38-cp38-macosx_10_9_x86_64.whl (1.0 MB)
Collecting six>=1.9
  Using cached six-1.16.0-py2.py3-none-any.whl (11 kB)
Installing collected packages: six, protobuf, mysql-connector-python
Successfully installed mysql-connector-python-8.0.24 protobuf-3.15.8 six-1.16.0
WARNING: You are using pip version 20.1.1; however, version 21.1.1 is available.
You should consider upgrading via the '/Users/aaronkurz/Dev/2/good-env/bin/python3 -m pip install --upgrade pip' command.
  • editing climatevisual/settings.py编辑climatevisual/settings.py
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'climate',
        'USER': 'root',
        'PASSWORD': 'yourpassword',
        'HOST': 'localhost',
        'PORT': '3306',
    }
}
  • auto generating the models python manage.py inspectdb THIS IS WHERE THE ERROR OCCURS :自动生成模型python manage.py inspectdb这是错误发生的地方:
Traceback (most recent call last):
  File "/Users/aaronkurz/Dev/2/good-env/lib/python3.8/site-packages/MySQLdb/__init__.py", line 18, in <module>
    from . import _mysql
ImportError: dlopen(/Users/aaronkurz/Dev/2/good-env/lib/python3.8/site-packages/MySQLdb/_mysql.cpython-38-darwin.so, 2): Symbol not found: _mysql_affected_rows
  Referenced from: /Users/aaronkurz/Dev/2/good-env/lib/python3.8/site-packages/MySQLdb/_mysql.cpython-38-darwin.so
  Expected in: flat namespace
 in /Users/aaronkurz/Dev/2/good-env/lib/python3.8/site-packages/MySQLdb/_mysql.cpython-38-darwin.so

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/Users/aaronkurz/Dev/2/good-env/lib/python3.8/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/Users/aaronkurz/Dev/2/good-env/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
    django.setup()
  File "/Users/aaronkurz/Dev/2/good-env/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/aaronkurz/Dev/2/good-env/lib/python3.8/site-packages/django/apps/registry.py", line 114, in populate
    app_config.import_models()
  File "/Users/aaronkurz/Dev/2/good-env/lib/python3.8/site-packages/django/apps/config.py", line 301, in import_models
    self.models_module = import_module(models_module_name)
  File "/Users/aaronkurz/opt/anaconda3/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Users/aaronkurz/Dev/2/good-env/lib/python3.8/site-packages/django/contrib/auth/models.py", line 3, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/Users/aaronkurz/Dev/2/good-env/lib/python3.8/site-packages/django/contrib/auth/base_user.py", line 48, in <module>
    class AbstractBaseUser(models.Model):
  File "/Users/aaronkurz/Dev/2/good-env/lib/python3.8/site-packages/django/db/models/base.py", line 122, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "/Users/aaronkurz/Dev/2/good-env/lib/python3.8/site-packages/django/db/models/base.py", line 326, in add_to_class
    value.contribute_to_class(cls, name)
  File "/Users/aaronkurz/Dev/2/good-env/lib/python3.8/site-packages/django/db/models/options.py", line 207, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/Users/aaronkurz/Dev/2/good-env/lib/python3.8/site-packages/django/utils/connection.py", line 15, in __getattr__
    return getattr(self._connections[self._alias], item)
  File "/Users/aaronkurz/Dev/2/good-env/lib/python3.8/site-packages/django/utils/connection.py", line 62, in __getitem__
    conn = self.create_connection(alias)
  File "/Users/aaronkurz/Dev/2/good-env/lib/python3.8/site-packages/django/db/utils.py", line 204, in create_connection
    backend = load_backend(db['ENGINE'])
  File "/Users/aaronkurz/Dev/2/good-env/lib/python3.8/site-packages/django/db/utils.py", line 111, in load_backend
    return import_module('%s.base' % backend_name)
  File "/Users/aaronkurz/opt/anaconda3/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/Users/aaronkurz/Dev/2/good-env/lib/python3.8/site-packages/django/db/backends/mysql/base.py", line 15, in <module>
    import MySQLdb as Database
  File "/Users/aaronkurz/Dev/2/good-env/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

If this step would have worked;如果这一步可行; I would next have followed/finished the tutorial here .接下来我会按照/完成 这里的教程

I have already tried countless things,我已经尝试了无数的事情,

  • reinstalling MySQL,重新安装 MySQL,
  • using the newest version of MySQL (even though I had to edit the DB dump then and it would not be possible in the real project since the project needs this version of MySQL)使用最新版本的 MySQL (即使我当时不得不编辑数据库转储,并且在实际项目中不可能,因为项目需要这个版本的 MySQL)
  • I have tried the solution the OP of this thread proposed我已经尝试了这个线程的OP提出的解决方案
  • also this answer也是这个答案
  • also [this answer](also [this answer]) (and many more tbh)还有 [this answer](also [this answer]) (还有更多 tbh)

Does somebody have a clue what is going wrong here?有人知道这里出了什么问题吗?

If it helps: I am running macOS 11.3.1.如果有帮助:我正在运行 macOS 11.3.1。 Thanks!谢谢!

So I found a solution to my problem.所以我找到了解决我的问题的方法。 I thing the problem is that there are some compatibility issues between the MySQLdb module and the M1 mac I am using.我的问题是 MySQLdb 模块和我正在使用的 M1 mac 之间存在一些兼容性问题。 But I could connect the database and import the model using PyMySQL instead: The steps I took are the following:但我可以连接数据库并使用PyMySQL导入 model: 我采取的步骤如下:

  • Change my settings.py database section to this:将我的 settings.py 数据库部分更改为:
import pymysql
# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'mysql.connector.django',
        'NAME': 'climate',
        'USER': 'root',
        'PASSWORD': 'yourpassword',
        'HOST': 'localhost',
        'PORT': '3306',
    }
}
# Fake PyMySQL's version and install as MySQLdb
# https://adamj.eu/tech/2020/02/04/how-to-use-pymysql-with-django/
pymysql.version_info = (1, 4, 2, "final", 0)
pymysql.install_as_MySQLdb()
  • run `pip install pymysql运行`pip install pymysql
  • then run python manage.py inspectdb > models.py然后运行python manage.py inspectdb > models.py
  • then run python manage.py migrate然后运行python manage.py migrate

Done!完毕!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM