简体   繁体   English

Docker 容器中的 mongodb 正在运行,但拒绝连接?

[英]The mongodb in Docker container is running ,but refuse to connect?

The error message is below错误信息如下

MongoDB shell version: 3.2.11
connecting to: test
2020-05-16T20:53:47.438+0000 W NETWORK  [thread1] Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2020-05-16T20:53:47.440+0000 E QUERY    [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:229:14
@(connect):1:6

By the way, is there any way to automatically seed the database in the Docker container?顺便问一下,有没有什么方法可以自动为 Docker 容器中的数据库播种? I have to manually seed the data base every time.我每次都必须手动为数据库播种。

Thank you, guys.感谢你们。

did you map the port of your localhost to MongoDB container?你 map 你的 localhost 到 MongoDB 容器的端口了吗? if not add -p 27017:27017 to your docker run command.如果不添加 -p 27017:27017 到您的 docker 运行命令。

If you are looking to seed the database upon initialization, you can use the following as stated on docker hub :如果您希望在初始化时为数据库播种,则可以使用docker 集线器上所述的以下内容:

When a container is started for the first time it will execute files with extensions.sh and.js that are found in /docker-entrypoint-initdb.d.当一个容器第一次启动时,它将执行 /docker-entrypoint-initdb.d 中带有 extensions.sh 和 .js 的文件。 Files will be executed in alphabetical order.文件将按字母顺序执行。 .js files will be executed by mongo using the database specified by the MONGO_INITDB_DATABASE variable, if it is present, or test otherwise. .js 文件将由 mongo 使用 MONGO_INITDB_DATABASE 变量指定的数据库(如果存在)执行,否则进行测试。 You may also switch databases within the.js script.您还可以在 .js 脚本中切换数据库。

You can do this by mounting a javascript or shell script file via -v "./path/to/file.js:/docker-entrypoint-initdb.d/file.js" or via the volumes: ["./path/to/file.js:/docker-entrypoint-initdb.d/file.js"] key of your mongodb service if you are using docker-compose .您可以通过-v "./path/to/file.js:/docker-entrypoint-initdb.d/file.js"或通过volumes: ["./path/to/file.js:/docker-entrypoint-initdb.d/file.js"]如果您使用的是docker-compose服务,则为您的 mongodb 服务的volumes: ["./path/to/file.js:/docker-entrypoint-initdb.d/file.js"]键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM