用springboot项目连接数据库时报了这个错误java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: NO)的错,找了一个半小时,上网查资料整理错误,才搞定。

D:\Java\jdk-8u241\bin\java.exe "-javaagent:D:\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=60618:D:\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath "D:\Java\jdk-8u241\jre\lib\charsets.jar;D:\Java\jdk-8u241\jre\lib\deploy.jar;D:\Java\jdk-8u241\jre\lib\ext\access-bridge-64.jar;D:\Java\jdk-8u241\jre\lib\ext\cldrdata.jar;D:\Java\jdk-8u241\jre\lib\ext\dnsns.jar;D:\Java\jdk-8u241\jre\lib\ext\jaccess.jar;D:\Java\jdk-8u241\jre\lib\ext\jfxrt.jar;D:\Java\jdk-8u241\jre\lib\ext\localedata.jar;D:\Java\jdk-8u241\jre\lib\ext\nashorn.jar;D:\Java\jdk-8u241\jre\lib\ext\sunec.jar;D:\Java\jdk-8u241\jre\lib\ext\sunjce_provider.jar;D:\Java\jdk-8u241\jre\lib\ext\sunmscapi.jar;D:\Java\jdk-8u241\jre\lib\ext\sunpkcs11.jar;D:\Java\jdk-8u241\jre\lib\ext\zipfs.jar;D:\Java\jdk-8u241\jre\lib\javaws.jar;D:\Java\jdk-8u241\jre\lib\jce.jar;D:\Java\jdk-8u241\jre\lib\jfr.jar;D:\Java\jdk-8u241\jre\lib\jfxswt.jar;D:\Java\jdk-8u241\jre\lib\jsse.jar;D:\Java\jdk-8u241\jre\lib\management-agent.jar;D:\Java\jdk-8u241\jre\lib\plugin.jar;D:\Java\jdk-8u241\jre\lib\resources.jar;D:\Java\jdk-8u241\jre\lib\rt.jar;D:\IntelliJ IDEA Projects location\miaosha\target\classes;C:\Users\ASUS\.m2\repository\org\springframework\boot\spring-boot-starter-web\2.3.1.RELEASE\spring-boot-starter-web-2.3.1.RELEASE.jar;C:\Users\ASUS\.m2\repository\org\springframework\boot\spring-boot-starter\2.3.1.RELEASE\spring-boot-starter-2.3.1.RELEASE.jar;C:\Users\ASUS\.m2\repository\org\springframework\boot\spring-boot\2.3.1.RELEASE\spring-boot-2.3.1.RELEASE.jar;C:\Users\ASUS\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\2.3.1.RELEASE\spring-boot-autoconfigure-2.3.1.RELEASE.jar;C:\Users\ASUS\.m2\repository\org\springframework\boot\spring-boot-starter-logging\2.3.1.RELEASE\spring-boot-starter-logging-2.3.1.RELEASE.jar;C:\Users\ASUS\.m2\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;C:\Users\ASUS\.m2\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;C:\Users\ASUS\.m2\repository\org\apache\logging\log4j\log4j-to-slf4j\2.13.3\log4j-to-slf4j-2.13.3.jar;C:\Users\ASUS\.m2\repository\org\apache\logging\log4j\log4j-api\2.13.3\log4j-api-2.13.3.jar;C:\Users\ASUS\.m2\repository\org\slf4j\jul-to-slf4j\1.7.30\jul-to-slf4j-1.7.30.jar;C:\Users\ASUS\.m2\repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;C:\Users\ASUS\.m2\repository\org\springframework\spring-core\5.2.7.RELEASE\spring-core-5.2.7.RELEASE.jar;C:\Users\ASUS\.m2\repository\org\springframework\spring-jcl\5.2.7.RELEASE\spring-jcl-5.2.7.RELEASE.jar;C:\Users\ASUS\.m2\repository\org\yaml\snakeyaml\1.26\snakeyaml-1.26.jar;C:\Users\ASUS\.m2\repository\org\springframework\boot\spring-boot-starter-json\2.3.1.RELEASE\spring-boot-starter-json-2.3.1.RELEASE.jar;C:\Users\ASUS\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.11.0\jackson-databind-2.11.0.jar;C:\Users\ASUS\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.11.0\jackson-annotations-2.11.0.jar;C:\Users\ASUS\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.11.0\jackson-core-2.11.0.jar;C:\Users\ASUS\.m2\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.11.0\jackson-datatype-jdk8-2.11.0.jar;C:\Users\ASUS\.m2\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.11.0\jackson-datatype-jsr310-2.11.0.jar;C:\Users\ASUS\.m2\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.11.0\jackson-module-parameter-names-2.11.0.jar;C:\Users\ASUS\.m2\repository\org\springframework\boot\spring-boot-starter-tomcat\2.3.1.RELEASE\spring-boot-starter-tomcat-2.3.1.RELEASE.jar;C:\Users\ASUS\.m2\repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.36\tomcat-embed-core-9.0.36.jar;C:\Users\ASUS\.m2\repository\org\glassfish\jakarta.el\3.0.3\jakarta.el-3.0.3.jar;C:\Users\ASUS\.m2\repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.36\tomcat-embed-websocket-9.0.36.jar;C:\Users\ASUS\.m2\repository\org\springframework\spring-web\5.2.7.RELEASE\spring-web-5.2.7.RELEASE.jar;C:\Users\ASUS\.m2\repository\org\springframework\spring-beans\5.2.7.RELEASE\spring-beans-5.2.7.RELEASE.jar;C:\Users\ASUS\.m2\repository\org\springframework\spring-webmvc\5.2.7.RELEASE\spring-webmvc-5.2.7.RELEASE.jar;C:\Users\ASUS\.m2\repository\org\springframework\spring-aop\5.2.7.RELEASE\spring-aop-5.2.7.RELEASE.jar;C:\Users\ASUS\.m2\repository\org\springframework\spring-context\5.2.7.RELEASE\spring-context-5.2.7.RELEASE.jar;C:\Users\ASUS\.m2\repository\org\springframework\spring-expression\5.2.7.RELEASE\spring-expression-5.2.7.RELEASE.jar;C:\Users\ASUS\.m2\repository\mysql\mysql-connector-java\5.1.46\mysql-connector-java-5.1.46.jar;C:\Users\ASUS\.m2\repository\com\alibaba\druid\1.1.3\druid-1.1.3.jar;D:\Java\jdk-8u241\lib\jconsole.jar;D:\Java\jdk-8u241\lib\tools.jar;C:\Users\ASUS\.m2\repository\org\mybatis\spring\boot\mybatis-spring-boot-starter\1.3.1\mybatis-spring-boot-starter-1.3.1.jar;C:\Users\ASUS\.m2\repository\org\springframework\boot\spring-boot-starter-jdbc\2.3.1.RELEASE\spring-boot-starter-jdbc-2.3.1.RELEASE.jar;C:\Users\ASUS\.m2\repository\com\zaxxer\HikariCP\3.4.5\HikariCP-3.4.5.jar;C:\Users\ASUS\.m2\repository\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;C:\Users\ASUS\.m2\repository\org\springframework\spring-jdbc\5.2.7.RELEASE\spring-jdbc-5.2.7.RELEASE.jar;C:\Users\ASUS\.m2\repository\org\springframework\spring-tx\5.2.7.RELEASE\spring-tx-5.2.7.RELEASE.jar;C:\Users\ASUS\.m2\repository\org\mybatis\spring\boot\mybatis-spring-boot-autoconfigure\1.3.1\mybatis-spring-boot-autoconfigure-1.3.1.jar;C:\Users\ASUS\.m2\repository\org\mybatis\mybatis\3.4.5\mybatis-3.4.5.jar;C:\Users\ASUS\.m2\repository\org\mybatis\mybatis-spring\1.3.1\mybatis-spring-1.3.1.jar" com.miaoshaproject.App
Hello World!

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.1.RELEASE)

