# PG::ConnectionBad 导致网站暂时崩溃

**URL:** <https://meta.discourse.org/t/pg-connectionbad-causes-site-to-temporarily-crash/146590>\
**Category:** Support\
**Created:** [2020年四月2日 15:41 UTC](https://meta.discourse.org/t/pg-connectionbad-causes-site-to-temporarily-crash/146590 "2020-04-02T15:41:48Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![LlamaSensei](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/llamasensei/32/133455_2.png) [@LlamaSensei](https://meta.discourse.org/u/LlamaSensei)\
**Post date:** [2020年四月2日 15:41 UTC](https://meta.discourse.org/t/pg-connectionbad-causes-site-to-temporarily-crash/146590/1 "2020-04-02T15:41:49Z")

</div>

在错误日志中，我间歇性地收到以下错误。这会导致网站在 15 到 30 秒内无法访问，直到恢复。

Info 错误信息：

> ****
>
> PG::ConnectionBad（致命错误：数据库系统正处于恢复模式）/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/pg-1.2.3/lib/pg.rb:58:in `initialize`

Env 错误信息：

> ****
>
> ![image](https://global.discourse-cdn.com/meta/original/3X/4/4/44b908279b7ef55c7675a9d8d1fcb131486a232d.png)

* * *

我在查看这些日志时发现一件非常奇怪的事：系统似乎在查找 /var/www/… 下的文件夹，但该文件夹在服务器上并不存在。它应该存在吗？任何帮助都将不胜感激。

---

<div class="post-metadata">

**Author:** ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)\
**Post date:** [2020年四月2日 16:28 UTC](https://meta.discourse.org/t/pg-connectionbad-causes-site-to-temporarily-crash/146590/2 "2020-04-02T16:28:36Z")

</div>

如果数据库损坏，那么网站必然也会出问题。你需要找出原因。最常见的原因是内存不足和磁盘空间不足。我猜是内存问题。你有多少内存？`free -h` 的输出是什么？

> [@LlamaSensei](#):
>
> 我在查看这些日志时发现一件非常奇怪的事：系统似乎在查找 /var/www/… 下的文件夹，但该文件夹在服务器上并不存在。

那是容器内部的路径。如果你执行 `./launcher enter app`，就能看到它在那里。

---

<div class="post-metadata">

**Author:** ![LlamaSensei](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/llamasensei/32/133455_2.png) [@LlamaSensei](https://meta.discourse.org/u/LlamaSensei)\
**Post date:** [2020年四月2日 16:31 UTC](https://meta.discourse.org/t/pg-connectionbad-causes-site-to-temporarily-crash/146590/3 "2020-04-02T16:31:11Z")

</div>

我想你是对的。这是 _free -h_ 的输出结果。

![image](https://global.discourse-cdn.com/meta/original/3X/8/5/85c269ff060955d27ca4f0954378a8f3f9c47ba9.png)

131M 的可用内存在我看来似乎并不多。

---

<div class="post-metadata">

**Author:** ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)\
**Post date:** [2020年四月2日 16:33 UTC](https://meta.discourse.org/t/pg-connectionbad-causes-site-to-temporarily-crash/146590/4 "2020-04-02T16:33:19Z")

</div>

好吧，你有 646M 可用内存，而 4GB 的内存通常已足够，无需使用交换空间。你的数据库有多大？流量有多少？服务器上除了 Discourse 还在运行其他服务吗？

---

<div class="post-metadata">

**Author:** ![LlamaSensei](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/llamasensei/32/133455_2.png) [@LlamaSensei](https://meta.discourse.org/u/LlamaSensei)\
**Post date:** [2020年四月2日 16:39 UTC](https://meta.discourse.org/t/pg-connectionbad-causes-site-to-temporarily-crash/146590/5 "2020-04-02T16:39:19Z")

</div>

服务器上除了 Discourse 外没有其他服务在运行。

为了获取大小，我运行了以下命令：

```
./launcher enter app
rails db
SELECT pg_size_pretty(pg_database_size(current_database()));

```

输出结果为 4650 MB（1 行）

关于流量：  
昨天有 1,200 次页面浏览量，过去一周为 12,400 次。过去一周大约有 160 名用户，任意时刻的并发用户数约为 80 人。

---

<div class="post-metadata">

**Author:** ![Stephen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/stephen/32/95011_2.png) [@Stephen](https://meta.discourse.org/u/Stephen)\
**Post date:** [2020年四月2日 16:54 UTC](https://meta.discourse.org/t/pg-connectionbad-causes-site-to-temporarily-crash/146590/6 "2020-04-02T16:54:47Z")

</div>

如果您有闲置的磁盘空间，请添加交换分区，为自己争取一些临时的缓冲空间。

---

<div class="post-metadata">

**Author:** ![LlamaSensei](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/llamasensei/32/133455_2.png) [@LlamaSensei](https://meta.discourse.org/u/LlamaSensei)\
**Post date:** [2020年四月2日 18:23 UTC](https://meta.discourse.org/t/pg-connectionbad-causes-site-to-temporarily-crash/146590/7 "2020-04-02T18:23:24Z")

</div>

我在那儿加了一个 GB 的交换空间。我们看看今晚高峰流量时会发生什么。

![image](https://global.discourse-cdn.com/meta/original/3X/c/5/c5e2a30b41fd231fc4c3535e141955e28caa4d18.png)

---

<div class="post-metadata">

**Author:** ![LlamaSensei](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/llamasensei/32/133455_2.png) [@LlamaSensei](https://meta.discourse.org/u/LlamaSensei)\
**Post date:** [2020年四月3日 15:39 UTC](https://meta.discourse.org/t/pg-connectionbad-causes-site-to-temporarily-crash/146590/8 "2020-04-03T15:39:53Z")

</div>

> [@Stephen](#):
>
> 如果你有闲置的磁盘空间，可以添加交换空间（swap）来争取一些临时的缓冲时间。

你太棒了。1 GB 的交换空间似乎彻底解决了所有崩溃问题。说实话，我很庆幸这是内存（RAM）问题而不是数据库（DB）问题。我打算在周末增加服务器的内存容量。非常感谢所有在这个帖子中提供帮助的朋友们！

---

<div class="post-metadata">

**Author:** ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)\
**Post date:** [2020年四月3日 21:15 UTC](https://meta.discourse.org/t/pg-connectionbad-causes-site-to-temporarily-crash/146590/9 "2020-04-03T21:15:42Z")

</div>

> [@LlamaSensei](#):
>
> 我打算在周末增加服务器的内存容量。

完成后，请重新运行 `discourse-setup` 以重置内存参数。

---

<div class="post-metadata">

**Author:** ![Stephen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/stephen/32/95011_2.png) [@Stephen](https://meta.discourse.org/u/Stephen)\
**Post date:** [2020年四月3日 21:28 UTC](https://meta.discourse.org/t/pg-connectionbad-causes-site-to-temporarily-crash/146590/10 "2020-04-03T21:28:54Z")

</div>

我很好奇它们一开始是否调校得当。🤷🏻‍♂️

---

<div class="post-metadata">

**Author:** ![LlamaSensei](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/llamasensei/32/133455_2.png) [@LlamaSensei](https://meta.discourse.org/u/LlamaSensei)\
**Post date:** [2020年四月5日 17:07 UTC](https://meta.discourse.org/t/pg-connectionbad-causes-site-to-temporarily-crash/146590/11 "2020-04-05T17:07:10Z")

</div>

说实话不太确定。在此之前，我们在更换硬件后曾进行过重建和服务器重启。我推测，重启后 Discourse 应该会检测到任何系统变更。

在安装了交换文件后，有时“内存”使用量会超过 4 GB，因此这显然是在我们最繁忙时段导致服务器崩溃的问题所在，而该时段与服务器崩溃的时间段完全吻合。我们今天进行了升级，现在网站运行非常顺畅。

---

<div class="post-metadata">

**Author:** ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)\
**Post date:** [2020年四月5日 17:16 UTC](https://meta.discourse.org/t/pg-connectionbad-causes-site-to-temporarily-crash/146590/12 "2020-04-05T17:16:23Z")

</div>

> [@LlamaSensei](#):
>
> 我本以为重启后，Discourse 应该能检测到任何系统变更。

但事实并非如此。有些设置（例如控制数据库可用内存大小的参数）是嵌入在 `app.yml` 中的。你可以手动编辑它们，或者重新运行 `discourse-setup`。

---

<div class="post-metadata">

**Author:** ![LlamaSensei](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/llamasensei/32/133455_2.png) [@LlamaSensei](https://meta.discourse.org/u/LlamaSensei)\
**Post date:** [2020年四月5日 17:19 UTC](https://meta.discourse.org/t/pg-connectionbad-causes-site-to-temporarily-crash/146590/13 "2020-04-05T17:19:15Z")

</div>

我今天重新运行了 discourse-setup 函数，因为之前我不知道这一点。这对我来说是全新的。

---

<div class="post-metadata">

**Author:** ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)\
**Post date:** [2020年五月5日 17:19 UTC](https://meta.discourse.org/t/pg-connectionbad-causes-site-to-temporarily-crash/146590/14 "2020-05-05T17:19:37Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
