mysql - Spring Boot服务启动后运行失败,原因是MySQL数据库还没有准备好

标签 mysql linux ubuntu spring-boot init.d

我在我的 Ubuntu 16 服务器上安装了一个 Spring Boot 应用程序作为 init.d 服务在系统启动时自动运行 as described here .

我还有 Spring Boot 应用程序需要的 MySQL 数据库。问题是,在 Spring boot 尝试连接到 db 后应用程序崩溃:

2016-09-21 12:54:32.382 ERROR 1384 --- [           main] o.a.tomcat.jdbc.pool.ConnectionPool      : Unable to create initial connections of pool.

com.mysql.jdbc.exceptions.jdbc4.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 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_101]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_101]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_101]
...
Caused by: java.net.ConnectException: Connection refused
        at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_101]
...
2016-09-21 12:54:32.384  WARN 1384 --- [           main] o.h.e.jdbc.internal.JdbcServicesImpl     : HHH000342: Could not obtain connection to query metadata : Communications link failure

我想,这是因为 MySQL 数据库还没有准备好。但这会导致 Spring Boot App 完全失败。

当我尝试再次手动启动相同的服务时,一切正常。

所以,我的问题是,我可以定义某种 init.d 依赖项吗?所以我的 Spring Boot 应用程序会一直等到数据库准备好?

最佳答案

我正在使用 systemd。

在您的链接中,在这部分:

[Unit]
Description=myapp
After=mysql.service

[Service]
User=myapp
ExecStart=/var/myapp/myapp.jar
SuccessExitStatus=143

[Install]
WantedBy=multi-user.target

像我展示的那样更改 After 行,用 mysql 替换 syslog。

关于mysql - Spring Boot服务启动后运行失败,原因是MySQL数据库还没有准备好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39617158/

相关文章:

php - 插入 php 时数据发生变化

mysql - Eloquent 表关系

java - 在 Linux 中进行 Mysql 数据库备份显示 java.io.IOException :

c - 从 64 位应用程序使用 32 位共享库?

带身份验证的PHP登录脚本,阻止用户访问特定页面

linux - 一次处理大文件 TR + SED

c++ - 在静态库中发现对成员函数的 undefined reference

Ubuntu 16 UFW 没有登录/var/log/ufw.log 也没有其他位置

ubuntu - 日志解析 stackdriver 中来自 textPayload 的警报?

ubuntu - 如何使用 cron 作为 root 用户重新加载远程服务器的 nginx