简体   繁体   English

如何使用 nodejs 在 docker-compose 上的 mongoDB 上设置身份验证?

[英]How to setup authentication on mongoDB on docker-compose with nodejs?

I want to setup basic authentication while connecting to the mongoDB database using my nodejs application.我想在使用我的 nodejs 应用程序连接到 mongoDB 数据库时设置基本身份验证。

Looked at the documentation for mongo image by specifying通过指定查看 mongo 图像的文档

environment:
    - MONGO_INITDB_ROOT_USERNAME=admin
    - MONGO_INITDB_ROOT_PASSWORD=pass

But this does not seem to work, when I use the connection string in nodejs (mongoose) as但这似乎不起作用,当我在 nodejs (mongoose) 中使用连接字符串时

mongodb://admin:pass@mongo:27017/myDatabase

I would just like to setup basic authentication to prevent any ransomware attacks like this :我只想设置基本身份验证以防止任何这样的勒索软件攻击:

Stackoverflow MongoDB Ransomware Stackoverflow MongoDB 勒索软件

MONGO_INITDB_ROOT_USERNAME and MONGO_INITDB_ROOT_PASSWORD are used to setup superuser account to manage overall databases. MONGO_INITDB_ROOT_USERNAMEMONGO_INITDB_ROOT_PASSWORD用于设置超级用户帐户来管理整个数据库。
You can connect as admin permission using this connection string mongodb://<MONGO_INITDB_ROOT_USERNAME>:<MONGO_INITDB_ROOT_PASSWORD>@mongo:27017 and create a database myDatabase , after that you also have to create user with a role to allow query database.您可以使用此连接字符串以管理员权限连接mongodb://<MONGO_INITDB_ROOT_USERNAME>:<MONGO_INITDB_ROOT_PASSWORD>@mongo:27017并创建数据库myDatabase ,之后您还必须创建具有角色的用户以允许查询数据库。

For example例如

> use myDatabase
> db.createUser({user: "user", pwd: "user", roles:["dbOwner"]})

Then you can connect with this string然后你可以用这个字符串连接

mongodb://user:user@mongo:27017/myDatabase

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

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