2020-07-03 17:16:01.761  INFO 11828 --- [           main] com.miaoshaproject.App                   : Starting App on LAPTOP-T04TU7CT with PID 11828 (D:\IntelliJ IDEA Projects location\miaosha\target\classes started by ASUS in D:\IntelliJ IDEA Projects location\miaosha)
2020-07-03 17:16:01.764  INFO 11828 --- [           main] com.miaoshaproject.App                   : No active profile set, falling back to default profiles: default
2020-07-03 17:16:03.054  INFO 11828 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-07-03 17:16:03.063  INFO 11828 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-07-03 17:16:03.063  INFO 11828 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.36]
2020-07-03 17:16:03.154  INFO 11828 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-07-03 17:16:03.154  INFO 11828 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1343 ms
2020-07-03 17:16:03.514  INFO 11828 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-07-03 17:16:03.736  INFO 11828 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-07-03 17:16:03.746  INFO 11828 --- [           main] com.miaoshaproject.App                   : Started App in 2.273 seconds (JVM running for 2.617)
2020-07-03 17:16:25.898  INFO 11828 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-07-03 17:16:25.898  INFO 11828 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-07-03 17:16:25.903  INFO 11828 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 5 ms
2020-07-03 17:16:26.039  INFO 11828 --- [nio-8080-exec-1] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} inited
Fri Jul 03 17:16:26 CST 2020 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
2020-07-03 17:16:26.760 ERROR 11828 --- [reate-908039252] com.alibaba.druid.pool.DruidDataSource   : create connection error, url: jdbc:mysql://localhost:3306/miaosha?serverTimezone=UTC, errorCode 1045, state 28000

