MongoDB what are the default user and password?

匿名 (未验证) 提交于 2019-12-03 02:44:02

问题:

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

What are the user and password ? Is it secure to keep it this way ?

回答1:

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

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

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

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" 


回答2:

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:

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


回答3:

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

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


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!