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

Cannot connect Django to existing MySQL database on M1 mac

更新:我找到了解决方案和可能的原因;在下面发布了一个答案,其中包含使其工作的步骤。

所以,我无法将 Django 连接到现有的 MySQL 数据库(为开发目的从转储创建)。这是我的步骤和所有终端输出:

准备好数据库

  1. Install MySql 5.7

  2. 开始MySQLbrew services start mysql@5.7

  3. 打开MySQLWorkbench并通过密码连接

  4. 创建新架构

  5. 打开MySQL数据库转储;在文件顶部插入 use climate,运行 脚本 -> 数据库准备就绪

设置 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.

连接 Django 与数据库

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.
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.
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'climate',
        'USER': 'root',
        'PASSWORD': 'yourpassword',
        'HOST': 'localhost',
        'PORT': '3306',
    }
}
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

如果这一步可行;接下来我会 followed/finished the tutorial here.

我已经尝试了无数的东西,

有人知道这里出了什么问题吗?

如果有帮助:我正在 运行ning macOS 11.3.1。 谢谢!

所以我找到了解决问题的方法!我认为问题是 MySQLdb 模块和我正在使用的 M1 mac 之间存在一些兼容性问题。但我可以连接数据库并使用 PyMySQL 导入模型! 我采取的步骤如下:

  • 将我的 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()
  • 运行`pip install pymysql
  • 然后 运行 python manage.py inspectdb > models.py
  • 然后 运行 python manage.py migrate

完成!