我按照这个博客设置了 MongoDB docker 容器:https : //medium.com/@kristaps.strals/docker-mongodb-net-core-a-good-time-e21f1acb4b7b
当我运行 .net core 应用程序时,保存一个 todo,我设法将其查询回来,似乎一切正常(创建了 todo 集合,创建了 db,保存了 todo)。但是,当我使用“docker exec -it mongodb bash”访问 mongo db 容器并尝试使用“show dbs”获取 dbs 时,它会检索空列表。此外,这个 docker compose 启动了 mongo express 容器,它也没有列出新的数据库和集合。你知道什么是问题吗?
干杯
jac*_*son 17
您可能启用了身份验证(用户名和密码)。这意味着您必须先在 shell 中进行身份验证,然后才能列出数据库。
这是如何完成的(为谷歌员工添加了额外的步骤):
$ docker ps // list images
IMAGE
my-mongodb-container
$ docker exec -it my-mongodb-container bash
// -it is a flag for 'interactive terminal', bash is the shell we'll use
root@example:/# mongo
// enter mongodb shell
> use admin
// switched to db admin
> db.auth("username", "password");
// 1
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
mydb 0.000GB
> use mydb
// switched to db mydb
Run Code Online (Sandbox Code Playgroud)
您现在可以在您的 mongodb 实例中运行 mongodb shell 命令 - 因此诸如“show collections”之类的东西将起作用。
希望对某人有所帮助!这些东西有时非常不透明:)
| 归档时间: |
|
| 查看次数: |
3315 次 |
| 最近记录: |