java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: NO)

今天用springboot项目连接数据库时报了这个错误:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: NO)
### The error may exist in file [D:\install\IDEA\workSpace\scs\target\classes\mapper\UserMapper.xml]
### The error may involve com.defei.scs.dao.UserMapper.selectByUserName
### The error occurred while executing a query
### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: NO)
    at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:92) ~[mybatis-spring-2.0.4.jar:2.0.4]
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:440) ~[mybatis-spring-2.0.4.jar:2.0.4]
    at com.sun.proxy.$Proxy61.selectOne(Unknown Source) ~[na:na]
    at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:159) ~[mybatis-spring-2.0.4.jar:2.0.4]
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:87) ~[mybatis-3.5.4.jar:3.5.4]

..........

错误的意思就是当系统去调用[D:\install\IDEA\workSpace\scs\target\classes\mapper\UserMapper.xml](这是我的数据库user表mybatis映射配置文件)文件中的selectByUserName方法连接数据库时无法连接数据库,主要是java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: NO)这个异常导致的。那么这个异常是怎么产生的呢?

我排除的步骤:

1.去网上搜,有人说是jar的版本太高,降低就好了。然而,我的版本是根据springboot的版本默认的,并没有单独给mysql的jar添加版本。因此此方法对我来讲不适用。

我的pom.xml内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.defei</groupId>
    <artifactId>scs</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>scs</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!--spring boot web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.2</version>
        </dependency>
        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!--test-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--shiro框架-->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring</artifactId>
            <version>1.4.0</version>
        </dependency>
        <!--Thymeleaf模板-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!--JSON数据-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.30</version>
        </dependency>
        <!--lombok插件-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!--AES加密算法-->
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.10</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <finalName>scs</finalName>
    </build>

</project>

2.有人说先去看下配置是否正确。于是我去仔细检查配置,结果发现是我的配置有问题。如下图:

原错误配置:

正确的配置:

就是因为把password写成了data-password导致的。。。。泪流满面.T_T.

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES) 是一个常见的数据库连接错误,表示用户'root'在本地主机上使用了错误的密码进行连接。 解决这个问题的方法有以下几种: 1. 确保用户名和密码正确:首先,确保你使用的用户名和密码是正确的。检查你的数据库配置文件或者数据库管理工具中的用户名和密码是否与你尝试连接的数据库一致。 2. 检查数据库权限:如果用户名和密码是正确的,但仍然无法连接数据库,可能是因为该用户没有足够的权限访问数据库。请确保该用户具有正确的权限,包括连接数据库和执行所需的操作。 3. 检查数据库连接字符串:检查你的数据库连接字符串是否正确。确保连接字符串中的用户名、密码和数据库名称都是正确的。 4. 检查数据库服务器配置:如果以上方法都没有解决问题,可能是因为数据库服务器的配置有问题。请检查数据库服务器的配置文件,确保允许远程连接,并且没有其他限制。 5. 检查防火墙设置:有时候,防火墙设置可能会阻止数据库连接。请确保你的防火墙允许数据库连接。 6. 重置密码:如果你确信用户名和密码是正确的,但仍然无法连接数据库,可以尝试重置密码。使用数据库管理工具或者命令行工具重置密码,并更新你的应用程序中的连接信息。 这些方法应该能够帮助你解决java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)错误。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值