Docker-compose启动springboot和mysql,springboot无法连接mysql服务

时间:2019-06-07 08:14:09

标签: docker-compose

我不知道如何在docker-compose.yml文件中配置网络属性,使用docker-compose up后,该项目运行,但无法连接到数据库,请查看其他人的配置,添加链接标记,但会出现错误消息,此处不允许输入序列。
springboot,msyql5.7
dockercompose.yml

version : '3'
services:
  mysql:
    container_name: mysql-compose
    image: mysql:5.7
    environment:
      MYSQL_DATABASE: aurora1
      MYSQL_ROOT_PASSWORD: 123456789
      MYSQL_ROOT_HOST: '%'
    networks:
      - spring-demo-network
    ports:
      - "6665:3306"
    restart: always
    command: [
      '--character-set-server=utf8mb4',
      '--collation-server=utf8mb4_unicode_ci',
      '--default-time-zone=+8:00'
    ]


  app:
    container_name: backend-compose
    build:
      context: .
      dockerfile: Dockerfile
    restart: always
    volumes:
      - .:/vol/development
    ports:
      - "8088:8088" 
    networks:
      - spring-demo-network   
    depends_on:
      - mysql
networks:
  spring-demo-network:
    driver: bridge

springboot项目中的配置:

datasource:
    url: jdbc:mysql://localhost:3306/aurora?useUnicode=yes&characterEncoding=UTF-8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC
    username: root
    password: 123456789
    driverClassName: com.mysql.jdbc.Driver



backend-compose | 19-06-07 08:09:02.165 [main] WARN  :  c.z.hikari.util.DriverDataSource.<init> - Registered driver with driverClassName=com.mysql.jdbc.Driver was not found, trying direct instantiation.
backend-compose | 19-06-07 08:09:03.324 [main] ERROR :  com.zaxxer.hikari.pool.HikariPool.throwPoolInitializationException - HikariPool-1 - Exception during pool initialization.
backend-compose | com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
backend-compose | 
backend-compose | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
backend-compose |   at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
backend-compose |   at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
backend-compose |   at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835)
backend-compose |   at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455)
backend-compose |   at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
backend-compose |   at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:207)
backend-compose |   at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:136)
backend-compose |   at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:369)
backend-compose |   at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:198)
backend-compose |   at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:467)
backend-compose |   at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:541)
backend-compose |   at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115)
backend-compose |   at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112)
backend-compose |   at liquibase.integration.spring.SpringLiquibase.afterPropertiesSet(SpringLiquibase.java:302)
backend-compose |   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1804)
backend-compose |   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1741)
backend-compose |   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:576)
backend-compose |   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
backend-compose |   at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
backend-compose |   at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
backend-compose |   at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
backend-compose |   at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
backend-compose |   at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:307)
backend-compose |   at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
backend-compose |   at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1083)
backend-compose |   at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:853)
backend-compose |   at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546)
backend-compose |   at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142)
backend-compose |   at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)
backend-compose |   at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
backend-compose |   at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
backend-compose |   at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
backend-compose |   at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
backend-compose |   at com.aurora.backend.Application.main(Application.java:19)
backend-compose |   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
backend-compose |   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
backend-compose |   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
backend-compose |   at java.lang.reflect.Method.invoke(Method.java:498)
backend-compose |   at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
backend-compose |   at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
backend-compose |   at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
backend-compose |   at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
backend-compose | Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
backend-compose |

1 个答案:

答案 0 :(得分:1)

如果我理解正确,那么您在

下发布的配置
  

springboot项目中的配置

是在您的backend-compose容器中,并且您正在尝试从mysql-compose容器中连接到mysql数据库(位于backend-compose容器中)吗?然后,您必须连接到mysql而不是localhost,以便您的数据源url看起来像:

jdbc:mysql://mysql:3306/aurora?useUnicode=yes&characterEncoding=UTF-8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC

如果您改为尝试从您的主机连接到mysql数据库(而不是从容器内部),则可以使用localhost,但是必须使用端口6665当您将mysql容器配置为将来自主机(运行容器的系统)端口6665的请求转发到容器端口3306时,您的jdbc网址应类似于:

jdbc:mysql://localhost:6665/aurora?useUnicode=yes&characterEncoding=UTF-8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC