如何解决“无法加载身份验证插件'caching_sha2_password'”问题

143
在Eclipse中启动应用程序时,我收到了这个错误提示 - Could not discover the dialect to use. java.sql.SQLException: Unable to load authentication plugin 'caching_sha2_password'.

在java.sql.SQLException中发生了Unable to load authentication plugin 'caching_sha2_password'的错误。 在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:868)处,在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:864)处,在com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1746)处, 在com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1226)处,在com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2191)处,在com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2222)处,在com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2017)处, 在com.mysql.jdbc.ConnectionImpl。(ConnectionImpl.java:779)处,在com.mysql.jdbc.JDBC4Connection。(JDBC4Connection.java:47)处,在sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)处, 在sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)处,在sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)处,在java.lang.reflect.Constructor.newInstance(Unknown Source)处, 在com.mysql.jdbc.Util.handleNewInstance(Util.java:425)处,在com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:389)处。

com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:330) 在java.sql.DriverManager.getConnection(未知来源)中 在java.sql.DriverManager.getConnection(未知来源)中 在ch.qos.logback.core.db.DriverManagerConnectionSource.getConnection(DriverManagerConnectionSource.java:54)中 在ch.qos.logback.core.db.ConnectionSourceBase.discoverConnectionProperties(ConnectionSourceBase.java:46)中 在ch.qos.logback.core.db.DriverManagerConnectionSource.start(DriverManagerConnectionSource.java:38)中 在ch.qos.logback.core.joran.action.NestedComplexPropertyIA.end(NestedComplexPropertyIA.java:161)中 在ch.qos.logback.core.joran.spi.Interpreter.callEndAction(Interpreter.java:309)中 在ch.qos.logback.core.joran.spi.Interpreter.endElement(Interpreter.java:193)中 在ch.qos.logback.core.joran.spi.Interpreter.endElement(Interpreter.java:179)中 在ch.qos.logback.core.joran.spi.EventPlayer.play(EventPlayer.java:62)中 在ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:165)中 在ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:152)中 在ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:110)中 在ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:53)中ch.qos.logback.classic.util.ContextInitializer.configureByResource(ContextInitializer.java:75) 在ch.qos.logback.classic.util.ContextInitializer.autoConfig(ContextInitializer.java:150)处自动配置 在org.slf4j.impl.StaticLoggerBinder.init(StaticLoggerBinder.java:84)处初始化 创建org.slf4j.impl.StaticLoggerBinder实例(StaticLoggerBinder.java:55) 在org.slf4j.LoggerFactory.bind(LoggerFactory.java:150)处绑定 在org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:124)处进行初始化 获取org.slf4j.ILoggerFactory(LoggerFactory.java:412) 在ch.qos.logback.classic.util.StatusViaSLF4JLoggerFactory.addStatus(StatusViaSLF4JLoggerFactory.java:32)处添加状态信息 在ch.qos.logback.classic.util.StatusViaSLF4JLoggerFactory.addInfo(StatusViaSLF4JLoggerFactory.java:20)处添加信息 在ch.qos.logback.classic.servlet.LogbackServletContainerInitializer.onStartup(LogbackServletContainerInitializer.java:32)处启动 在org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5245)处启动 在org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)处启动生命周期 在org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1421)处调用启动子容器 在org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1411)处调用启动子容器 在java.util.concurrent.FutureTask.run(Unknown Source)处运行 在java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)处运行线程池执行者。在Java中,ThreadPoolExecutor的工作线程正在运行(来源未知),同时一个普通线程也在运行(来源未知)。
18个回答

205

从MySQL 8.0.4开始,MySQL服务器的默认身份验证插件已经从mysql_native_password更改为caching_sha2_password

您可以运行以下命令来解决此问题。

示例用户名/密码=> student / pass123

ALTER USER 'student'@'localhost' IDENTIFIED WITH mysql_native_password BY 'pass123';

请参考官方页面了解详情:MySQL参考手册


13
虽然这样可以运行,但最好升级MySQL Connector/J版本(正如其他答案所建议的那样),因为这允许您使用更安全的sha2_caching身份验证插件。 - Mark Rotteveel
6
你需要更新MySQL Connector/J至少到5.1.46版本,或者更好的选择是升级到8.0.14版本。 - Mark Rotteveel
@MarkRotteveel 我该怎么做?https://stackoverflow.com/questions/54413365/unable-to-load-authentication-plugin-caching-sha2-password-even-when-mysql-co - theonlygusti
@theonlygusti 你可以通过更新Maven或Gradle依赖定义中的依赖项,或者手动从MySQL网站下载更新版本并将其替换为项目的依赖项来解决这个问题。 - Mark Rotteveel
@Yusef 创建一个新用户并不是一个好的解决方案,但至少它能够工作。 - HeadhunterKev
显示剩余4条评论

96

