IDEA链接MySQL数据库遇到的部分问题及解决方案
合理怀疑MySQL是不是克我,上次配环境配到崩溃就是因为MySQL安装失败,这次是因为链接不上IDEA
闲话少叙,直接看代码、报错信息以及解决方式:
代码:
package jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class bookstore {
public static void main(String[] args) {
//加载驱动
try {
//加载
Class.forName("com.mysql.jdbc.Driver");
System.out.println("加载成功");
//链接数据库,获得链接对象
String jdbcUrl = "jdbc:mysql://127.0.0.1:3306/db3";
String username = "root";
String password = "123456";
Connection connection = DriverManager.getConnection(jdbcUrl, username, password);
System.out.println("链接成功");
} catch (ClassNotFoundException | SQLException e) {
System.out.println("失败");
throw new RuntimeException(e);
}
}
}
问题一:
报错信息:Tue Dec 12 01:47:57 CST 2023 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
原因:JDK版本问题
解决方式:
建议使用
String jdbcUrl = "jdbc:mysql://127.0.0.1:3306/db3?useSSL=false"
(也就是在末尾加个?useSSL=false)显式地设置 useSSL 为 false,以禁用SSL连接,不然会抛出异常。
问题二:
报错信息:Unable to load authentication plugin ‘caching_sha2_password‘.
原因:MySQL8之前的版本中加密规则是mysql_native_password,MySQL8之后,加密规则是caching_sha2_password。
解决方式:进入MySQL 8.0 Command Line Client,逐行输入如下代码:(123456换成你相应的密码)。
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456' PASSWORD EXPIRE NEVER;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
FLUSH PRIVILEGES;
alter user 'root'@'localhost' identified by '123456';
修改后的完整代码:
package jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class bookstore {
public static void main(String[] args) {
//加载驱动
try {
//加载
Class.forName("com.mysql.jdbc.Driver");
System.out.println("加载成功");
//链接数据库,获得链接对象
String jdbcUrl = "jdbc:mysql://127.0.0.1:3306/db3?useSSL=false";
String username = "root";
String password = "123456";
Connection connection = DriverManager.getConnection(jdbcUrl, username, password);
System.out.println("链接成功");
} catch (ClassNotFoundException | SQLException e) {
System.out.println("失败");
throw new RuntimeException(e);
}
}
}