1. 程式人生 > >Enable Authentication on MongoDB

Enable Authentication on MongoDB

1、Connect to the server using the mongo shell

mongo mongodb://localhost:27017

  

2、Create the user administrator

Change to the admin database:

use admin

  

db.createUser(
  {
    user: "Admin",
    pwd: "Admin123",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)

  

執行成功顯示如下提示:

 

Then disconnect from the mongo shell (Ctrl+D)

3、Enable authentication in mongod configuration file

 編輯mongodb 配置檔案:

sudo vim /etc/mongod.conf

  

在 # security: 節點下輸入以下內容:

security:
  authorization: "enabled"

  

4、save the file and restart mongod

 :

sudo service mongod restart

  

5、 Connect and authenticate as the user administrator

mongo mongodb://localhost:27017

 

use admin

  

db.auth("Admin", "Admin123")

  

6、Finally, create additional users as needed

use BIMDB
db.createUser(
  {
    user: "myTester",
    pwd: "xyz123",
    roles: [ { role: "readWrite", db: "BIMDB" } ]
  }
)

 

7、use auth on appsetting.json

mongodb://myTester:[email protected]:27017/BIMDB