@OneLeques

Ошибка подключения СУБД MySQL на Django. Как исправить?

Пытаюсь подключить MySQL как СУБД для своего проекта на Django. Django 3.0, Python 3.8, MySQL Server 8, mysqlclient 1.4.6

Создал базу данных + отдельного пользователя для своего проекта с помощью следующих команд

CREATE DATABASE test_db DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;
CREATE USER 'django'@'localhost' IDENTIFIED BY '1234';
GRANT ALL PRIVILEGES ON test_db.* TO 'django'@'localhost';
FLUSH PRIVILEGES;


Далее изменил settings проекта:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'test_db',
        'USER': 'django',
        'PASSWORD': '1234',
        'HOST': '127.0.0.1',
        'PORT': '',
    }
}


После чего при попытке произвести миграцию получаю следующую ошибку:

Traceback (most recent call last):
  File "C:\Users\Daniil\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\base\base.py", line 220, in ensure_connection
    self.connect()
  File "C:\Users\Daniil\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "C:\Users\Daniil\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\base\base.py", line 197, in connect
    self.connection = self.get_new_connection(conn_params)
  File "C:\Users\Daniil\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "C:\Users\Daniil\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\mysql\base.py", line 233, in get_new_connection
    return Database.connect(**conn_params)
  File "C:\Users\Daniil\AppData\Local\Programs\Python\Python38-32\lib\site-packages\MySQLdb\__init__.py", line 84, in Connect
    return Connection(*args, **kwargs)
  File "C:\Users\Daniil\AppData\Local\Programs\Python\Python38-32\lib\site-packages\MySQLdb\connections.py", line 179, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
MySQLdb._exceptions.OperationalError: (2059, <NULL>)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\Daniil\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "C:\Users\Daniil\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\Daniil\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\Daniil\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\base.py", line 366, in execute
    self.check()
  File "C:\Users\Daniil\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\base.py", line 392, in check
    all_issues = self._run_checks(
  File "C:\Users\Daniil\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\commands\migrate.py", line 63, in _run_checks
    issues = run_checks(tags=[Tags.database])
  File "C:\Users\Daniil\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\checks\registry.py", line 72, in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:\Users\Daniil\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\checks\database.py", line 10, in check_database_backends
    issues.extend(conn.validation.check(**kwargs))
  File "C:\Users\Daniil\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\mysql\validation.py", line 9, in check
    issues.extend(self._check_sql_mode(**kwargs))
  File "C:\Users\Daniil\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\mysql\validation.py", line 13, in _check_sql_mode
    with self.connection.cursor() as cursor:
  File "C:\Users\Daniil\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "C:\Users\Daniil\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\base\base.py", line 260, in cursor
    return self._cursor()
  File "C:\Users\Daniil\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\base\base.py", line 236, in _cursor
    self.ensure_connection()
  File "C:\Users\Daniil\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "C:\Users\Daniil\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\base\base.py", line 220, in ensure_connection
    self.connect()
  File "C:\Users\Daniil\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\Daniil\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\base\base.py", line 220, in ensure_connection
    self.connect()
  File "C:\Users\Daniil\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "C:\Users\Daniil\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\base\base.py", line 197, in connect
    self.connection = self.get_new_connection(conn_params)
  File "C:\Users\Daniil\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "C:\Users\Daniil\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\mysql\base.py", line 233, in get_new_connection
    return Database.connect(**conn_params)
  File "C:\Users\Daniil\AppData\Local\Programs\Python\Python38-32\lib\site-packages\MySQLdb\__init__.py", line 84, in Connect
    return Connection(*args, **kwargs)
  File "C:\Users\Daniil\AppData\Local\Programs\Python\Python38-32\lib\site-packages\MySQLdb\connections.py", line 179, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
django.db.utils.OperationalError: (2059, <NULL>)


PS
Я новичок в Django и уже довольно долго ищу решение этой проблемы. Заранее спасибо
  • Вопрос задан
  • 327 просмотров
Решения вопроса 2
@OneLeques Автор вопроса
Для решения проблемы необходимо изменить plugin для django (имя пользователя)

Команды для изменения plugin, пароля и перезагрузки привилегий

ALTER USER 'django'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
FLUSH PRIVILEGES;


После этого соответственно нужно изменить settings (изменить password)
Ответ написан
Комментировать
@KingstonKMS
поменяйте на mysql_native_password, перезадайте пароль и перезагрузите привилегии
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы