SpringBoot 配置 JDBC 连接 低版本 MySql 数据库报错 java.sql.SQLException: Access denied for user ‘root’@‘localhost’ (using password: YES)

段子手168

1、问题描述:

笔者在用 mysql-connector-java-5.1.32.jar 连接 mysql-5.7.22 时,遇到如下错误:
在这里插入图片描述

2、分析原因:

在项目的 application.yml 配置文件里,连接 低版本 mysql 时,用户名和密码都需要加上单引号。

在这里插入图片描述

3、解决方法:

修改 application.yml 配置文件,用户名和密码都加上单引号。

## D:\java_test\Intesij_idea\spring_cloud_demo\product_service\src\main\resources\application.yml

server:
  port: 9001 #端口

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
#    url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8
    username: 'root'
    password: '012311'
  application:
    name: service-product #服务名称
  jpa:
    database: MySQL
    show-sql: true
    open-in-view: true

#spring:
#  application:
#    name: service-product #服务名称
#  datasource:
#    driver-class-name: com.mysql.jdbc.Driver
#    url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8
#    username: root
#    password: 012311
#  jpa:
#    database: MySQL
#    show-sql: true
#    open-in-view: true

在这里插入图片描述

更多推荐