连接到在 Docker 容器中运行的数据库时,sails-postgresql“poolSize”太高错误
Posted
技术标签:
【中文标题】连接到在 Docker 容器中运行的数据库时,sails-postgresql“poolSize”太高错误【英文标题】:sails-postgresql "poolSize" too high error when connecting to database running in Docker container 【发布时间】:2019-04-12 00:25:57 【问题描述】:我正在构建一个连接到 PostgreSQL 数据库的新 Sails.js 应用程序。在我使用sails generate model products
创建新模型之前,该应用程序运行良好。当应用程序尝试启动时,我现在遇到以下错误:
server_1 | [nodemon] starting `node app.js`
server_1 | info: ·• Auto-migrating... (alter)
server_1 | info: Hold tight, this could take a moment.
server_1 | Troubleshooting tips:
server_1 |
server_1 | -> Is your Postgresql configuration correct? Maybe your `poolSize` configuration is set too high? e.g. If your Postgresql database only supports 20 concurrent connections, you should make sure you have your `poolSize` set as something < 20 (see http://***.com/a/27387928/486547). The default `poolSize` is 10. To override default settings, specify the desired properties on the relevant Postgresql "connection" config object where the host/port/database/etc. are configured. If you're using Sails, this is generally located in `config/datastores.js`, or wherever your environment-specific database configuration is set.
server_1 |
server_1 | -> Maybe your `poolSize` configuration is set too high? e.g. If your Postgresql database only supports 20 concurrent connections, you should make sure you have your `poolSize` set as something < 20 (see http://***.com/a/27387928/486547). The default `poolSize` is 10.
server_1 |
server_1 | -> Do you have multiple Sails instances sharing the same Postgresql database? Each Sails instance may use up to the configured `poolSize` # of connections. Assuming all of the Sails instances are just copies of one another (a reasonable best practice) we can calculate the actual # of Postgresql connections used (C) by multiplying the configured `poolSize` (P) by the number of Sails instances (N). If the actual number of connections (C) exceeds the total # of **AVAILABLE** connections to your Postgresql database (V), then you have problems. If this applies to you, try reducing your `poolSize` configuration. A reasonable `poolSize` setting would be V/N.
为了响应这个错误,我在我的 datastores.js 文件中添加了一个 poolSize
键,如下所示:
module.exports.datastores =
default:
adapter: 'sails-postgresql',
url: 'postgresql://postgres@postgresql:5432/web-service',
poolSize: 1
,
;
它仍然会抛出相同的错误。应用程序和数据库都在 Docker 环境中运行。这就是我的 docker-compose.yml 的样子:
version: '3.4'
services:
server:
image: web-service
build: .
environment:
NODE_ENV: development
IS_DEV_MACHINE: "yes"
ports:
- 1338:1337 # HOST_PORT is 1338 to avoid conflicts with other Sails.js apps running on host
volumes:
- ../:/usr/src/app
entrypoint: nodemon
postgresql:
image: bitnami/postgresql:9.6
ports:
- 5433:5432 # HOST_PORT is 5433 to avoid conflicts with other PostgreSQL instances running on host
volumes:
- ../database:/bitnami
我似乎无法在任何地方找到解决方案。关于这个主题的其他 *** 线程已经一年多没有得到回答了。任何帮助表示赞赏。
【问题讨论】:
【参考方案1】:我设法通过从 bitnami:postgresql:9.6 Docker 映像切换到 postgres:9.6 来解决该错误。显然 postgres:9.6 接受 100 个连接(这似乎比 Bitnami 图像接受的要多)。即使我从 datastores.js 中删除 poolSize: 1
,Sails 也不再引发 poolSize 错误。这就是我的 docker-compose.yml 文件现在的样子
version: '3.4'
services:
server:
image: web-service
build: .
environment:
NODE_ENV: development
IS_DEV_MACHINE: "yes"
ports:
- 1338:1337 # HOST_PORT is 1338 to avoid conflicts with other Sails.js apps running on host
volumes:
- ../:/usr/src/app
entrypoint: nodemon
postgresql:
image: postgres:9.6
environment:
POSTGRES_USER: admin
POSTGRES_PASSWORD: admin
ports:
- 5433:5432 # HOST_PORT is 5433 to avoid conflicts with other PostgreSQL instances running on host
volumes:
- ../database:/var/lib/postgresql/data
【讨论】:
以上是关于连接到在 Docker 容器中运行的数据库时,sails-postgresql“poolSize”太高错误的主要内容,如果未能解决你的问题,请参考以下文章
如何将运行在 Docker 容器中的 Flyway 连接到运行在 Docker 容器中的数据库?
当 DBeaver 驱动程序设置中的“allowPublicKeyRetrieval=true”时,只能连接到在 docker 容器中运行的 mysql