Spring Boot,MySQL,Tomcat无法在Eclipse中创建池的初始连接

时间:2019-05-29 10:37:27

标签: java mysql eclipse spring-boot tomcat

我已经在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);
    }

}

2 个答案:

答案 0 :(得分:1)

您必须更改很少的内容

1。转到C盘中的mysql安装目录,然后找到my.ini文件。

将default_authentication_plugin更改为此

default_authentication_plugin=mysql_native_password
  1. ,然后转到您的MySQL命令行客户端, 登录后,运行以下查询:

    ALTER USER'yourusername'@'localhost'由mysql_native_password标识为'yourpassword';

然后重新启动mysql服务并运行程序。

用您的MySQL实际用户名和密码替换"yourusername""yourpassword"

  1. 转到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>

我希望它会有所帮助:)

答案 1 :(得分:0)

这些是要检查的东西

  1. 验证数据库服务器是否已启动并正在运行,以及您的连接详细信息在独立测试中是否正常运行。
  2. 如果没有访问权限,请访问:

Host 'xxx.xx.xxx.xxx' is not allowed to connect to this MySQL server

  1. 为什么首先要在tomcat中运行spring boot而不是一个胖子? 其次,应将mysql客户端JAR包含在tomcat库中

  2. 如果服务器用尽了可用的连接数,则增加mySQL最大连接数