SpringBoot App无法在Docker中连接到MySQL

时间:2019-08-19 15:24:05

标签: docker-compose

我使用的系统是centos7,并且禁用了selinux
docker-compose.yaml:

version: "2"
services:
  web-service:
    build:
      context: .
      dockerfile: springboot.dockerfile
    container_name: smweb-container
    ports:
      - "8088:8888"
#    networks:
#      - net-spring-db
    depends_on:
      - db-service
  db-service:
    build:
      context: . 
      dockerfile: sql.dockerfile 
    container_name: mysql-container
    restart: always
    ports:
      - "3306:3306"
    networks:
      - net-spring-db
    ## environment:
    ##  MYSQL_ROOT_PASSWORD: 123
    environment:
          MYSQL_DATABASE: rbac
          MYSQL_USER: h2t
          MYSQL_PASSWORD: 123456
networks:
  net-spring-db:
    driver: bridge

application.properties:

server.port=8888

## user container name to replace ip
spring.datasource.url=jdbc:mysql://mysql-container:3306/rbac?characterEncoding=utf8&useSSL=false
spring.datasource.username=h2t
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

mybatis-plus.mapper-locations=classpath*:/mapper/*.xml
mybatis-plus.type-aliases-package=com.h2t.study.pojo
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
mybatis-plus.global-config.db-config.logic-delete-value=1
mybatis-plus.global-config.db-config.logic-not-delete-value=0

当我使用docker-compose up

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
smweb-container | 
smweb-container | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
smweb-container |   at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_111]
smweb-container |   at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_111]
smweb-container |   at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_111]
smweb-container |   at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_111]
smweb-container |   at com.mysql.jdbc.Util.handleNewInstance(Util.java:404) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
smweb-container |   at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:983) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
smweb-container |   at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:339) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
smweb-container |   at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2252) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
smweb-container |   at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2285) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
smweb-container |   at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2084) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
smweb-container |   at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:795) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
smweb-container |   at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
smweb-container |   at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_111]
smweb-container |   at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_111]
smweb-container |   at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_111]
smweb-container |   at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_111]
smweb-container |   at com.mysql.jdbc.Util.handleNewInstance(Util.java:404) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
smweb-container |   at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:400) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
smweb-container |   at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:327) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
smweb-container |   at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:136) ~[HikariCP-3.2.0.jar!/:na]
smweb-container |   at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:369) ~[HikariCP-3.2.0.jar!/:na]
smweb-container |   at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:198) ~[HikariCP-3.2.0.jar!/:na]
smweb-container |   at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:467) [HikariCP-3.2.0.jar!/:na]
smweb-container |   at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:541) [HikariCP-3.2.0.jar!/:na]
smweb-container |   at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) [HikariCP-3.2.0.jar!/:na]
smweb-container |   at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) [HikariCP-3.2.0.jar!/:na]

我可以使用docker exec -it mysql-container bash命令进入mysql容器,也可以使用mysql -uh2t -p123456连接mysql。同时,我检查了表,它已初始化,但是SpringBoot App仍然可以。 t连接到MySQL。
谢谢您能帮我吗。

0 个答案:

没有答案