无法从我在docker中运行的java springboot应用程序连接到mysql(在docker中运行)

时间:2020-04-26 11:35:44

标签: java mysql spring-boot docker

我有一个简单的spring boot应用程序,可以在使用本地mysql数据库时完美运行。但是当我将它托管在docker中时,它无法启动,因为它无法连接到托管在docker容器中的mysql。 我对docker映像的appliactaion.properties是:

logging.level.org.springframework.web = info
spring.mvc.view.prefix = /WEB-INF/jsp/
spring.mvc.view.suffix = .jsp
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=jdbc:mysql://mysql:3306/todo_example
spring.datasource.username=todouser
spring.datasource.password=todopassword
spring.jpa.hibernate.naming.physical-`strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl`

和mysql docker ps命令显示:

a43d27ade511        mysql:5             "docker-entrypoint.s…"   About an hour ago   Up About an hour    33060/tcp, 0.0.0.0:3307->3306/tcp   mysql

Spring启动应用程序日志显示:

2020-04-26 10:39:36.651 ERROR 1 --- [           main] com.zaxxer.hikari.pool.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) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:197) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
    at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-3.4.2.jar!/:na]

0 个答案:

没有答案