在Docker容器中,数据库容器和Java容器之间的连接被拒绝。

huangapple go评论114阅读模式
英文:

Connection refused in Docker container between database container and java container in docker

问题

我正在使用Docker Compose启动一个MySQL数据库和一个Spring Boot应用程序。

这是我的docker-compose.yml文件:

  1. version: '3'
  2. services:
  3. sql:
  4. image: mysql
  5. environment:
  6. MYSQL_DATABASE: people
  7. MYSQL_USER: root
  8. MYSQL_PASSWORD: my-secret-pw
  9. MYSQL_ROOT_PASSWORD: my-secret-pw
  10. ports:
  11. - "3307:3306"
  12. appointment-service:
  13. depends_on:
  14. - sql # This service depends on mysql. Start that first.
  15. environment: # Pass environment variables to the service
  16. - DATABASE_HOST=sql
  17. - DATABASE_USER=root
  18. - DATABASE_PASSWORD=my-secret-pw
  19. - DATABASE_NAME=people
  20. - DATABASE_PORT=3306
  21. links:
  22. - "sql:localhost"
  23. build: .
  24. ports:
  25. - "8081:8081"

这是我的application.properties文件:

  1. server.port=8081
  2. spring.datasource.url=jdbc:mysql://localhost:3306/people?useSSL=false&allowPublicKeyRetrieval=true
  3. spring.datasource.username=root
  4. spring.datasource.password=my-secret-pw
  5. url.organizations = https://hr.apografi.gov.gr/api/public/organizations
  6. url.db = jdbc:mysql://localhost:3306/people?serverTimezone=UTC
  7. url.username = root
  8. url.password = my-secret-pw

这是我创建Jdbi连接的代码:

  1. private Jdbi jdbi = Jdbi.create("jdbc:mysql://localhost:3306/people?useSSL=false&allowPublicKeyRetrieval=true", "root", "my-secret-pw");

无论我尝试什么,我都无法使容器在Docker内部进行通信。如果我单独启动SQL,我可以在主机上(在Datagrip中)看到连接。如果我从容器中运行Java程序,它会在我的主机上看到数据库。即使我运行docker-compose文件,两个容器也会被创建,我可以从主机访问它们。我知道在DockerVM内部数据库的名称为sql/。我尝试过使用sql名称和localhost名称(来自links属性)。但无论我尝试什么,我都无法从Docker内部的Java程序中访问它。

这是堆栈跟踪:

  1. docker-compose up --build
  2. ...(以下内容省略)

(注意:此处仅为原文的翻译,其中的代码和内容与原文保持一致,没有进行实际的更改。)

英文:

I am using docker compose to start a mysql da and a springboot app.

This is my docker-compose.yml

  1. version: '3'
  2. services:
  3. sql:
  4. image: mysql
  5. environment:
  6. MYSQL_DATABASE: people
  7. MYSQL_USER: root
  8. MYSQL_PASSWORD: my-secret-pw
  9. MYSQL_ROOT_PASSWORD: my-secret-pw
  10. ports:
  11. - "3307:3306"
  12. appointment-service:
  13. depends_on:
  14. - sql # This service depends on mysql. Start that first.
  15. environment: # Pass environment variables to the service
  16. - DATABASE_HOST=sql
  17. - DATABASE_USER=root
  18. - DATABASE_PASSWORD=my-secret-pw
  19. - DATABASE_NAME=people
  20. - DATABASE_PORT=3306
  21. links:
  22. - "sql:localhost"
  23. build: .
  24. ports:
  25. - "8081:8081"

This is my application.properties

  1. server.port=8081
  2. spring.datasource.url=jdbc:mysql://localhost:3306/people?useSSL=false&allowPublicKeyRetrieval=true
  3. spring.datasource.username=root
  4. spring.datasource.password=my-secret-pw
  5. url.organizations = https://hr.apografi.gov.gr/api/public/organizations
  6. url.db = jdbc:mysql://localhost:3306/people?serverTimezone=UTC
  7. url.username = root
  8. url.password = my-secret-pw

