SpringBoot
项目配置文件有两种格式:
application-xx.properties、
application-xx.yaml
运行项目时常常出现以上错误。一般是两个原因导致的:连接信息配置错误,数据库未授权
- 连接信息配置错误
错误配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/springbootdb
username: root
password: 0612
driverClassName: com.mysql.jdbc.Driver
正确配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/springbootdb
username: root
password: "0612"
driverClassName: com.mysql.jdbc.Driver
注意了,
yaml
是区分数据类型的,所以如果用户名或者密码是数字的话,就要小心了。如果password
为0612
的话,最终获取到的值是612,显然不对,那么怎么表示0612呢?只能用字符串"000000"
,这样获取到的密码就是正确的了!
-
数据库未授权
如果以上信息都配置正确的话,很可能是数据库授权失败,所以请先进数据库对当前远程用户授权。
- 从控制台进入
MySQL
环境 - 授权
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '你的密码' WITH GRANT OPTION;
- 刷新权限表
FLUSH PRIVILEGES;