errorCode 1045, state 28000 java.sql.SQLException: Access denied for user

谁借莪1个温暖的怀抱¢ 2024-04-01 13:28 179阅读 0赞

前言

使用Spring配置数据源,当加载druid.properties文件进行数据库连接时,容易碰到下面的报错:

  1. create connection SQLException, url: jdbc:mysql://127.0.0.1:3306/mydbs, errorCode 1045, state 28000
  2. java.sql.SQLException: Access denied for user 'ä¸ å® '@'localhost' (using password: YES)

问题分析

问题出在配置文件里,原因如下:

在配置数据源时,通常会使用 druid.properties 这个外部文件,但在该文件中,用户名的键名不能是username,因为spring在加载时会把username当作你的计算机名。

解决方法

下面是debug前后两种写法的对比:

报错的写法

  1. driver=com.mysql.jdbc.Driver
  2. url=jdbc:mysql://127.0.0.1:3306/furn_ssm?useSSL=true&useUnicode=true&characterEncoding=UTF-8
  3. username=root
  4. pwd=12345

正确的写法

  1. driver=com.mysql.jdbc.Driver
  2. url=jdbc:mysql://127.0.0.1:3306/furn_ssm?useSSL=true&useUnicode=true&characterEncoding=UTF-8
  3. user=root
  4. pwd=12345

发表评论

表情:
评论列表 (有 0 条评论,179人围观)

还没有评论,来说两句吧...

相关阅读