java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965) ~[mysql-connector-java-5.1.46.jar:5.1.46]
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3976) ~[mysql-connector-java-5.1.46.jar:5.1.46]
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3912) ~[mysql-connector-java-5.1.46.jar:5.1.46]
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:871) ~[mysql-connector-java-5.1.46.jar:5.1.46]
	at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1714) ~[mysql-connector-java-5.1.46.jar:5.1.46]
	at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1224) ~[mysql-connector-java-5.1.46.jar:5.1.46]
	at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2190) ~[mysql-connector-java-5.1.46.jar:5.1.46]
	at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2221) ~[mysql-connector-java-5.1.46.jar:5.1.46]
	at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2016) ~[mysql-connector-java-5.1.46.jar:5.1.46]
	at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:776) ~[mysql-connector-java-5.1.46.jar:5.1.46]
	at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47) ~[mysql-connector-java-5.1.46.jar:5.1.46]
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_241]
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_241]
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_241]
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_241]
	at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) ~[mysql-connector-java-5.1.46.jar:5.1.46]
	at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:386) ~[mysql-connector-java-5.1.46.jar:5.1.46]
	at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:330) ~[mysql-connector-java-5.1.46.jar:5.1.46]
	at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1464) ~[druid-1.1.3.jar:1.1.3]
	at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1529) ~[druid-1.1.3.jar:1.1.3]
	at com.alibaba.druid.pool.DruidDataSource$CreateConnectionThread.run(DruidDataSource.java:2239) ~[druid-1.1.3.jar:1.1.3]

还是老样子,提取关键性的错误信息:

2020-07-03 17:16:26.760 ERROR 11828 — [reate-908039252] com.alibaba.druid.pool.DruidDataSource : create connection error, url: jdbc:mysql://localhost:3306/miaosha?serverTimezone=UTC, errorCode 1045, state 28000
java.sql.SQLException: Access denied for user ‘’@‘localhost’ (using password: NO)

我的主方法
在这里插入图片描述
意思是:当系统去调用[D:\IntelliJ IDEA Projects location\miaosha\target\classes\mapping\UserDOMapper.xml](这是我的数据库user_info表mybatis映射配置文件)文件中的selectByPrimaryKey()方法连接数据库时无法连接数据库,主要是java.sql.SQLException: Access denied for user ‘root’@‘localhost’ (using password: NO)这个异常导致的。那么这个异常是怎么产生的呢?

1.网上找到的答案大部分说时mysql权限问题,我用idea的database连接了一下,能连通,排除权限问题,
2.有人说先去看下配置是否正确。于是我去仔细检查配置,结果发现是我的配置有问题。如下图:
在这里插入图片描述
正确配置如下:
在这里插入图片描述
就是因为把password写成了data-password导致的。。。。知道真相的我眼泪流下来.T_T.

对于这个错误,我这里有更详细的解释,以表格的形式解读----Spring-Boot配置文件数据源配置项

参数介绍
spring.datasource.continue-on-error = false初始化数据库时发生错误时,请勿停止
spring.datasource.data =Data(DML)脚本资源引用
spring.datasource.data-username =执行DML脚本(如果不同)的数据库用户
spring.datasource.data-password =执行DML脚本的数据库密码(如果不同)
spring.datasource.dbcp2.* =Commons DBCP2具体设置
spring.datasource.driver-class-nameJDBC驱动程序的完全限定名称默认情况下,根据URL自动检测
spring.datasource.generate-unique-name = false生成随机数据源名称
spring.datasource.hikari.* =Hikari具体设置
spring.datasource.initialize = true使用’data.sql’填充数据库
spring.datasource.jmx-enabled = false启用JMX支持(如果基础池提供)
spring.datasource.jndi-name =数据源的JNDI位置。设置时,类,网址,用户名和密码将被忽略
spring.datasource.name = testdb数据源的名称
spring.datasource.password登录数据库的密码
spring.datasource.platform =所有在架构资源中使用的平台(schema - $ {platform} .sql)
spring.datasource.schema =Schema(DDL)脚本资源引用
spring.datasource.schema-username =数据库用户执行DDL脚本(如果不同)
spring.datasource.schema-password =执行DDL脚本的数据库密码(如果不同)
spring.datasource.separator =;语句分隔符在SQL初始化脚本中
spring.datasource.sql-script-encoding =SQL脚本编码
spring.datasource.tomcat.* =Tomcat数据源特定设置
spring.datasource.type =要使用的连接池实现的完全限定名称。默认情况下,它是从类路径自动检测的
spring.datasource.url数据库的JDBC url
spring.datasource.username登录数据库的用户
spring.datasource.xa.data-source-class-name =XA datasource全限定名
spring.datasource.xa.properties =传递给XA数据源的属性。
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