SpringBoot项目如何连接MySQL8.0数据库

 

SpringBoot连接MySQL8.0数据库

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版的坑

错误描述

出错时:数据库连接,需要先配置application-dev.yml文件,配置文件如下:

server:
port: 8081
servlet:
  context-path: /luckymoney

limit:
minMoney: 0.01
maxMoney: 9999
description: 最少要发${limit.minMoney}元,最多要发${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

报错内容:

然后在网上查找相应的内容,其实关于时区错误有很多解决方案。

我找到一个能够解决的方案为修改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

解决了上述问题,再次运行程序,依然报错:

报错内容:

这一次还是连接池初始化错误,但是这个问题就花费很多时间去查找。在下面给出解决方案。

解决方案

将数据库密码用单引号括起来,就是这样就解决了。

但是具体原理还需要我进一步去了解,为什么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'
...

这下就可以完美启动了!

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程宝库

相信使用过redis的,或者正在做分布式开发的童鞋都知道redisson组件,它的功能很多,但我们使用最频繁的应该还是它的分布式锁功能,少量的代码,却实现了加锁、锁续命(看门狗)、锁订阅、解锁、锁 ...