【问题标题】:Spring Boot Service fail to run after startup because MySQL Database is not ready yet由于 MySQL 数据库尚未准备好,Spring Boot Service 启动后无法运行
【发布时间】:2017-01-29 17:45:14
【问题描述】:

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

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

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 应用程序要等到数据库准备好?

【问题讨论】:

    标签: mysql linux ubuntu spring-boot init.d


    【解决方案1】:

    我正在使用 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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-01
      • 2018-03-15
      • 2018-06-05
      • 2018-05-11
      • 2021-10-19
      • 2016-05-25
      • 2020-08-13
      相关资源
      最近更新 更多