SpringBoot+Hibernate+MySQL连接问题排查求助
我已经连续好几天卡在这个问题上了,翻遍了Stack Overflow相关的帖子还是没找到解决办法。我用Eclipse开发一个在线银行的SpringBoot项目,先后装了MySQL基准测试工具和MySQL Server 8.0,但就是没法和数据库建立连接,搞不清楚Tomcat和数据库服务器之间到底哪里出了问题。
项目启动日志(部分)
. ____ _ __ _ _ /\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.4.1.RELEASE) 2018-05-15 15:52:10.050 INFO 9160 --- [ main] com.userfront.UserFrontApplication : Starting UserFrontApplication on Anix with PID 9160 (C:\Users\Anix\Documents\eclipse-workspace\UserFront\target\classes started by Anix in C:\Users\Anix\Documents\eclipse-workspace\UserFront) 2018-05-15 15:52:10.052 INFO 9160 --- [ main] com.userfront.UserFrontApplication : No active profile set, falling back to default profiles: default 2018-05-15 15:52:10.230 INFO 9160 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@45b9a632: startup date [Tue May 15 15:52:10 NPT 2018]; root of context hierarchy 2018-05-15 15:52:11.494 INFO 9160 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [class org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$d7db709a] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2018-05-15 15:52:11.530 INFO 9160 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.security.config.annotation.configuration.ObjectPostProcessorConfiguration' of type [class org.springframework.security.config.annotation.configuration.ObjectPostProcessorConfiguration$$EnhancerBySpringCGLIB$$708938d4] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2018-05-15 15:52:11.537 INFO 9160 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'objectPostProcessor' of type [class org.springframework.security.config.annotation.configuration.AutowireBeanFactoryObjectPostProcessor] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2018-05-15 15:52:11.539 INFO 9160 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler@3fc05ea2' of type [class org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2018-05-15 15:52:11.543 INFO 9160 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration' of type [class org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration$$EnhancerBySpringCGLIB$$955ddb86] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2018-05-15 15:52:11.550 INFO 9160 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'methodSecurityMetadataSource' of type [class org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2018-05-15 15:52:11.851 INFO 9160 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http) 2018-05-15 15:52:11.859 INFO 9160 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat 2018-05-15 15:52:11.860 INFO 9160 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.5 2018-05-15 15:52:11.922 INFO 9160 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2018-05-15 15:52:11.922 INFO 9160 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1695 ms 2018-05-15 15:52:12.091 INFO 9160 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestFilter' to: [/*] 2018-05-15 15:52:12.091 INFO 9160 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 2018-05-15 15:52:12.092 INFO 9160 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2018-05-15 15:52:12.092 INFO 9160 --- [ost-startSto
我的排查建议
兄弟,我之前也踩过SpringBoot老版本对接MySQL 8.0的坑,给你几个具体方向试试:
驱动版本不兼容是核心问题
SpringBoot 1.4.1默认依赖的MySQL驱动是5.x版本,完全不支持MySQL 8.0的新认证协议。你必须在pom.xml里手动指定兼容的驱动版本:<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.33</version> </dependency>连接配置必须更新
MySQL 8.0要求使用新的驱动类,还强制指定时区,否则会直接抛出连接异常。你的application.properties得改成这样:spring.datasource.url=jdbc:mysql://localhost:3306/你的数据库名?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.username=你的数据库账号 spring.datasource.password=你的数据库密码注意:驱动类已经从
com.mysql.jdbc.Driver改为com.mysql.cj.jdbc.Driver,时区参数serverTimezone不能省略。先确认MySQL服务状态
别光盯着代码,先检查MySQL服务是否正常运行。Windows下打开命令提示符输入net start mysql,Linux系统用systemctl status mysql,确保服务处于running状态。数据库账号权限要到位
有时候账号没有本地连接权限也会导致连接失败。登录MySQL客户端,执行以下命令给账号授权:GRANT ALL PRIVILEGES ON 你的数据库名.* TO '你的账号'@'localhost' IDENTIFIED BY '你的密码'; FLUSH PRIVILEGES;执行后刷新权限配置,确保账号能从本地访问目标数据库。
一定要获取完整的错误日志
你当前贴的日志被截断了,数据库连接失败的关键错误信息(比如认证失败、连接超时、驱动找不到等)肯定在日志后半段。找到完整的启动日志,里面的异常堆栈能直接定位问题根源。
如果试完这些还是不行,把完整的错误日志贴出来,我再帮你深挖!
内容的提问来源于stack exchange,提问作者AniX Sharma

