SpringBoot項目如何連接MySQL8.0數(shù)據(jù)庫
SpringBoot連接MySQL8.0數(shù)據(jù)庫
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/springsecuritydate?serverTimezone=GMT%2B8 spring.datasource.username=root spring.datasource.password=root
SpringBoot連接MySQL8.0版的坑
錯誤描述
出錯時:數(shù)據(jù)庫連接,需要先配置application-dev.yml文件,配置文件如下:
server: port: 8081 servlet: context-path: /luckymoney limit: minMoney: 0.01 maxMoney: 9999 description: 最少要發(fā)${limit.minMoney}元,最多要發(fā)${limit.maxMoney}元 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/luckymoney username: root password: 0625 jpa: hibernate: ddl-auto: create show-sql: true
報錯內容:
然后在網(wǎng)上查找相應的內容,其實關于時區(qū)錯誤有很多解決方案。
我找到一個能夠解決的方案為修改spring.datasource.url
spring: datasource: url: jdbc:mysql://localhost:3306/luckymoney?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=true
即添加:?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=true
解決了上述問題,再次運行程序,依然報錯:
報錯內容:
這一次還是連接池初始化錯誤,但是這個問題就花費很多時間去查找。在下面給出解決方案。
解決方案
將數(shù)據(jù)庫密碼用單引號括起來,就是這樣就解決了。
但是具體原理還需要我進一步去了解,為什么springboot在于mysql 8.0版本連接的時候,密碼要使用單引號括起來。(好像這個將密碼括起來不是那么必須?但是也是一種解決方案)
... spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/luckymoney?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=true username: root password: '0625' ...
這下就可以完美啟動了!
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。