and here I create the jdbi:

  1. private Jdbi jdbi = Jdbi.create("jdbc:mysql://localhost:3306/people?useSSL=false&allowPublicKeyRetrieval=true", "root", "my-secret-pw");

no matter what i tried i could not make the containers talk to each-other inside the docker.
If I start the sql alone, I can see the connection in my host machine (in datagrip).
If I run the java program from container, it sees the database in my host machine.
Even if I run my docker-compose file, both the containers are created and I can access them from my host machine. I know that inside the DockerVM the database exists with the name sql/ I have tried both with sql name and the localhost name (from the links property). But no matter what I tried I was not able to access it from my java program inside the Docker.

Here is the stackTrace

  1. docker-compose up --build
  2. Building appointment-service
  3. Step 1/4 : FROM openjdk:8-jdk-alpine
  4. ---> a3562aa0b991
  5. Step 2/4 : ARG JAR_FILE=target/*.jar
  6. ---> Using cache
  7. ---> 603b2c59103f
  8. Step 3/4 : COPY ${JAR_FILE} app.jar
  9. ---> Using cache
  10. ---> 4783d891166c
  11. Step 4/4 : ENTRYPOINT ["java","-jar","/app.jar"]
  12. ---> Using cache
  13. ---> 24deebb3147a
  14. Successfully built 24deebb3147a
  15. Successfully tagged appointments-service_appointment-service:latest
  16. Starting appointments-service_sql_1 ... done
  17. Starting appointments-service_appointment-service_1 ... done
  18. Attaching to appointments-service_sql_1, appointments-service_appointment-service_1
  19. sql_1 | 2020-09-11 16:47:27+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.21-1debian10 started.
  20. sql_1 | 2020-09-11 16:47:27+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
  21. sql_1 | 2020-09-11 16:47:27+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.21-1debian10 started.
  22. sql_1 | 2020-09-11T16:47:28.258555Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.21) starting as process 1
  23. sql_1 | 2020-09-11T16:47:28.275621Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
  24. sql_1 | 2020-09-11T16:47:28.641173Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
  25. sql_1 | 2020-09-11T16:47:28.823110Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
  26. sql_1 | 2020-09-11T16:47:28.988711Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
  27. sql_1 | 2020-09-11T16:47:28.989000Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
  28. sql_1 | 2020-09-11T16:47:28.999907Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
  29. sql_1 | 2020-09-11T16:47:29.056993Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.21' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.
  30. appointment-service_1 |
  31. appointment-service_1 | . ____ _ __ _ _
  32. appointment-service_1 | /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
  33. appointment-service_1 | ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
  34. appointment-service_1 | \\/ ___)| |_)| | | | | || (_| | ) ) ) )
  35. appointment-service_1 | ' |____| .__|_| |_|_| |_\__, | / / / /
  36. appointment-service_1 | =========|_|==============|___/=/_/_/_/
  37. appointment-service_1 | :: Spring Boot :: (v2.3.2.RELEASE)
  38. appointment-service_1 |
  39. appointment-service_1 | 2020-09-11 16:47:30.628 INFO 1 --- [ main] c.a.service.ServiceApplication : Starting ServiceApplication v0.0.1-SNAPSHOT on 5bbe74d3ddb0 with PID 1 (/app.jar started by root in /)
  40. appointment-service_1 | 2020-09-11 16:47:30.635 INFO 1 --- [ main] c.a.service.ServiceApplication : No active profile set, falling back to default profiles: default
  41. appointment-service_1 | 2020-09-11 16:47:32.822 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFERRED mode.
  42. appointment-service_1 | 2020-09-11 16:47:32.876 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 33ms. Found 0 JPA repository interfaces.
  43. appointment-service_1 | 2020-09-11 16:47:34.254 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8081 (http)
  44. appointment-service_1 | 2020-09-11 16:47:34.300 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
  45. appointment-service_1 | 2020-09-11 16:47:34.302 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.37]
  46. appointment-service_1 | 2020-09-11 16:47:34.472 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
  47. appointment-service_1 | 2020-09-11 16:47:34.473 INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3697 ms
  48. appointment-service_1 | 2020-09-11 16:47:35.055 INFO 1 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
  49. appointment-service_1 | 2020-09-11 16:47:35.100 INFO 1 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
  50. appointment-service_1 | 2020-09-11 16:47:36.338 ERROR 1 --- [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization.
  51. appointment-service_1 |
  52. appointment-service_1 | com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
  53. appointment-service_1 |
  54. appointment-service_1 | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
  55. appointment-service_1 | at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
  56. appointment-service_1 | at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
  57. appointment-service_1 | at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
  58. appointment-service_1 | at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
  59. appointment-service_1 | at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
  60. appointment-service_1 | at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:197) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
  61. appointment-service_1 | at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-3.4.5.jar!/:na]
  62. appointment-service_1 | at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:358) ~[HikariCP-3.4.5.jar!/:na]
  63. appointment-service_1 | at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206) ~[HikariCP-3.4.5.jar!/:na]
  64. appointment-service_1 | at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:477) [HikariCP-3.4.5.jar!/:na]
  65. appointment-service_1 | at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:560) [HikariCP-3.4.5.jar!/:na]
  66. appointment-service_1 | at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) [HikariCP-3.4.5.jar!/:na]
  67. appointment-service_1 | at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) [HikariCP-3.4.5.jar!/:na]
  68. appointment-service_1 | at org.springframework.jdbc.datasource.DataSourceUtils.fetchConnection(DataSourceUtils.java:158) [spring-jdbc-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
  69. appointment-service_1 | at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:116) [spring-jdbc-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
  70. appointment-service_1 | at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:79) [spring-jdbc-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
  71. appointment-service_1 | at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:324) [spring-jdbc-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
  72. appointment-service_1 | at org.springframework.boot.jdbc.EmbeddedDatabaseConnection.isEmbedded(EmbeddedDatabaseConnection.java:120) [spring-boot-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
  73. appointment-service_1 | at org.springframework.boot.autoconfigure.orm.jpa.HibernateDefaultDdlAutoProvider.getDefaultDdlAuto(HibernateDefaultDdlAutoProvider.java:42) [spring-boot-autoconfigure-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
  74. appointment-service_1 | at org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration.lambda$getVendorProperties$1(HibernateJpaConfiguration.java:130) [spring-boot-autoconfigure-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
  75. appointment-service_1 | at org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings.getDdlAuto(HibernateSettings.java:41) ~[spring-boot-autoconfigure-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
  76. appointment-service_1 | at org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties.determineDdlAuto(HibernateProperties.java:136) ~[spring-boot-autoconfigure-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
  77. appointment-service_1 | at org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties.getAdditionalProperties(HibernateProperties.java:102) ~[spring-boot-autoconfigure-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
  78. appointment-service_1 | at org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties.determineHibernateProperties(HibernateProperties.java:94) ~[spring-boot-autoconfigure-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
  79. appointment-service_1 | at org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration.getVendorProperties(HibernateJpaConfiguration.java:132) [spring-boot-autoconfigure-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
  80. appointment-service_1 | at org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.entityManagerFactory(JpaBaseConfiguration.java:133) ~[spring-boot-autoconfigure-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
  81. appointment-service_1 | at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_212]
  82. appointment-service_1 | at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_212]
  83. appointment-service_1 | at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_212]
  84. appointment-service_1 | at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_212]
  85. appointment-service_1 | at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
  86. appointment-service_1 | at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:650) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
  87. appointment-service_1 | at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:635) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
  88. appointment-service_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1336) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
  89. appointment-service_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1176) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
  90. appointment-service_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:556) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
  91. appointment-service_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
  92. appointment-service_1 | at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
  93. appointment-service_1 | at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
  94. appointment-service_1 | at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
  95. appointment-service_1 | at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
  96. appointment-service_1 | at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1109) ~[spring-context-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
  97. appointment-service_1 | at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869) ~[spring-context-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
  98. appointment-service_1 | at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551) ~[spring-context-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
  99. appointment-service_1 | at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
  100. appointment-service_1 | at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) ~[spring-boot-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
  101. appointment-service_1 | at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) ~[spring-boot-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
  102. appointment-service_1 | at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
  103. appointment-service_1 | at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
  104. appointment-service_1 | at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) ~[spring-boot-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
  105. appointment-service_1 | at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
  106. appointment-service_1 | at com.appointments.service.ServiceApplication.main(ServiceApplication.java:10) ~[classes!/:0.0.1-SNAPSHOT]
  107. appointment-service_1 | at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_212]
  108. appointment-service_1 | at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_212]
  109. appointment-service_1 | at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_212]
  110. appointment-service_1 | at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_212]
  111. appointment-service_1 | at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) ~[app.jar:0.0.1-SNAPSHOT]
  112. appointment-service_1 | at org.springframework.boot.loader.Launcher.launch(Launcher.java:109) ~[app.jar:0.0.1-SNAPSHOT]
  113. appointment-service_1 | at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) ~[app.jar:0.0.1-SNAPSHOT]
  114. appointment-service_1 | at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88) ~[app.jar:0.0.1-SNAPSHOT]
  115. appointment-service_1 | Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
  116. appointment-service_1 |
  117. appointment-service_1 | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
  118. appointment-service_1 | at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_212]
  119. appointment-service_1 | at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_212]
  120. appointment-service_1 | at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_212]
  121. appointment-service_1 | at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_212]
  122. appointment-service_1 | at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
  123. appointment-service_1 | at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
  124. appointment-service_1 | at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
  125. appointment-service_1 | at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
  126. appointment-service_1 | at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
  127. appointment-service_1 | at com.mysql.cj.NativeSession.connect(NativeSession.java:144) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
  128. appointment-service_1 | at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:956) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
  129. appointment-service_1 | at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:826) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
  130. appointment-service_1 | ... 57 common frames omitted
  131. appointment-service_1 | Caused by: java.net.ConnectException: Connection refused (Connection refused)
  132. appointment-service_1 | at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_212]
  133. appointment-service_1 | at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_212]
  134. appointment-service_1 | at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_212]
  135. appointment-service_1 | at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_212]
  136. appointment-service_1 | at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_212]
  137. appointment-service_1 | at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_212]
  138. appointment-service_1 | at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
  139. appointment-service_1 | at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
  140. appointment-service_1 | ... 60 common frames omitted
  141. appointment-service_1

|

答案1

得分: 2

  • links:在这种情况下已被弃用且无用,只需删除该行。
  • 在Java容器内部使用的localhost将指向容器本身。您必须使用为SQL容器指定的服务名称(在您的情况下为sql)。服务名称将解析为SQL容器的IP。
  • depends_on:将使Java容器等待SQL容器启动,但这并不保证SQL已经启动并且可以接受连接。有几种方法可以解决这个问题(wait-for-it,restart: on-failure,这个帖子等)。
  • 如果您只打算使用root用户,那么不需要指定MYSQL_USER: rootMYSQL_PASSWORD: my-secret-pw。(这与您当前的问题无关)
  1. version: '3'
  2. services:
  3. sql:
  4. image: mysql
  5. environment:
  6. MYSQL_DATABASE: people
  7. MYSQL_ROOT_PASSWORD: my-secret-pw
  8. ports:
  9. - "3307:3306"
  10. appointment-service:
  11. depends_on:
  12. - sql # This service depends on mysql. Start that first.
  13. restart: on-failure
  14. environment: # Pass environment variables to the service
  15. - DATABASE_HOST=sql
  16. - DATABASE_USER=root
  17. - DATABASE_PASSWORD=my-secret-pw
  18. - DATABASE_NAME=people
  19. - DATABASE_PORT=3306
  20. build: .
  21. ports:
  22. - "8081:8081"
  1. server.port=8081
  2. spring.datasource.url=jdbc:mysql://sql:3306/people?useSSL=false&allowPublicKeyRetrieval=true
  3. spring.datasource.username=root
  4. spring.datasource.password=my-secret-pw
  5. url.organizations = https://hr.apografi.gov.gr/api/public/organizations
  6. url.db = jdbc:mysql://sql:3306/people?serverTimezone=UTC
  7. url.username = root
  8. url.password = my-secret-pw
  1. private Jdbi jdbi = Jdbi.create("jdbc:mysql://sql:3306/people?useSSL=false&allowPublicKeyRetrieval=true", "root", "my-secret-pw");
英文:
  • links: is deprecated and useless in this case - just delete that line.
  • localhost used inside the java container will point to the container itself. You must use the service name that you gave for your sql container(sql in your case). The service name will be resolved to the IP of the SQL container.
  • depends_on: will make the java containers wait for the SQL containers to start, but this does not guarantee that the SQL is up and running, ready to accept connections. The are several ways to solve this problem(wait-for-it, restart: on-failure, this post etc.)
  • you don't need to specify the MYSQL_USER: root nor the MYSQL_PASSWORD: my-secret-pw if your intent is to use just the root user.(this has nothing to do with your current problem)
  1. version: '3'
  2. services:
  3. sql:
  4. image: mysql
  5. environment:
  6. MYSQL_DATABASE: people
  7. MYSQL_ROOT_PASSWORD: my-secret-pw
  8. ports:
  9. - "3307:3306"
  10. appointment-service:
  11. depends_on:
  12. - sql # This service depends on mysql. Start that first.
  13. restart: on-failure
  14. environment: # Pass environment variables to the service
  15. - DATABASE_HOST=sql
  16. - DATABASE_USER=root
  17. - DATABASE_PASSWORD=my-secret-pw
  18. - DATABASE_NAME=people
  19. - DATABASE_PORT=3306
  20. build: .
  21. ports:
  22. - "8081:8081"
  1. server.port=8081
  2. spring.datasource.url=jdbc:mysql://sql:3306/people?useSSL=false&allowPublicKeyRetrieval=true
  3. spring.datasource.username=root
  4. spring.datasource.password=my-secret-pw
  5. url.organizations = https://hr.apografi.gov.gr/api/public/organizations
  6. url.db = jdbc:mysql://sql:3306/people?serverTimezone=UTC
  7. url.username = root
  8. url.password = my-secret-pw
  1. private Jdbi jdbi = Jdbi.create("jdbc:mysql://sql:3306/people?useSSL=false&allowPublicKeyRetrieval=true", "root", "my-secret-pw");

答案2

得分: 0

localhost始终指向回环接口(可以是127.0.0.1::1)。您不能也不应该尝试覆盖此设置。

在使用docker-compose时,您可以使用服务名称作为主机名,因此数据库的正确JDBC URL应为:

  1. jdbc:mysql://sql:3306/people?useSSL=false&allowPublicKeyRetrieval=true
英文:

localhost always points to the loopback interface (either 127.0.0.1 or ::1). You cannot, and should not attempt to, override this.

When using docker-compose, you can user service names as hostnames, so the proper JDBC URL for your database would be:

  1. jdbc:mysql://sql:3306/people?useSSL=false&allowPublicKeyRetrieval=true

huangapple
  • 本文由 发表于 2020年9月12日 00:56:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63851313.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定