[英]Connection time out when mongoose attempts to connect to a mongodb instance running in docker
I am developing a Nodejs application locally in a Windows machine.我正在 Windows 机器上本地开发一个 Nodejs 应用程序。 For that I decided to run Mongodb inside a Docker container simply to learn how to use Docker.为此,我决定在 Docker 容器中运行 Mongodb,只是为了学习如何使用 Docker。 I am also running Mongo-express in another container in order to manage the database.我还在另一个容器中运行Mongo-express以管理数据库。
The problem is when I execute the file to connect to the database, a connection time out occours.问题是当我执行文件以连接到数据库时,发生连接超时。
This is the file with the Database code:这是带有数据库代码的文件:
let mongoose = require('mongoose');
let debug = require('debug')('s-optimizer:mongoDb')
mongoose.connect("mongodb://192.168.99.100:27017/local", {
user: "Lab",
pass: "123123as",
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => debug("Success"))
.catch((error) => debug(error))
let db = mongoose.connection
db.on('error', () => debug("Connection Error"));
db.once('open', () => debug("Connection established"))
And this is the error :这是错误:
s-optimizer:mongoDb MongooseTimeoutError: Server selection timed out after 30000 ms
s-optimizer:mongoDb at new MongooseTimeoutError (C:\Users\vmari\Projects\S-optimazer\node_modules\mongoose\lib\error\timeout.js:22:11)
s-optimizer:mongoDb at NativeConnection.Connection.openUri (C:\Users\vmari\Projects\S-optimazer\node_modules\mongoose\lib\connection.js:763:19)
s-optimizer:mongoDb at Mongoose.connect (C:\Users\vmari\Projects\S-optimazer\node_modules\mongoose\lib\index.js:332:15)
s-optimizer:mongoDb at Object.<anonymous> (C:\Users\vmari\Projects\S-optimazer\mongo\Database.js:5:10)
s-optimizer:mongoDb at Module._compile (internal/modules/cjs/loader.js:959:30)
s-optimizer:mongoDb at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
s-optimizer:mongoDb at Module.load (internal/modules/cjs/loader.js:815:32)
s-optimizer:mongoDb at Function.Module._load (internal/modules/cjs/loader.js:727:14)
s-optimizer:mongoDb at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)
s-optimizer:mongoDb at internal/main/run_main_module.js:17:11 +0ms
s-optimizer:mongoDb Connection Error +7ms
Information about my environment关于我的环境的信息
I am using docker-machine and virtual box to run Docker in Windows.我正在使用 docker-machine 和 virtual box 在 Windows 中运行 Docker。
This is the enviroment for docker-machine when I run: docker-machine env
.这是我运行时 docker-machine 的环境: docker-machine env
。
$Env:DOCKER_TLS_VERIFY = "1"
$Env:DOCKER_HOST = "tcp://192.168.99.100:2376"
$Env:DOCKER_CERT_PATH = "C:\Users\vmari\.docker\machine\machines\default"
$Env:DOCKER_MACHINE_NAME = "default"
$Env:COMPOSE_CONVERT_WINDOWS_PATHS = "true"
And the Docker containers running when I do: Docker ps
以及运行时运行的 Docker 容器: Docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1d9788a4c5da mongo-express "tini -- /docker-ent…" About an hour ago Up About an hour 0.0.0.0:8081->8081/tcp fervent_chebyshev
c7bab68f0725 mongo "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:27017->27017/tcp bold_noether
I must point out that I ran mongo-express using docker run -it --rm -d -p 8081:8081 --link bold_noether:mongo mongo-express
and that I can access it on the browser on: http://192.168.99.100:8081/
.我必须指出,我使用docker run -it --rm -d -p 8081:8081 --link bold_noether:mongo mongo-express
并且我可以在浏览器上访问它: http://192.168.99.100:8081/
。 But when I try to connect to the database container through my code I get the timeout.但是当我尝试通过我的代码连接到数据库容器时,我得到了超时。
Dude white list your IP then check伙计白名单您的IP然后检查
https://github.com/docker-library/mongo/issues/177 https://github.com/docker-library/mongo/issues/177
Check the thread it can help you检查可以帮助您的线程
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.