mongodb 如何在docker容器中启动mongodb shell?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32944729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to start a mongodb shell in docker container?
提问by Madhavi Jouhari
To start the container, I am typing the following command:
要启动容器,我输入以下命令:
sudo docker run -i -t -p 28000:27017 mongo:latest /usr/bin/mongod --smallfiles
But I want to open the shell in this container to type the mongo commands. What command should I run to do the same?
但是我想在这个容器中打开 shell 来输入 mongo 命令。我应该运行什么命令来做同样的事情?
回答by vladzam
You can run the interactive mongo shell by running the following command:
您可以通过运行以下命令来运行交互式 mongo shell:
docker run -it -p 28000:27017 --name mongoContainer mongo:latest mongo
Otherwise, if your container is already running, you can use the exec
command:
否则,如果您的容器已经在运行,您可以使用以下exec
命令:
docker exec -it mongoContainer mongo
回答by Alek Kras
The thing that I struggled too but found a solution:
我也挣扎但找到了解决方案的事情:
docker pull mongo
docker run --name CONTAINERNAME --restart=always -d -p 8080:8080 mongo mongod --auth
sudo docker exec -i -t CONTAINERNAME bash
mongo
use admin
db.createUser({user:"user", pwd:"password", roles:[{role:"root", db:"admin"}]})
exit && exit
Now you have created a running Docker container with everything you need. Now if you want to connect from any client with an admin user, just run this
现在您已经创建了一个正在运行的 Docker 容器,其中包含您需要的一切。现在,如果您想从任何具有管理员用户的客户端连接,只需运行此
mongo -u "user" -p "password" HOSTIP --authenticationDatabase "admin"
回答by Madhavi Jouhari
This is an alternate way: Open a new terminal
这是另一种方式:打开一个新终端
mongo 127.0.0.1:28000
Your mongo shell starts in this terminal now.
你的 mongo shell 现在在这个终端中启动。