Configure MongoDB

$ mongo
> use admin
> db.createUser( { user: "<Enter a username>", pwd: "<Enter a secure password>", roles: [ { role: "readWriteAnyDatabase", db: "admin" }, { role: "userAdminAnyDatabase", db: "admin" } ] } )
> use <Enter a app name>
> db.createUser( { user: "<Enter a app username>", pwd: "<Enter a secure password>", roles: [ { role: "readWrite", db: "<Enter a app name>" }, { role: "clusterMonitor", db: "admin" } ] } )
> quit()

Enable database authorization in the MongoDB configuration file /usr/local/etc/mongod.conf by add security: and enabling authorization:

security:
  authorization: enabled

Restart MongoDB and verify the administrative user created earlier can connect:

$ brew services restart mongodb
$ mongo -u your_username -p your_password --authenticationDatabase=admin
添加新评论