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

Posted

技术标签:

【中文标题】django.db.utils.operationalError: (2059,"Authentication Plugin \'caching_sha2_password\'")【英文标题】:django.db.utils.operationalError: (2059,"Authentication Plugin 'caching_sha2_password'")django.db.utils.operationalError: (2059,"Authentication Plugin 'caching_sha2_password'") 【发布时间】:2018-11-01 08:01:42 【问题描述】:

我正在尝试将我的 django 项目“mysite”连接到 mysql。我在 mysql 中创建了一个用户,并授予它访问项目的所有权限。这些是我对 settings.py 所做的更改:

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


现在,当我尝试使用 python3 manage.py makemigrations 迁移数据库时,出现以下错误:

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>

完整的堆栈跟踪如下:

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")

如何解决此错误?我在 ubuntu 16.04 平台上使用 python 3。

【问题讨论】:

【参考方案1】:

问题(可能)是您的(新)版本的 MySQL。

从 8.04 版开始,MySQL 使用caching_sha2_password 作为默认身份验证插件,以前使用 mysql_native_password(这会导致与需要 mysql_native_password 身份验证的旧服务的兼容性问题)。

可能的解决方案:

将 MySQL 服务器降级到低于更改或更改身份验证插件的版本(基于用户)

例如在创建用户时:

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

【讨论】:

得到错误'访问被拒绝,USING_PASSWORD:是'虽然密码输入正确。【参考方案2】:

其实MYSQL Server没有必要降级。按照这两个步骤,它应该工作:

第 1 步:更改 MYSQL 配置以使用 mysql_native_password。编辑 mysqld 正在使用的 ini 文件之一。您可以使用命令查看我的哪一个

mysqld --verbose --help 

并确保添加了这一行 -

default-authentication-plugin=mysql_native_password

第 2 步:创建新用户或更改现有用户。使用 mysql_native_password 创建新密码:

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

改变用户

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

对了,这一步也被@Kol_ya指出了

【讨论】:

Alter 命令运行良好。我更改了安装 MySQL 时默认创建的“根”ID 我收到了这个ERROR 1396 (HY000): Operation ALTER USER failed for 'sms_db_user'@'127.0.0.1' 。还有ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'default-authentication-plugin=mysql_native_password' at line 1【参考方案3】:

@Kol_ya 使用 CREATE USER 'username'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password' 创建了新用户;其中用户名 = 创建新用户名和密码 = 当前登录 root 的密码(使用缓存 sha2 创建的用户名)。此步骤允许我连接到 MySql Server 8.0.13 而不会出现caching_sha2 身份验证错误。 寻找解决方案几个线程,但这个帮助就像那样。谢谢@kol_ya

给遇到 cache_sha2 问题的人的建议。在服务器端,确保您的用户身份验证类型是标准的,而不是caching_sha2_password。

【讨论】:

【参考方案4】:

在 MySql 8.0 中,默认的身份验证插件是“caching_sha2_password”,这会导致身份验证插件问题(错误 2059),可以在 MySql 工作台中创建新用户 并将身份验证设置为标准。

【讨论】:

【参考方案5】:

当数据库用户没有对数据库的所有访问权限时,通常会出现此错误

运行这个命令:

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

*确保您的 MySQL 服务器正在运行。

【讨论】:

以上是关于django.db.utils.operationalError: (2059,"Authentication Plugin 'caching_sha2_password'")的主要内容,如果未能解决你的问题,请参考以下文章