• 连接数据库时遇到的bug1号


    问题描述:

    • 用springboot创建项目,连接数据库,数据库已经连好,配置文件也已经写好,依赖没有导错
    • 配置文件
    spring:
      datasource:
        username: root
        password: 123456
        url: jdbc:mysql://localhost:3306/employee?useUnicode=true&characterEncoding=utf-8
        driver-class-name: com.mysql.cj.jdbc.Driver
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 测试连接
    package com.example;
    
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    
    import javax.sql.DataSource;
    import java.sql.Connection;
    import java.sql.SQLException;
    
    @SpringBootTest
    class Spring04DataApplicationTests {
    
    
        @Autowired
        DataSource dataSource;
    
        @Test
        void contextLoads() throws SQLException {
            //查看默认数据源
            System.out.println(dataSource.getClass());
            //获得数据库连接
            Connection connection = dataSource.getConnection();
            System.out.println(connection);
            //xxx Template: SpringBoot已经配置好模板bean,拿来即用
    
            //关闭
            connection.close();
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 运行测试
    java.sql.SQLException: Access denied for user '***'@'localhost' (using password: YES)
    
    	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
    	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
    	at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
    	at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836)
    	at com.mysql.cj.jdbc.ConnectionImpl.(ConnectionImpl.java:456)
    	at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)
    	at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198)
    	at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138)
    	at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:358)
    	at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206)
    	at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:477)
    	at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:560)
    	at com.zaxxer.hikari.pool.HikariPool.(HikariPool.java:115)
    	at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112)
    	at com.example.Spring04DataApplicationTests.contextLoads(Spring04DataApplicationTests.java:23)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.lang.reflect.Method.invoke(Method.java:498)
    	at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:686)
    	at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
    	at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
    	at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
    	at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
    	at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
    	at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
    	at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
    	at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
    	at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
    	at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
    	at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
    	at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
    	at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
    	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:212)
    	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:208)
    	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:137)
    	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:71)
    	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:135)
    	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
    	at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
    	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
    	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
    	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
    	at java.util.ArrayList.forEach(ArrayList.java:1259)
    	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
    	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
    	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
    	at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
    	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
    	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
    	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
    	at java.util.ArrayList.forEach(ArrayList.java:1259)
    	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
    	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
    	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
    	at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
    	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
    	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
    	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
    	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
    	at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
    	at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
    	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:248)
    	at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$5(DefaultLauncher.java:211)
    	at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:226)
    	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:199)
    	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:132)
    	at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:71)
    	at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
    	at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
    	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
    	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
    	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 出现java.sql.SQLException: Access denied for user ‘***’@‘localhost’ (using password: YES)

    解决方法:

    查阅了csdn、博客园等很多地方,一条一条的解决方法尝试,最后总结出这几个可能有用的,大家出现这种情况用一下的方法也许可以解决,在方法的最后在给大家写上我自己的解决方案,实在是很magic!!!

    • 首当其冲的是配置文件里出错,这个是最简单的出错也是最容易发现的
    spring:
      datasource:
        username: root
        password: 123456
        url: jdbc:mysql://localhost:3306/employee?useUnicode=true&characterEncoding=utf-8
        driver-class-name: com.mysql.cj.jdbc.Driver
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    这个是正确的配置方法。大家可以对照着看,常见的错误在,例如,username写成了data-username,这个是会报错的,同理password、url等都不要写错。

    看到csdn中有的友友是通过把密码用双引号包裹,然后解决问题,这个我实在想不到原理,我估摸着应该是个例吧,这个也很magic,大家可以尝试一下,但是应该没多大效果

    • 如果不是配置文件的错,那么可以进入下一个方法。权限问题,root没有所有的权限,或者没有最高权限,这个就是通过命令窗口,来给自己的用户提高权限,这个我发现有个博主总结的特别好,在这里引用一下,大家可以去看他的

      https://www.cnblogs.com/freespring/p/10692994.html

    • 好了,如果权限已经是最高了,那么可以继续下一个方法了。这个方法我看上去是不信的,我觉得这个太暴力了并且治标不治本,但是考虑到确实也有人通过这样成功了,那就写下来吧。

    这个方法也很简单,就是找到mysql安装文件,然后修改配置文件(我的是my.ini,你们的看自己有没有修改,类似的配置文件),在配置文件的最后一行,加上

    skip-grant-tables

    这个的作用其实就是让你在登录用户的时候,可以不用输入密码。。。很暴力的解决方法对吧,但是针对一些情况应该是有用的,因为看到有人成功解决了,但是这个还是没有解决我的问题,运行了无数次,还是出现那个熟悉的bug!!

    • 到这里已经是各种抓脑了,然后就是比较机械的解决方法,例如重新创建新用户,重新创建连接,重新下载mysql等等一系列操作,最终还是没有解决

    • 最后解决的方法

    最后以一种非常magic的方法解决了。就在我一筹莫展甚至想要放弃的时候,决定严肃的重新创建该项目(之前也试过重新创建项目但是不管用),再一次重新创建是想着再不行就放弃摸索了去找高人了。奇迹般的是,这次仅仅是重新创建项目,最后bug居然不了了之了。唯一修改的地方,是,我之前创建springboot项目时,由于总是连不上官网start.spring.io ,于是我会选择用阿里的那个https://start.aliyun.com/。这次新创建我使用的是spring官网,奇迹般bug消失了哈哈哈哈,可以运行了

    一点总结

    总结的就是说,首先遇到bug,该尝试的方法肯定要一个一个尝试过去,不要轻言放弃,也不要没有耐心,上网搜过才发现,很多人都是一个bug查一下午或者一整天的,这在学习过程中是很正常的事情,即便在所有方法尝试遍但仍然没有解决问题,我们还要耐心的排错,像重启idea、重启电脑、重启项目,这都是很常见的操作,不要不相信,有时候可能真的就是网络连接错误或者刷新错误。总之耐心的排错,相信magic!

  • 相关阅读:
    【UNR #6 E】神隐(交互)(二进制分组)
    【API 管理】什么是 API 管理,为什么它很重要?
    async-validator.js数据校验器
    mongoDB安装教程
    Python 中的随机 IP 地址生成器
    前端(二十五)——前端实现 OCR 图文识别的详细步骤与示例代码
    FullCalendarDemo5 控件的实例讲解—拖拽实现值班排班(四)
    java开发小公司跳槽华为od笔试面试过程
    (个人笔记质量不佳)SQL 左连接、右连接、内连接的区别
    DNS配置
  • 原文地址:https://blog.csdn.net/m0_56116754/article/details/126086298