如何使用用户名和密码保护MongoDB [英] How to secure MongoDB with username and password

查看:117
本文介绍了如何使用用户名和密码保护MongoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要设置用户名&我的MongoDB实例的密码验证,以便任何远程访问都将要求输入用户名&密码.我从MongoDB网站尝试了该教程,并做了以下操作:

I want to set up user name & password authentication for my MongoDB instance, so that any remote access will ask for the user name & password. I tried the tutorial from the MongoDB site and did following:

use admin
db.addUser('theadmin', '12345');
db.auth('theadmin','12345');

此后,我退出并再次运行mongo.而且我不需要密码即可访问它.即使我远程连接到数据库,也不会提示我输入用户名&.密码.

After that, I exited and ran mongo again. And I don't need password to access it. Even if I connect to the database remotely, I am not prompted for user name & password.

更新这是我最终使用的解决方案

UPDATE Here is the solution I ended up using

1) At the mongo command line, set the administrator:

    use admin;
    db.addUser('admin','123456');

2) Shutdown the server and exit

    db.shutdownServer();
    exit

3) Restart mongod with --auth

  $ sudo ./mongodb/bin/mongod --auth --dbpath /mnt/db/

4) Run mongo again in 2 ways:

   i) run mongo first then login:

        $ ./mongodb/bin/mongo localhost:27017
        use admin
        db.auth('admin','123456');

  ii) run & login to mongo in command line.

        $ ./mongodb/bin/mongo localhost:27017/admin -u admin -p 123456

用户名& mongodumpmongoexport的密码将以相同的方式工作.

The username & password will work the same way for mongodump and mongoexport.

推荐答案

设置用户后,需要使用--auth选项启动mongod.

You need to start mongod with the --auth option after setting up the user.

从MongoDB站点:

From the MongoDB Site:

使用--auth选项运行数据库(mongod进程)以启用 安全.您必须先将用户添加到admin数据库中 使用--auth启动服务器,或从 本地主机接口.

Run the database (mongod process) with the --auth option to enable security. You must either have added a user to the admin db before starting the server with --auth, or add the first user from the localhost interface.

MongoDB身份验证

这篇关于如何使用用户名和密码保护MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