简体   繁体   English

如何将用户添加到在Docker容器中运行的mongoDB数据库中?

[英]How to add a user to a mongoDB database running in a docker container?

I want to add a custom user to a MongoDB database running on docker. 我想向在docker上运行的MongoDB数据库添加自定义用户。

I started my image using the following command: 我使用以下命令启动了映像:

docker run -p 27017-27019:27017-27019 
--name mongo 
-e MONGO_INITDB_ROOT_USERNAME=mongoadmin 
-e MONGO_INITDB_ROOT_PASSWORD=secret 
-e MONGO_INITDB_DATABASE=interviewTest 
-d mongo

and after all, I am trying to run the bash command to ,,talk'' with my container like: 毕竟,我正在尝试运行bash命令以与容器进行``交谈'',例如:

docker exec -it mongo bash

after all I am using command in terminal: 毕竟我在终端中使用命令:

mongo

and I am receiving information in log: 我正在日志中接收信息:

MongoDB shell version v4.2.0
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("57a90f02-6ba4-4ef3-8bba-8a5f1b424574") }
MongoDB server version: 4.2.0

Seemingly everything is ok, I can create database with the command: 似乎一切正常,我可以使用以下命令创建数据库:

use test

but when I am trying to list database with the command: 但是当我尝试使用命令列出数据库时:

show dbs

I am not receiving any result in the command line. 我没有在命令行中收到任何结果。 The command is accepted and I am receiving empty line with a blinking cursor. 该命令被接受,并且我正在接收带有闪烁光标的空行。

Moreover, when I am trying to create a database on my own like test with command 此外,当我尝试自己创建数据库时,例如使用命令test

use test

seemingly it is created without any problem but after all when I want to try to create a user with a script: 看起来它创建起来没有任何问题,但是毕竟当我想尝试使用脚本创建用户时:

db.createUser(
{
    user: "myUser",
    pwd: "myPassword",
    roles: [ { role: "readWrite", db: "interviewTest" }, "readWriteAnyDatabase" ]
}
);

I am receiving an error: 我收到一个错误:

QUERY [js] uncaught exception: Error: couldn't add user: command createUser requires authentication :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.createUser@src/mongo/shell/db.js:1370:11

Seemingly with above command docker run I should have a database and list it without any problem. 似乎上述命令docker run我应该有一个数据库并列出它,没有任何问题。 I have a Mongo Explorer and after use test, there is no new database created. 我有一个Mongo Explorer,使用后经过测试,没有创建新数据库。

I will be grateful for a solution which will give a possibility to create a user in my database and list DBs. 我将很高兴提供一个解决方案,该解决方案可以在我的数据库中创建用户并列出数据库。

In fact you currently create the admin user (and implicitely activate authentication), but never use it to connect to your db. 实际上,您当前正在创建管理员用户(并隐式激活身份验证),但从不使用它来连接数据库。

Providing username and password while connecting will grant you access as admin, then you will be able to create other users : 在连接时提供用户名和密码将授予您以管理员身份的访问权限,然后您便可以创建其他用户:

mongo --username mongoadmin --password --host localhost:27017 --authenticationDatabase admin

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

相关问题 如何配置在 docker 容器中运行的 Web 应用程序以成功连接到在不同容器中运行的 MongoDB 数据库 - How to configure a web application running in a docker container to succesfully connect to a MongoDB database running in a different container Docker mongodb-将磁盘上的数据库添加到容器 - Docker mongodb - add database on disk to container 如何连接到Docker容器中运行的MongoDB? - How to connect to MongoDB running in Docker container? 如何将MongoDB添加到Docker容器中? - How do I add MongoDB to a Docker container? 启动时添加Root密码并在MongoDB Docker容器上创建Application User - Add Root password and create Application User on MongoDB Docker container on startup 如何从 docker 容器连接到在本地计算机上运行的 MongoDB? - How to connect to MongoDB running in local machine from a docker container? 从主机连接到运行MongoDB的Docker容器 - Connect from host to Docker container running MongoDB 无法连接到在 docker 容器中运行的 mongoDB - Unable to connect to mongoDB running in docker container 在 docker 容器中运行的 MongoDB 在 windows 上未正确退出 - MongoDB running in docker container is not exiting correctly on windows Docker 容器中的 mongodb 正在运行,但拒绝连接? - The mongodb in Docker container is running ,but refuse to connect?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM