Spring Boot、MySQL、Tomcat 无法在 Eclipse 中创建池的初始连接
Posted
技术标签:
【中文标题】Spring Boot、MySQL、Tomcat 无法在 Eclipse 中创建池的初始连接【英文标题】:Spring Boot, MySQL, Tomcat can't create intital connections of pool in Eclipse 【发布时间】:2019-10-14 22:46:47 【问题描述】:我在 Eclipse 中使用 Spring Boot 和 REST API 创建了一个 webapp。当我尝试使用 Tomcat 8 运行它时,它给出了错误 “o.a.tomcat.jdbc.pool.ConnectionPool:无法创建池的初始连接。”
我尝试使用提供的解决方案,但似乎没有任何效果,它总是给出相同的错误。
这是我的 application.properties 文件 -
spring.datasource.url = jdbc:mysql://localhost:3306/dbase?autoReconnect=true&useSSL=false
spring.datasource.username = 1234
spring.datasource.password = 1234
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto = update
这是 pom.xml 文件 -
<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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.springboot</groupId>
<artifactId>springbootrestapi</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>springbootrestapiexample Maven Webapp</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>springbootrestapiexample</finalName>
</build>
</project>
这是堆栈跟踪 -
16:33:43.182 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Included patterns for restart : []
16:33:43.206 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Excluded patterns for restart : [/spring-boot-starter/target/classes/, /spring-boot-autoconfigure/target/classes/, /spring-boot-starter-[\w-]+/, /spring-boot/target/classes/, /spring-boot-actuator/target/classes/, /spring-boot-devtools/target/classes/]
16:33:43.207 [main] DEBUG org.springframework.boot.devtools.restart.ChangeableUrls - Matching URLs for reloading : [file:/C:/Users/Amber%20Bhanarkar/eclipse-workspace/springbootrestapi/target/classes/]
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.4.RELEASE)
2019-05-29 16:33:43.767 INFO 13396 --- [ restartedMain] c.s.s.StudentApplication : Starting StudentApplication on LAPTOP-BV6107FE with PID 13396 (started by Amber Bhanarkar in C:\Users\Amber Bhanarkar\eclipse-workspace\springbootrestapi)
2019-05-29 16:33:43.768 INFO 13396 --- [ restartedMain] c.s.s.StudentApplication : No active profile set, falling back to default profiles: default
2019-05-29 16:33:43.892 INFO 13396 --- [ restartedMain] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@754391d8: startup date [Wed May 29 16:33:43 IST 2019]; root of context hierarchy
2019-05-29 16:33:49.463 INFO 13396 --- [ restartedMain] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2019-05-29 16:33:49.552 INFO 13396 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-05-29 16:33:49.554 INFO 13396 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.15
2019-05-29 16:33:50.076 INFO 13396 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-05-29 16:33:50.076 INFO 13396 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 6188 ms
2019-05-29 16:33:50.354 INFO 13396 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2019-05-29 16:33:50.359 INFO 13396 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2019-05-29 16:33:50.360 INFO 13396 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2019-05-29 16:33:50.360 INFO 13396 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2019-05-29 16:33:50.360 INFO 13396 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2019-05-29 16:33:55.529 ERROR 13396 --- [ restartedMain] o.a.tomcat.jdbc.pool.ConnectionPool : Unable to create initial connections of pool.
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
这里是 StudentApplication.java -
package com.springboot.springbootrestapi;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
@SpringBootApplication
@EnableJpaAuditing
public class StudentApplication
public static void main(String[] args)
SpringApplication.run(StudentApplication.class, args);
【问题讨论】:
您可以编辑您的问题并添加错误的完整堆栈跟踪吗? 你的 MySQL 服务是否在后台运行? @TechatTheSparksFoundation 我该如何检查?我有 MySQL 命令行客户端,它应该在后台运行吗? 转到start -> run
,输入services.msc
按回车。查找名为mysqld
的服务,其状态应为正在运行。
@TechatTheSparksFoundation 而不是 mysqld 有 MySQL80 并且它处于运行状态并且启动类型是自动的。
【参考方案1】:
你需要改变的东西很少
1.进入C盘你的mysql安装目录,找到my.ini
文件。
将 default_authentication_plugin 更改为此
default_authentication_plugin=mysql_native_password
然后转到您的 MySQL 命令行客户端, 登录后,运行以下查询:
ALTER USER 'yourusername'@'localhost' IDENTIFIED WITH mysql_native_password BY 'yourpassword';
然后重启mysql服务并运行你的程序。
将"yourusername"
和"yourpassword"
替换为您的MySQL 实际用户名和密码。
转到 POM 并替换:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
使用最新的 MySQL8 连接器
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
希望对你有帮助:)
【讨论】:
不,它没有用。我的 mysql 连接器也是“mysql-connector-java-5.1.42.jar:5.1.42”。 更新到 (mysql-connector-java-8.0.16) 因为你使用的是 mysql8 你是否重启了sql服务? 我应该将文件(MySQL 连接器 jar 文件)保存在 MySQL bin 或 tomcat bin 的什么位置? 是的,我每次都在重启服务【参考方案2】:这些是要检查的东西
-
验证数据库服务器是否启动并运行,以及您的连接详细信息在独立测试中是否正常工作。
如果不存在,请授予访问权限,请访问:
Host 'xxx.xx.xxx.xxx' is not allowed to connect to this MySQL server
为什么你首先在 tomcat 中运行 spring boot 而不是作为一个胖罐子? 其次,mysql客户端JAR应该包含在tomcat库中
如果服务器的可用连接不足,则增加 mySQL 最大连接数
【讨论】:
以上是关于Spring Boot、MySQL、Tomcat 无法在 Eclipse 中创建池的初始连接的主要内容,如果未能解决你的问题,请参考以下文章
使用嵌入式 tomcat 未在 Spring Boot 中映射请求
Spring boot + Mybatis + Thymeleaf + Druid +mySql
在spring-boot启动期间tomcat数据源不检查有效连接?
基于 Spring Boot (Tomcat) 的应用程序作为守护进程 - 如何停止?