问题描述
基于Springboot的代码,编写了一个数据库访问的接口,但是当通过链接访问时出现了以下错误:
查看IDEA中的错误信息,显示为:
Could not create connection to database server
以及
mysql.cj.exceptions.CJException: null, message from server: “Host ‘x‘ is not allowed to connect
原因分析及解决方案:
原因一:mysql版本与驱动版本不一致。
原因二:Thymeleaf报错。
在我的搜索过程中,许多博主对该问题进行了解释说明和解决,但在我的应用中,经过仔细检查,不存在该问题,故就此略过,如果存在该问题,可移步其他博文。
原因三:mysql权限问题。
此处参考了 https://blog.csdn.net/Gaozier/article/details/115515440 中对权限修改的方法,进入可以敲mysql语句的界面后,输入:
use mysql
update user set host = "%" where user="root";
flush privilege;
之后,在我的项目中出现了新的问题:Public Key Retrieval is not allowed.
继续查资料,参考这篇Mysql 8报错:Public Key Retrieval is not allowed(解决方式),其实就是在 application.yml 中数据库部分,添加public key的信息,如下代码中第四行url的最后,allowPublicKeyRetrieval=true
spring:
datasource:
name: test # 数据库名称
url: jdbc:mysql://127.0.0.1:13306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&useSSL=false&allowPublicKeyRetrieval=true
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
redis:
host: 127.0.0.1
port: 16379
password: 123456
database: 0
然后就解决了!