SOLVED: java.sql.SQLException: Unable to load authentication plugin ‘caching_sha2_password’.

  • Post author:
  • Post category:MySQL
  • Post last modified:December 12, 2022

While you connecting to MySQL are you getting error java.sql.SQLException: Unable to load authentication plugin ‘caching_sha2_password’? Though this error seems to be generic, you would mostly get this error while connecting to MySQL from Eclipse, IntelliJ, DBeaver e.t.c.

This error is caused by a version mismatch between the Java MySQL connector jar you are using and the MySQL server version. I got this error while running a sample Java application from IntelliJ with maven dependency mysql-connector-java-6.0.6.jar to connect to MySQL.

authentication plugin caching_sha2_password

Solution 1 : Change the MySQL Connector Version

I was running MySQL server version mysql-8.0.31 and by mistake I was using the old MySQL connector mysql-connector-java-6.0.6.jar hence, I was getting the error java.sql.SQLException: Unable to load authentication plugin ‘caching_sha2_password’.

After upgrading my MySQL connector jar version to mysql-connector-java-8.0.13.jar, my issue is resolved and I was able to connect to the server and access the schema and tables.

When you use this jar make sure you are using the driver com.mysql.cj.jdbc.Driver as the other driver deprecated.

Solution 2 : Maven Dependency for MySQL Connector

If you are using the Maven, use the following dependency.


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

Solution 3 : Gradle Dependency to Solve caching_sha2_password

If you are using Gradle, you can update the build.gradle file with the following.


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

Solution 4 : Update the Authentication to mysql_native_password

Since MySQL version 8.0.4, they have changed the default authentication plugin from mysql_native_password to caching_sha2_password hence you are getting java.sql.SQLException: Unable to load authentication plugin ‘caching_sha2_password’.

For any reason, if you wanted to continue using the MySQL connector version without upgrading, you need to change the authentication plugin to mysql_native_password.

In the below example, the root is a user and Passsword#789 is a password. Change these values according to your application and environment.


ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Passsword#789';

Solution 5 : Solving authentication plugin ‘caching_sha2_password’ with DBeaver

If you are using the DBeaver and getting the error java.sql.SQLException: Unable to load authentication plugin ‘caching_sha2_password’. This would be because you are using the older version of the connector. Make sure you are using the right MySQL database version as shown below.

authentication plugin caching_sha2_password

I hope this solves your problem.

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply