MongoDB 的默认用户名和密码是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38921414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:47:49  来源:igfitidea点击:

MongoDB what are the default user and password?

mongodb

提问by Juan Pablo Fernandez

I am using the same connection string on local and production. When the connection string is mongodb://localhost/mydb

我在本地和生产中使用相同的连接字符串。当连接字符串是mongodb://localhost/mydb

What is the username and password? Is it secure to keep it this way?

用户名和密码是什么?保持这种方式安全吗?

回答by Camilo Silva

By default mongodb has no enabled access control, so there is no default user or password.

默认情况下 mongodb 没有启用访问控制,因此没有默认用户或密码。

To enable access control, use either the command line option --author security.authorization configuration file setting.

要启用访问控制,请使用命令行选项--auth或 security.authorization 配置文件设置。

You can use the following procedure or refer to Enabling Authin the MongoDB docs.

您可以使用以下过程或参考MongoDB 文档中的启用身份验证

Procedure

程序

  1. Start MongoDB without access control.

    mongod --port 27017 --dbpath /data/db1
    
  2. Connect to the instance.

    mongo --port 27017
    
  3. Create the user administrator.

    use admin
    db.createUser(
      {
        user: "myUserAdmin",
        pwd: "abc123",
        roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
      }
    )
    
  4. Re-start the MongoDB instance with access control.

    mongod --auth --port 27017 --dbpath /data/db1
    
  5. Authenticate as the user administrator.

    mongo --port 27017 -u "myUserAdmin" -p "abc123" \
      --authenticationDatabase "admin"
    
  1. 在没有访问控制的情况下启动 MongoDB。

    mongod --port 27017 --dbpath /data/db1
    
  2. 连接到实例。

    mongo --port 27017
    
  3. 创建用户管理员。

    use admin
    db.createUser(
      {
        user: "myUserAdmin",
        pwd: "abc123",
        roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
      }
    )
    
  4. 使用访问控制重新启动 MongoDB 实例。

    mongod --auth --port 27017 --dbpath /data/db1
    
  5. 以用户管理员身份进行身份验证。

    mongo --port 27017 -u "myUserAdmin" -p "abc123" \
      --authenticationDatabase "admin"
    

回答by Razvan Dumitru

In addition with what @Camilo Silva already mentioned, if you want to give free access to create databases, read, write databases, etc, but you don't want to create a root role, you can change the 3rd step with the following:

除了@Camilo Silva 已经提到的内容之外,如果您想提供创建数据库、读取、写入数据库等的免费访问权限,但又不想创建 root 角色,您可以使用以下内容更改第 3 步:

use admin
db.createUser(
  {
    user: "myUserAdmin",
    pwd: "abc123",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" }, 
             { role: "dbAdminAnyDatabase", db: "admin" }, 
             { role: "readWriteAnyDatabase", db: "admin" } ]
  }
)

回答by Black

For MongoDB earlier than 2.6, the command to add a root user is addUser(e.g.)

对于 2.6 之前的 MongoDB,添加 root 用户的命令是addUser(例如)

db.addUser({user:'admin',pwd:'<password>',roles:["root"]})

回答by Fjut

In addition to previously provided answers, one option is to follow the 'localhost exception' approach to create the first user if your db is already started with access control (--authswitch). In order to do that, you need to have localhost access to the server and then run:

除了先前提供的答案之外,如果您的数据库已经通过访问控制(--auth开关)启动,则一个选项是遵循“本地主机异常”方法来创建第一个用户。为此,您需要拥有对服务器的 localhost 访问权限,然后运行:

mongo
use admin
db.createUser(
 {
     user: "user_name",
     pwd: "user_pass",
     roles: [
           { role: "userAdminAnyDatabase", db: "admin" },
           { role: "readWriteAnyDatabase", db: "admin" },
           { role: "dbAdminAnyDatabase", db: "admin" }
        ]
 })

As stated in MongoDB documentation:

如 MongoDB 文档中所述:

The localhost exception allows you to enable access control and then create the first user in the system. With the localhost exception, after you enable access control, connect to the localhost interface and create the first user in the admin database. The first user must have privileges to create other users, such as a user with the userAdmin or userAdminAnyDatabase role. Connections using the localhost exception only have access to create the first user on the admin database.

localhost 例外允许您启用访问控制,然后在系统中创建第一个用户。除了 localhost 例外,在启用访问控制后,连接到 localhost 界面并在 admin 数据库中创建第一个用户。第一个用户必须有权创建其他用户,例如具有 userAdmin 或 userAdminAnyDatabase 角色的用户。使用 localhost 异常的连接只能访问在 admin 数据库上创建第一个用户。

Here is the link to that sectionof the docs.

这是文档该部分的链接。