Django does not connect to mysql database

Asked

Viewed 376 times

0

was doing this project on linux Mint and I had to switch to windows and I’m having some problems to configure the environment.

when rotating the command

$ python Manage.py migrate

get the following error:

(venv) C:\Users\Nee3p\PycharmProjects\clinica>python manage.py migrate Traceback (most recent call last): File "C:\Users\Nee3p\PycharmProjects\clinica\venv\lib\site-packages\django\db\backends\base\base.py", line 216, in ensure_connection self.connect() File "C:\Users\Nee3p\PycharmProjects\clinica\venv\lib\site-packages\django\db\backends\base\base.py", line 194, in connect self.connection = self.get_new_connection(conn_params) File "C:\Users\Nee3p\PycharmProjects\clinica\venv\lib\site-packages\django\db\backends\mysql\base.py", line 227, in get_new_connection return Database.connect(**conn_params) File "C:\Users\Nee3p\PycharmProjects\clinica\venv\lib\site-packages\MySQLdb\__init__.py", line 85, in Connect return Connection(*args, **kwargs) File "C:\Users\Nee3p\PycharmProjects\clinica\venv\lib\site-packages\MySQLdb\connections.py", line 204, in __init__ super(Connection, self).__init__(*args, **kwargs2) _mysql_exceptions.OperationalError: (2059, <NULL>)

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

Traceback (most recent call last): File "manage.py", line 15, in <module> execute_from_command_line(sys.argv) File "C:\Users\Nee3p\PycharmProjects\clinica\venv\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line utility.execute() File "C:\Users\Nee3p\PycharmProjects\clinica\venv\lib\site-packages\django\core\management\__init__.py", line 375, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "C:\Users\Nee3p\PycharmProjects\clinica\venv\lib\site-packages\django\core\management\base.py", line 316, in run_from_argv self.execute(*args, **cmd_options) File "C:\Users\Nee3p\PycharmProjects\clinica\venv\lib\site-packages\django\core\management\base.py", line 350, in execute self.check() File "C:\Users\Nee3p\PycharmProjects\clinica\venv\lib\site-packages\django\core\management\base.py", line 379, in check include_deployment_checks=include_deployment_checks, File "C:\Users\Nee3p\PycharmProjects\clinica\venv\lib\site-packages\django\core\management\commands\migrate.py", line 59, in _run_checks issues = run_checks(tags=[Tags.database]) File "C:\Users\Nee3p\PycharmProjects\clinica\venv\lib\site-packages\django\core\checks\registry.py", line 71, in run_checks new_errors = check(app_configs=app_configs) File "C:\Users\Nee3p\PycharmProjects\clinica\venv\lib\site-packages\django\core\checks\database.py", line 10, in check_database_backends issues.extend(conn.validation.check(**kwargs)) File "C:\Users\Nee3p\PycharmProjects\clinica\venv\lib\site-packages\django\db\backends\mysql\validation.py", line 9, in check issues.extend(self._check_sql_mode(**kwargs)) File "C:\Users\Nee3p\PycharmProjects\clinica\venv\lib\site-packages\django\db\backends\mysql\validation.py", line 13, in _check_sql_mode with self.connection.cursor() as cursor: File "C:\Users\Nee3p\PycharmProjects\clinica\venv\lib\site-packages\django\db\backends\base\base.py", line 255, in cursor return self._cursor() File "C:\Users\Nee3p\PycharmProjects\clinica\venv\lib\site-packages\django\db\backends\base\base.py", line 232, in _cursor self.ensure_connection() File "C:\Users\Nee3p\PycharmProjects\clinica\venv\lib\site-packages\django\db\backends\base\base.py", line 216, in ensure_connection self.connect() File "C:\Users\Nee3p\PycharmProjects\clinica\venv\lib\site-packages\django\db\utils.py", line 89, in __exit__ raise dj_exc_value.with_traceback(traceback) from exc_value File "C:\Users\Nee3p\PycharmProjects\clinica\venv\lib\site-packages\django\db\backends\base\base.py", line 216, in ensure_connection self.connect() File "C:\Users\Nee3p\PycharmProjects\clinica\venv\lib\site-packages\django\db\backends\base\base.py", line 194, in connect self.connection = self.get_new_connection(conn_params) File "C:\Users\Nee3p\PycharmProjects\clinica\venv\lib\site-packages\django\db\backends\mysql\base.py", line 227, in get_new_connection return Database.connect(**conn_params) File "C:\Users\Nee3p\PycharmProjects\clinica\venv\lib\site-packages\MySQLdb\__init__.py", line 85, in Connect return Connection(*args, **kwargs) File "C:\Users\Nee3p\PycharmProjects\clinica\venv\lib\site-packages\MySQLdb\connections.py", line 204, in __init__ super(Connection, self).__init__(*args, **kwargs2) django.db.utils.OperationalError: (2059, <NULL>)

in the settings.py I have the following:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': config('DB_NAME'),
        'USER': config('DB_USER'),
        'PASSWORD': config('DB_PASSWORD'),
        'HOST': config('DB_HOST'),
        'PORT': config('DB_PORT'),
    }
}

and my file settings.ini has the correct information about the bank. Someone can help me?

  • settings.ini or settings.py?

  • settings.ini has the information for python decouple, access them there by config(). o settings.py is also correct as brought it from the backup

2 answers

1


The problem is the new version of Mysql.

Starting with version 8.04, Mysql uses caching_sha2_password as the default authentication plugin, where previously mysql_native_password was used (which causes compatibility problems with older services that expect mysql_native_password authentication).

Solutions:

Downgrade the Mysql server to a version below this change or change the authentication plug-in (based on the user)

for example, when creating the user

CREATE USER 'username'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

if necessary, give access permissions to the user.

GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'localhost';

That solved my problem.

0

py Settings.
There’s not so much mystery,

Pip install Pymysql

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',# nome do driver
        'NAME': 'nome_db',
        'USER': 'usuario',
        'PASSWORD': 'senha',
        'HOST': 'localhost', # não obrigatorio, se desejar pode deixar ja especificado
        'PORT': '3306',
    }
}

To finish you have to create the database with the same name defined in Settings.
Mysql: lib

Browser other questions tagged

You are not signed in. Login or sign up in order to post.