Spring Boot docker无法连接到mysql(连接被拒绝/ createCommunicationsException)

时间:2019-06-03 15:02:33

标签: java mysql spring-boot docker nginx

好的,所以我有spring-boot java应用程序,mysql db和nginx。 我启动它们,看起来像这样(我需要执行的命令 docker-compose up ): enter image description here

我的应用程序 workaround_app_1 中出现以下错误:

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

14:46:59.083 INFO [c.b.w.WorkaroundApplication]  Starting WorkaroundApplication on 9b42d0d4614b with PID 48 (/app/target/classes started by root in /app)
14:46:59.089 INFO [c.b.w.WorkaroundApplication]  The following profiles are active: devdock
14:47:11.985 INFO [o.a.c.h.Http11NioProtocol]  Initializing ProtocolHandler ["https-jsse-nio-8080"]
14:47:12.015 INFO [o.a.c.c.StandardService]  Starting service [Tomcat]
14:47:12.016 INFO [o.a.c.c.StandardEngine]  Starting Servlet engine: [Apache Tomcat/9.0.17]
14:47:13.993 INFO [o.a.c.c.C.[.[.[/workaround]]  Initializing Spring embedded WebApplicationContext
14:47:13.996 INFO [o.s.w.c.ContextLoader]  Root WebApplicationContext: initialization completed in 14635 ms
14:47:17.906 INFO [c.z.h.HikariDataSource]  HikariPool-1 - Starting...
14:47:19.294 ERROR [c.z.h.p.HikariPool]  HikariPool-1 - Exception during pool initialization.
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
        at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
        at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
        at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835)
        at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455)
        at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
        at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199)
        at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:136)
        at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:369)
        at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:198)
        at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:467)
        at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:541)
        at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115)
        at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112)
        at org.springframework.jdbc.datasource.DataSourceUtils.fetchConnection(DataSourceUtils.java:157)
        at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:115)
        at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:78)
        at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:319)
        at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:356)
        at org.springframework.boot.autoconfigure.orm.jpa.DatabaseLookup.getDatabase(DatabaseLookup.java:73)
        at org.springframework.boot.autoconfigure.orm.jpa.JpaProperties.determineDatabase(JpaProperties.java:142)
        at org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.jpaVendorAdapter(JpaBaseConfiguration.java:113)
        at org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration$$EnhancerBySpringCGLIB$$aadd42f9.CGLIB$jpaVendorAdapter$4(<generated>)
        at org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration$$EnhancerBySpringCGLIB$$aadd42f9$$FastClassBySpringCGLIB$$962bc1e0.invoke(<generated>)
        at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
        at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(Configuratio

这种情况一直持续到您看到

    The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)
        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)
        at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167)
        at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91)
        at com.mysql.cj.NativeSession.connect(NativeSession.java:152)
        at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:955)
        at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825)
        ... 188 common frames omitted
Caused by: java.net.ConnectException: Connection refused (Connection refused)
        at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)
        at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)
        at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)
        at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403)
        at java.base/java.net.Socket.connect(Socket.java:591)
        at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155)
        at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65)
        ... 191 common frames omitted

这是我的docker-compose:

version: '3'
services:
  nginx:
   container_name: some-nginx
   image: nginx:1.13
   restart: always
   ports:
   - 8080:8080
   volumes:
   - ./nginx/conf.d:/etc/nginx/conf.d
   depends_on:
   - app

  mysql:
   container_name: workaround-mysql
   image: mysql/mysql-server:5.7
   environment:
    MYSQL_DATABASE: workaround
    MYSQL_USER: springuser
    MYSQL_PASSWORD: admin
    MYSQL_ROOT_PASSWORD: admin
    MYSQL_ROOT_HOST: '%'
   ports:
   - "3308:3306"
   restart: always

  app:
    restart: always
    build: ./
    working_dir: /app
    volumes:
      - ./:/app
      - ~/.m2:/root/.m2
    expose:
      - "8080"
    command: mvn clean spring-boot:run
    depends_on:
      - mysql

这是我的application.properties:

###################################
#---------DATABASE
###################################
#
# URL for the mysql db
spring.datasource.url=jdbc:mysql://workaround-mysql:3308/workaround?serverTimezone=UTC&max_allowed_packet=15728640
# User name in mysql
spring.datasource.username=springuser
# Password for mysql
spring.datasource.password=admin

我的Dockerfile仅包含一个内衬: FROM openjdk:12-jdk

所有这些,为什么无法连接到数据库会发生什么?当我在docker上执行此操作时,在localhost上一切正常。芽无法使用此设置。难道是因为我的workaround_app_1早于mysql就开始了,现在却无法运行吗?在我的docker-comd中发出芽,我指定它取决于要正确启动的mysql吗?我是docker + nginx的新手。

注意:

我尝试了不同的mysql芽端口,但这似乎不是问题。我也不认为它与资源或任何与硬件限制相关的问题。我的配置是否正确?顺便说一句,我使用Docker for Windows,64位计算机jdk12。我已经尝试了一些演示应用程序,但它们运行良好。

3 个答案:

答案 0 :(得分:1)

docker-compose.yml中的端口映射仅与主机相关,因此您可以通过localhost:3308连接到数据库。但是在其他由docker-compose组成的容器(即默认的撰写网络)中,您将不得不使用workaround-mysql:3306

答案 1 :(得分:0)

您可以从主机连接到docker容器中的db,但是不能从 other docker容器中连接到docker容器中的db。

检查mysql设置,并允许来自应用容器的IP(或docker网络)的连接。

答案 2 :(得分:0)

好了,问题出在了,mysql被卡住了:

[Entrypoint]启动MySQL 5.7.26-1.1.11

没有任何东西可以连接到它。 需要它的应用程序正在启动,并且无法连接,因此引发了错误。

以下是有关此问题的mayne的另一个问题,您可以看到它已解决: Docker MySQL - can't connect from Spring Boot app to MySQL database