无法建立与数据库服务器的连接。码头工人

时间:2019-12-04 19:52:48

标签: mysql docker docker-compose

我将我的容器应用程序连接到数据库容器时遇到问题。数据库容器正在工作,我可以使用计算机上的应用程序连接到它。我尝试了不同的解决方案,但没有一个提供帮助,因此我决定在此处撰写我的文章。我提前为我的英语道歉。
我正在寻求帮助,因为我想知道我做错了什么。

这是我的代码:

application.properties

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
server.port=9200
spring.datasource.url=jdbc:mysql://localhost:3306/currency-converter?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.testWhileIdle=true
spring.datasource.validationQuery=SELECT 1
spring.jpa.properties.hibernate.id.new_generator_mappings=false
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.messages.encoding=UTF-8

Dockerfile

FROM openjdk:14-ea-15-jdk-alpine3.10
ADD target/currencyConverter-0.0.1-SNAPSHOT.jar .
EXPOSE 9200
CMD java -jar currencyConverter-0.0.1-SNAPSHOT.jar

docker-compose

version: '3.3'                         
services:                             
  api:                                
    build: ./currencyConverter    
    ports:
    - "9200:9200"                   
    depends_on:                       
    - localhost
  localhost:
    container_name: localhost      
    image: mysql               
    environment:                      
      MYSQL_PASSWORD: root
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: currency-converter
    ports:
    - "3306:3306"

PowerShell

localhost    | 2019-12-04 19:26:42+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.18-1debian9 started.
localhost    | 2019-12-04 19:26:42+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
localhost    | 2019-12-04 19:26:42+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.18-1debian9 started.
localhost    | 2019-12-04 19:26:42+00:00 [Note] [Entrypoint]: Initializing database files
localhost    | 2019-12-04T19:26:42.587405Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
localhost    | 2019-12-04T19:26:42.587499Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.18) initializing of server in progress as process 44
api_1        |
api_1        |   .   ____          _            __ _ _
api_1        |  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
api_1        | ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
api_1        |  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
api_1        |   '  |____| .__|_| |_|_| |_\__, | / / / /
api_1        |  =========|_|==============|___/=/_/_/_/
api_1        |  :: Spring Boot ::        (v2.0.4.RELEASE)
api_1        |
localhost    | 2019-12-04T19:26:45.434568Z 5 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
api_1        | 2019-12-04 19:26:45.544  INFO 1 --- [           main] c.e.c.CurrencyConverterApplication       : Starting CurrencyConverterApplication v0.0.1-SNAPSHOT on 55df45c0b438 with PID 1 (/currencyConverter-0.0.1-SNAPSHOT.jar started by root in /)
api_1        | 2019-12-04 19:26:45.556  INFO 1 --- [           main] c.e.c.CurrencyConverterApplication       : No active profile set, falling back to default profiles: default
api_1        | 2019-12-04 19:26:45.790  INFO 1 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@6e0e048a: startup date [Wed Dec 04 19:26:45 GMT 2019]; root of context hierarchy
api_1        | WARNING: An illegal reflective access operation has occurred
api_1        | WARNING: Illegal reflective access by org.springframework.cglib.core.ReflectUtils$1 (jar:file:/currencyConverter-0.0.1-SNAPSHOT.jar!/BOOT-INF/lib/spring-core-5.0.8.RELEASE.jar!/) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
api_1        | WARNING: Please consider reporting this to the maintainers of org.springframework.cglib.core.ReflectUtils$1
api_1        | WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
api_1        | WARNING: All illegal access operations will be denied in a future release
api_1        | 2019-12-04 19:26:48.655  INFO 1 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$44808c13] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
api_1        | 2019-12-04 19:26:49.396  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 9200 (http)
api_1        | 2019-12-04 19:26:49.456  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
api_1        | 2019-12-04 19:26:49.457  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.32
api_1        | 2019-12-04 19:26:49.489  INFO 1 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/opt/openjdk-14/lib/server:/opt/openjdk-14/lib:/opt/openjdk-14/../lib:/usr/java/packages/lib:/usr/lib64:/lib64:/lib:/usr/lib]
localhost    | 2019-12-04 19:26:49+00:00 [Note] [Entrypoint]: Database files initialized
localhost    | 2019-12-04 19:26:49+00:00 [Note] [Entrypoint]: Starting temporary server
api_1        | 2019-12-04 19:26:49.668  INFO 1 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
api_1        | 2019-12-04 19:26:49.668  INFO 1 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3906 ms
api_1        | 2019-12-04 19:26:49.870  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
api_1        | 2019-12-04 19:26:49.883  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
localhost    | 2019-12-04T19:26:49.841496Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
localhost    | 2019-12-04T19:26:49.841596Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.18) starting as process 93
api_1        | 2019-12-04 19:26:49.888  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
api_1        | 2019-12-04 19:26:49.888  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
api_1        | 2019-12-04 19:26:49.888  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
api_1        | Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
api_1        | 2019-12-04 19:26:50.327  INFO 1 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
api_1        | 2019-12-04 19:26:50.334  WARN 1 --- [           main] com.zaxxer.hikari.util.DriverDataSource  : Registered driver with driverClassName=com.mysql.jdbc.Driver was not found, trying direct instantiation.
localhost    | 2019-12-04T19:26:50.722255Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
localhost    | 2019-12-04T19:26:50.726892Z 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.
localhost    | 2019-12-04T19:26:50.752302Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.18'  socket: '/var/run/mysqld/mysqld.sock'  port: 0  MySQL Community Server - GPL.
localhost    | 2019-12-04 19:26:50+00:00 [Note] [Entrypoint]: Temporary server started.
localhost    | 2019-12-04T19:26:50.857626Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock'
localhost    | Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
localhost    | Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
localhost    | Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
localhost    | Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
localhost    | 2019-12-04 19:26:53+00:00 [Note] [Entrypoint]: Creating database currency-converter
localhost    |
localhost    | 2019-12-04 19:26:53+00:00 [Note] [Entrypoint]: Stopping temporary server
localhost    | 2019-12-04T19:26:53.910884Z 11 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.0.18).
api_1        | 2019-12-04 19:26:55.466 ERROR 1 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Exception during pool initialization.
api_1        |
api_1        | java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
api_1        |  at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:108) ~[mysql-connector-java-8.0.11.jar!/:8.0.11]
api_1        |  at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95) ~[mysql-connector-java-8.0.11.jar!/:8.0.11]

0 个答案:

没有答案