导入spring jpa依赖时,使用的数据库使用了SSL,报错

在resources目录下新增persistence.xml文件配置安全数据库

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="myPU" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/pmom?useSSL=true&amp;requireSSL=true"/>
            <property name="javax.persistence.jdbc.user" value="root"/>
            <property name="javax.persistence.jdbc.password" value="123456"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL7Dialect"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
        </properties>
    </persistence-unit>
</persistence>

如果接下来报错:Unable to load authentication plugin ‘caching_sha2_password‘.

说明MySQL使用的密码方式不是本地密码,是caching_sha2_password

使用如下命令将密码验证方式改为本地密码:

# 修改密码方式为本地密码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';
# 修改密码为原密码
ALTER USER 'root'@'localhost' IDENTIFIED BY '123';

重启MySQL服务即可

 

 

参考

【数据库】忘记mysql本地密码
Unable to load authentication plugin ‘caching_sha2_password‘.