ThingsBoard——Docker重启失败,报错Connection to localhost:5432 refused的解决方法

ThingsBoard——Docker重启失败,报错Connection to localhost:5432 refused的解决方法一、问题现在还没编译好thingsboard源代码,用的是docker搭建起来的环境。在写自定义节点,要打包好扔到docker里,再重启docker。后来发现经常重启失败,报错的错误也都是这样:2022-03-0508:53:23,164[main]ERRORcom.zaxxer.hikari.pool.HikariPool-HikariPool-1-Exceptionduringpoolinitialization.org.postgresql.util.PSQLExcepti

大家好,又见面了,我是你们的朋友全栈君。

一、问题

最近在写自定义规则节点,因为还没编译好thingsboard源代码,用的是docker搭建起来的环境。写好的规则节点后,要打包好扔到docker里,再重启docker。发现经常重启失败,报错的日志也都是这样:

2022-03-05 08:53:23,164 [main] ERROR com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Exception during pool initialization.
org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
	at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:303)
...

2022-03-05 08:53:25,187 [main] ERROR o.s.boot.SpringApplication - Application run failed
org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:161)
...

pg_ctl: could not open PID file "/data/db/postmaster.pid": Operation not permitted

二、原因

试过删除后重新run,也试过重新赋权限(好像第一次能成功),试过用docker-compose,结果都是一样。最后还是在issues里找到了解决方案,感谢alyf80提供的思路:

I think this is due to postgres not being properly shut down when the container is stopped; this leaves a stale postmaster.pid file in the data/db directory, which on the next run causes pg_ctl to try talking to a non-existing daemon and ultimately failing to start the db server. Deleting that file prior to starting the container reliably fixes the problem in my case.

I have no idea why this is happening. It looks to me that the TB process is ignoring (or simply not getting) the SIGTERM that Docker sends to initiate a graceful shutdown of the container, so after the default 10s shutdown timeout expires everything gets killed without stop-db.sh having had a chance to run.

大致意思:PostgreSQL在docker关闭时,可能没有正常接收到关闭指令,所以数据库进程一直在使用中,没有被释放,导致docker启动后无法连接上数据库

补充:应该是大多数时机都没关闭,偶尔才正常关闭,所以重试几次后,会有成功的

三、解决

既然是进程未释放原因,那就好办了,就在重启前,先把db给结束掉。这里提供多种方法(扩展思路,试过都OK)

# 【荐】法1:安全关闭容器里的数据库,也是alyf80所提到的
$ docker exec -it mytb stop-db.sh

# 法2:强制关闭
$ docker exec -it mytb killall postgres 

# 法3:强制关闭,进入容器内操作
$ docker exec -it mytb bash
# 或先用ps aux找到postgres的PID,然后kill -9 [PID]
$ killall postgres

根据这个原因分析,应该可以推断出修改权限后,为什么第一次成功了。修改权限后,db停止了,所以重启后也就正常了

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/150978.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 解决docker下载镜像速度过慢_docker image是什么

    解决docker下载镜像速度过慢_docker image是什么前言上一篇讲到pull镜像,但是pull镜像的时候下拉的速度实在感人,有什么解决办法吗?我们只需将docker镜像源修改为国内的将docker镜像源修改为国内的:在/etc/docker/d

    2022年7月30日
    7
  • Dockerfile add_dockerfile copy和add区别

    Dockerfile add_dockerfile copy和add区别前言Dockerfile中提供了两个非常相似的命令COPY和ADD,本文尝试解释这两个命令的基本功能,以及其异同点,然后总结其各自适合的应用场景。Build上下文的概念在使用dock

    2022年7月30日
    9
  • docker 离线安装_Docker安装

    docker 离线安装_Docker安装一.上传docker-offline-installer.tar并解压缩(1)tar-xvfdocker-offline-installer.tar如果在网上找不到docker-offline-installer.tar,可以在我的CSDN中点击[资源]去下载二.配置yum源(1)进入工作目录:cd/etc/yum.repos.d/(2)备份现有库源:mkdirrepo&&mv*.reporepo/…

    2022年9月26日
    2
  • docker复制文件到宿主机_下面哪几个属于docker网络模式

    docker复制文件到宿主机_下面哪几个属于docker网络模式自从Docker容器出现以来,容器的网络通信就一直是被关注的焦点,也是生产环境的迫切需求。容器的网络通信又可以分为两大方面:单主机容器上的相互通信,和跨主机的容器相互通信。 一、端口映射(局域网,外网此方式均可)。此种方式是将容器的某个端口映射到宿主机的某个端口,其它主机访问容器提供的服务需要通过宿主机的IP进行访问:dockerrun-p9000:8000–namec…

    2022年8月21日
    9
  • docker 安装 Mysql 5.7[通俗易懂]

    docker 安装 Mysql 5.7[通俗易懂]1.安装mysql5.7docker镜像拉取官方mysql5.7镜像dockerpullmysql:5.7查看镜像库dockerimages2.创建mysql容器在本地创建mysql的映射目录mkdir-p/root/mysql/data/root/mysql/logs/root/mysql/conf在/root/mysql/con…

    2022年10月10日
    4
  • docker(2)CentOS 7安装docker环境[通俗易懂]

    docker(2)CentOS 7安装docker环境[通俗易懂]前言前面一篇学了mac安装docker,这篇来学习在linux上安装docker环境准备Docker支持以下的CentOS版本,目前,CentOS仅发行版本中的内核支持Docker。Doc

    2022年7月29日
    9

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号