java - Docker:无法连接 Spring Boot 和 MYSQL

标签 java mysql spring spring-boot docker

我尝试使用 Dockerfile 或 Docker-compose 将我的项目容器化。首先,我使用命令从 mysql 创建容器:docker run --name ms -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password mysql然后我在容器中创建数据库和表并将数据放入表中,然后构建我的项目的镜像:docker build -f Dockerfile -t week .我有日志:

Sending build context to Docker daemon  212.1MB
Step 1/4 : FROM openjdk
---> 30503f5328a0
Step 2/4 : ADD target/week10-1.0-SNAPSHOT.jar week10.jar
---> 727648b3ec10
Step 3/4 : EXPOSE 8080
---> Running in d60c2566ac0b
Removing intermediate container d60c2566ac0b
---> 59b94f7abc52
Step 4/4 : ENTRYPOINT ["java","-jar","week10.jar"]
---> Running in 20627cbee09e
Removing intermediate container 20627cbee09e
---> 725bc9303c08
Successfully built 725bc9303c08
Successfully tagged week:latest
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It
is recommended to double check and reset permissions for sensitive files and directories.

接下来我用我的图像创建容器:docker run -p 8080:8080 --name weeek --link ms:mysql -d week我的周日志:
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.5.RELEASE)

2020-04-12 05:51:40.095  INFO 1 --- [           main] com.fruitshop.Application                : Starting Application v1.0-SNAPSHOT on 0a26be9daad3 with PID 1 (/week10.jar started by root in /)
2020-04-12 05:51:40.237  INFO 1 --- [           main] com.fruitshop.Application                : No active profile set, falling back to default profiles: default
2020-04-12 05:51:50.398  INFO 1 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2020-04-12 05:51:51.647  INFO 1 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 1144ms. Found 1 JPA repository interfaces.
2020-04-12 05:52:00.131  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-04-12 05:52:00.371  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-04-12 05:52:00.377  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.31]
2020-04-12 05:52:01.025  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-04-12 05:52:01.031  INFO 1 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 20029 ms
2020-04-12 05:52:05.034  INFO 1 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2020-04-12 05:52:12.915  INFO 1 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2020-04-12 05:52:15.289  INFO 1 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-04-12 05:52:17.719  INFO 1 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.12.Final
2020-04-12 05:52:19.813  INFO 1 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-04-12 05:52:21.547  INFO 1 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2020-04-12 05:52:31.942  INFO 1 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2020-04-12 05:52:32.009  INFO 1 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2020-04-12 05:52:36.313  WARN 1 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2020-04-12 05:52:43.551  INFO 1 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-04-12 05:52:52.483  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-04-12 05:52:52.502  INFO 1 --- [           main] com.fruitshop.Application                : Started Application in 79.232 seconds (JVM running for 88.015)


jetty worker 日志毫秒

2020-04-12 04:50:12+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.19-1debian10 started.
2020-04-12 04:50:16+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2020-04-12 04:50:16+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.19-1debian10 started.
2020-04-12 04:50:16+00:00 [Note] [Entrypoint]: Initializing database files
2020-04-12T04:50:16.737441Z 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.
2020-04-12T04:50:16.737895Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.19) initializing of server in progress as process 41
2020-04-12T04:50:23.656403Z 5 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
2020-04-12 04:50:41+00:00 [Note] [Entrypoint]: Database files initialized
2020-04-12 04:50:41+00:00 [Note] [Entrypoint]: Starting temporary server
2020-04-12T04:50:42.568802Z 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.
2020-04-12T04:50:42.569282Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.19) starting as process 91
2020-04-12T04:50:44.820291Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2020-04-12T04:50:44.838763Z 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.
2020-04-12T04:50:44.956526Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.19'  socket: '/var/run/mysqld/mysqld.sock'  port: 0  MySQL Community Server - GPL.
2020-04-12 04:50:45+00:00 [Note] [Entrypoint]: Temporary server started.
2020-04-12T04:50:45.205952Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock'
Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.

2020-04-12 04:51:30+00:00 [Note] [Entrypoint]: Stopping temporary server
2020-04-12T04:51:30.467028Z 10 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.0.19).
2020-04-12T04:51:32.552114Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.19)  MySQL Community Server - GPL.
2020-04-12 04:51:33+00:00 [Note] [Entrypoint]: Temporary server stopped

2020-04-12 04:51:33+00:00 [Note] [Entrypoint]: MySQL init process done. Ready for start up.

2020-04-12T04:51:34.552483Z 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.
2020-04-12T04:51:34.552877Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.19) starting as process 1
2020-04-12T04:51:36.174094Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2020-04-12T04:51:36.186916Z 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.
2020-04-12T04:51:36.315329Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.19'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.
2020-04-12T04:51:36.509687Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060
mbind: Operation not permitted


jetty worker ps

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
0a26be9daad3        week                "java -jar week10.jar"   15 hours ago        Up 15 hours         0.0.0.0:8080->8080/tcp              weeek
e2b97b4a01bd        mysql               "docker-entrypoint.s…"   16 hours ago        Up 16 hours         0.0.0.0:3306->3306/tcp, 33060/tcp   ms


如果我通过 postman 发送请求,我会得到:
Could not get any response
There was an error connecting to http://localhost:8080/products/allProducts.

或者如果我输入浏览器:localhost:8080/products/allProducts我得到:Page not found
我所有的项目都在这里:Project

最佳答案

我注意到您的代码有几个问题:

  • Docker-撰写文件
  • 使用量 ./mysql-data:/var/lib/mysql 你更有可能遇到
    这个错误
    [ERROR] The designated data directory /var/lib/mysql/ is unusable
    将其更改为任何目录,例如 /data/mysql
  • 的概率Java服务容器将在 之前开始数据库容器 非常高。因此需要添加restart-on-failure optionwait-for-it.shsleep option .这些选项将启用服务容器 等待数据库容器 开始。
        java:
           restart: on-failure
    
  • 属性文件。

    Java 服务属性文件指向不存在的容器 ms
    将其更改为
    spring.datasource.url=jdbc:mysql://db:3306/fruitshop?serverTimezone=UTC
    代替
    spring.datasource.url=jdbc:mysql://ms:3306/fruitshop?serverTimezone=UTC

  • 上述更改运行后
    docker-compose up --build
    Github Pull Request

    关于java - Docker:无法连接 Spring Boot 和 MYSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61167859/

    相关文章:

    mysql - 搜索以 X & Y 开头的字符串返回 0 个结果 MySQL

    java - 对现有 webapp 进行 Spring Boot 健康检查

    java - 在运行时排除 Spring 配置文件的故障

    java - Java Spring 中的 session 劫持

    java - 带有 resultClass 参数的 NamedQuery 未返回查询结果的预期 "type"

    java - do-while 循环没有按预期工作

    java - Talend 中的 HOUR_OF_DAY 错误?

    java - 支持包 "com.ibm.misc"及其类

    php - wordpress 无法使用定义的常量连接到 mysql

    java - 如何在 Java 中使用 JDBC 从 MySQL 检索数学方程式?