- 0, Login Admin library, open a MONGO shell
MONGO--port 27017-u "AdminUser"-P "adminpass"--authenticationdatabase "admin"
- 1, first switch to a new database TestDB
Use TestDB
- 2. Create an authenticated user for the database
Db.createuser ({User: ' TestDB ', pwd: ' 111111 ', roles:[{"role": "ReadWrite", "db": "Dbenop_sandbox"}]})
Db.auth (' TestDB ', ' 111111 ')
- 4. Add a random piece of data
Db.test.insert ({"Test": "Test"})
- 5, finished, see what the database is it?
Show DBS
READ: Allows the user to read the specified database
ReadWrite: Allows the user to read and write to the specified database
DbAdmin: Allows the user to perform administrative functions in the specified database, such as index creation, deletion, viewing statistics, or accessing System.profile
Useradmin: Allows the user to write to the System.users collection to create, delete, and manage users in the specified database
Clusteradmin: Available only in the admin database, giving the user administrative privileges on all shards and replica set related functions.
Readanydatabase: Only available in the Admin database, giving users read access to all databases
Readwriteanydatabase: Only available in the Admin database, giving users read and write access to all databases
Useradminanydatabase: Only available in the Admin database, giving the user useradmin permissions for all databases
Dbadminanydatabase: Only available in the Admin database, giving the user dbadmin permissions for all databases.
Root: Available only in the admin database. Super account, Super privilege
How do I create a MONGO database and add an authenticated user to it?