Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django.db.utils.operationalError: (2059,"Authentication Plugin 'caching_sha2_password'")

I am trying to connect my django project 'mysite' to mysql. I made a user in mysql and granted it all privileges to access the project. These are the changes I made to settings.py :

    DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'mysitedb',
    'USER': 'username',
    'PASSWORD': 'password',
    'HOST': 'localhost',
    'PORT': '',
}
}

Now when I try to migrate the database using python3 manage.py makemigrations, I get the following error:

django.db.utils.OperationalError: (2059, "Authentication plugin 'caching_sha2_password' cannot be loaded: /usr/lib/mysql/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory")</code></pre>

The complete stack trace is as follows:

Traceback (most recent call last):
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
self.connect()
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/backends/base/base.py", line 194, in connect
self.connection = self.get_new_connection(conn_params)
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/backends/mysql/base.py", line 236, in get_new_connection
return Database.connect(**conn_params)
File "/usr/lib/python3/dist-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/lib/python3/dist-packages/MySQLdb/connections.py", line 204, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2059, "Authentication plugin 'caching_sha2_password' cannot be loaded: /usr/lib/mysql/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory")

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 "/home/prabhjeet/.local/lib/python3.5/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/core/management/__init__.py", line 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/core/management/base.py", line 332, in execute
self.check()
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/core/management/base.py", line 364, in check
include_deployment_checks=include_deployment_checks,
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/core/management/base.py", line 351, in _run_checks
return checks.run_checks(**kwargs)
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/core/checks/registry.py", line 73, in run_checks
new_errors = check(app_configs=app_configs)
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/core/checks/model_checks.py", line 27, in check_all_models
errors.extend(model.check(**kwargs))
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/models/base.py", line 1200, in check
errors.extend(cls._check_fields(**kwargs))
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/models/base.py", line 1272, in _check_fields
errors.extend(field.check(**kwargs))
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 894, in check
errors = super().check(**kwargs)
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 206, in check
errors.extend(self._check_backend_specific_checks(**kwargs))
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 303, in _check_backend_specific_checks
return connections[db].validation.check_field(self, **kwargs)
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/backends/base/validation.py", line 21, in check_field
field_type = field.db_type(self.connection)
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 648, in db_type
return connection.data_types[self.get_internal_type()] % data
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/utils/functional.py", line 36, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/backends/mysql/base.py", line 133, in data_types
if self.features.supports_microsecond_precision:
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/utils/functional.py", line 36, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/backends/mysql/features.py", line 65, in supports_microsecond_precision
return self.connection.mysql_version >= (5, 6, 4)
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/utils/functional.py", line 36, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/backends/mysql/base.py", line 345, in mysql_version
with self.temporary_connection() as cursor:
File "/usr/lib/python3.5/contextlib.py", line 59, in __enter__
return next(self.gen)
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/backends/base/base.py", line 576, in temporary_connection
cursor = self.cursor()
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/backends/base/base.py", line 255, in cursor
return self._cursor()
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/backends/base/base.py", line 232, in _cursor
self.ensure_connection()
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
self.connect()
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
self.connect()
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/backends/base/base.py", line 194, in connect
self.connection = self.get_new_connection(conn_params)
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/backends/mysql/base.py", line 236, in get_new_connection
return Database.connect(**conn_params)
File "/usr/lib/python3/dist-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/lib/python3/dist-packages/MySQLdb/connections.py", line 204, in __init__
super(Connection, self).__init__(*args, **kwargs2)
django.db.utils.OperationalError: (2059, "Authentication plugin 'caching_sha2_password' cannot be loaded: /usr/lib/mysql/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory")

How can I resolve this error? I am using python 3 on ubuntu 16.04 platform.

like image 713
Prabhjeet Singh Avatar asked May 22 '18 13:05

Prabhjeet Singh


3 Answers

The issue is (probably) your (new) version of MySQL.

Starting with version 8.04 MySQL uses caching_sha2_password as default authentication plugin where previously mysql_native_password has been used (which causes compatibility issues with older services that expect mysql_native_password authentication).

Possible solutions:

Downgrade the MySQL Server to a version below that change or change the authentication plugin (on a user basis)

eg when creating the user:

CREATE USER 'username'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
like image 78
Kol_ya Avatar answered Nov 20 '22 07:11

Kol_ya


Actually, there is no need to downgrade the MYSQL Server. Follow these two steps and it should work:

Step 1: Change MYSQL configuration to use mysql_native_password. Edit one of ini files that mysqld is using. You can see which one my using the command

mysqld --verbose --help 

and make sure this line is added -

default-authentication-plugin=mysql_native_password

Step 2: Create an new or alter the existing user. To create a new with mysql_native_password password:

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

and to alter user

ALTER USER 'username'@'ip_address' IDENTIFIED WITH mysql_native_password BY 'password';

By the way, this step is also pointed out by @Kol_ya

like image 28
Heapify Avatar answered Nov 20 '22 08:11

Heapify


@Kol_ya created new user using CREATE USER 'username'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'; where username = creation of new username and password = current password to login for root(username created with caching_sha2). This step allowed me to connect to MySql Server 8.0.13 without having caching_sha2 authentication error. Looked for solution several threads but this one helped just like that. Thanks @kol_ya

Suggestion to people who are having caching_sha2 issue. In server side make sure your user authentication type is standard not caching_sha2_password.

like image 1
user8917093 Avatar answered Nov 20 '22 07:11

user8917093