我在我的 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/