[英]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.