First, the importance of MongoDB security check
The database in each MongoDB instance can have many users. If the security check is not enabled and the user is restricted, each user who enters the database can read, write, or even write to the database data. Such scenes in the actual application is very dangerous, easy to lead to data loss, accidental deletion and other accidents, so the database service to open security check, it is necessary.
Second,MongoDB How to turn on security check
Once MongoDB has turned on security, it must be a database authentication user to be able to read, write, or read data. Be sure to have at least one administrator account before conducting a security check. Because only administrators can read and write to any database, execute specific administrative commands, such as adding specific users to a database, restricting read and write permissions, and so on.
For example, the admin database has been added Super Administrator root, password is 1234, convenient after the database to open security checks, using the user login after the database can be managed, add the user's syntax for
Db.adduser ("username", "password", whether read-only true|false)
Then two users were added to the Kaiye database, with the syntax
Db.adduser ("haha", "123"): Haha user has read and write access to the Kaiye database
Db.adduser ("hehe", "123", true): hehe user has only read access to the Kaiye database
After restarting the server and adding--auth after the Open Server command, the MongoDB security check is initiated.
Mongod--dbpath=c:\\mymongodb--port 27017 --auth
After the security check is turned on at the server, the haha and hehe users are logged in, and the data reads and writes, and the exception is reported without permission at show collections.
At this time need to use the database user authentication login, Syntax: Db.auth ("User name", "password"), login just added hehe user (added read-only permission), found that the data can only be retrieved query, but not the data insert operation, This is because the third value of the AddUser () function was set to True when the hehe user was added to the Kaiye database just now, that is, the user has read-only permissions, so the write operation cannot be performed.
Switching users, that is, haha users who have read and write access at the same time, will find that both the find operation and the insert operation are possible.
MongoDB Security Check