其他人已经指出了根本问题,但在我的情况下,我正在使用dbeaver,并且最初在设置与dbeaver的mysql连接时选择了错误的mysql驱动程序(在此要感谢这个回答: https://github.com/dbeaver/dbeaver/issues/4691#issuecomment-442173584)。

如下图所示选择MySQL选项将会产生上述错误,因为驱动程序是mysql4+,可以在数据库信息提示中看到。


选择此处的mysql 4+选项会导致出现此问题


不要选择MySQL驱动程序,而是选择MySQL 8+驱动程序,如下图所示。


输入图像描述


9
谢谢,这正是我的问题。你接下来是否遇到了另一个错误:“不允许公钥检索”? - Tomáš Zato
6
谢谢。这对我非常有帮助。对于那些在DBeaver中仍然遇到“Public Key Retrieval is not allowed”错误的人,我找到了一个解决方案的链接在这里 - jpisty
10
公钥检索不允许。 - aderchox
6
在最新版本的DBeaver中(已验证版本为7.3),有两个选项可用,分别为“MySql 5”和“MySql”。您需要选择“MySql”选项,并且当您将鼠标悬停在该选项上时,会显示一个提示,说明这适用于mysql 8+版本。 - techmagister
1
只需在DBeaver中转到驱动程序属性并将allowPublicKeyRetrieval设置为“True”,即可解决“不允许公钥检索”的问题。对于非Dbeaver用户,请参考https://dev59.com/VFUL5IYBdhLWcg3waXcr。 - Dreamweaver
显示剩余2条评论

63

将您的mysql-connector" lib包升级到与您的mysql版本相同的版本,例如我使用的是8.0.13版本,在pom中更改版本:

升级您的mysql-connector“lib包,使其版本与您的mysql版本相同。例如,我正在使用8.0.13版本,并在pom文件中更改了版本:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
    <version>8.0.13</version>
</dependency>

这件事情已经解决了。


2
感谢您提供有用的答案。对于其他人,如果要查找最新版本,请使用此链接:https://mvnrepository.com/artifact/mysql/mysql-connector-java - yusuf

34

可能您正在使用错误的mysql_connector。

请使用与MySQL版本相同的connector。


谢谢。这对我有用(在DBeaver上,选择“MySQL 8+”而不是“MySQL”),我相信这应该是正确的解决方案。 - ibic

18

在一个Spring Boot应用程序中,我遇到了一个错误,但在另一个应用程序中没有出现。最后,我发现不工作的应用程序使用的Spring Boot版本是2.0.0.RELEASE,而能正常工作的版本是2.0.1.RELEASE。这导致了MySQL连接器的差异-- 5.1.45与5.1.46。我更新了启动时出错的应用程序的Spring Boot版本,现在它可以正常工作了。


这对我很有帮助! - Anand Varkey Philips
对我很有用!我使用的是1.5.8.RELEASE版本。 - Ionut Ciuta

18
这可能是MySQL连接器需要更新的原因,因为MySQL8更改了密码加密方式,所以旧的连接器会对其进行错误加密。
Java连接器的maven仓库可以在这里找到。
如果您使用flyway插件,您也应该考虑更新它! 然后,您可以简单地使用以下内容更新您的maven pom:
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.17</version>
</dependency>

对于那些使用 Gradle 的人,你可以通过更新 build.gradle 文件来进行:

 buildscript {
    ext {
        ...
    }
    repositories {
        ...
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath('mysql:mysql-connector-java:8.0.11')
    }
 }

这里是所有你真正需要做的(build.gradle 示例)


15

您只需要删除旧连接器并下载新版本(mysql-connector-java-5.1.46)即可。


它起作用了。我浪费了将近两天的时间来寻找这个解决方案。谢谢。 - Hemant Nagpal

11

我正在使用mysql 8.0.12版本,将mysql connector更新为mysql-connector-java-8.0.12解决了我的问题。

希望这能帮助到某些人。


升级至至少5.1.46或最新版本,如今我们有8.0.18。这将有助于解决问题。 - Atul

9

如上所述:

从MySQL 8.0.4开始,MySQL团队将MySQL服务器的默认身份验证插件从mysql_native_password更改为caching_sha2_password。

因此,有三种方法可以解决此问题:

 1. drop USER 'user_name'@'localhost';
    flush privileges;
    CREATE USER 'user_name'@'localhost' IDENTIFIED BY 'user_name';
    GRANT ALL PRIVILEGES ON * . * TO 'user_name'@'localhost';
    ALTER USER 'user_name'@'localhost' IDENTIFIED WITH mysql_native_password BY 
    'user_name';

 2. drop USER 'user_name'@'localhost';
    flush privileges;
    CREATE USER 'user_name'@'localhost' IDENTIFIED WITH mysql_native_password BY 'user_name';
GRANT ALL PRIVILEGES ON * . * TO 'user_name'@'localhost'

 3. If the user is already created, the use the following command:

    ALTER USER 'user_name'@'localhost' IDENTIFIED WITH mysql_native_password BY 
        'user_name';

6

如果您遇到了新版MySQL中的"caching_sha2_password"插件问题,请按照以下命令解决。

DROP USER 'your_user_name'@'%';
CREATE USER 'your_user_name'@'%' IDENTIFIED WITH mysql_native_password BY 'your_user_password';
GRANT ALL PRIVILEGES ON your_db_name.* TO 'your_user_name'@'%' identified by 'your_user_password';

或者您可以使用以下命令来保持您的权限不变:

ALTER USER your_user_name IDENTIFIED WITH mysql_native_password;

你好,请问第三行中的“your_db_name.*”是什么意思?那个星号代表什么? - Sarath Mohan
用户应该获得访问权限的数据库名称。或者您可以仅使用最后一个ALTER语句来解决问题。在ALTER中,您只需要传递您的用户名即可。 - user8406805
授予 'your_user_name'@'%' 用户名以 'your_user_password' 作为身份验证方式,对 'your_db_name.*' 的所有权限。 - Sarath Mohan
好的,你在连接数据库时遇到了问题对吗?这意味着你在连接字符串中也指定了一些数据库名称,在 your_db_name 的位置上需要添加同样的数据库。希望你明白了。 - user8406805

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接