MongoDB permission to open authentication method Detailed steps

After you install MongoDB, turn off auth authentication, start the server, create an account, the account needs to have grant permission, namely: account management of delegated authority. Note that the account is followed by the library to go, so in the designated library authorization, you must also specify the verification library (auth).
Add an administrative user:
use admin 
db.createUser( {user: "admin",pwd: "123456",roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]})
user: username
pwd: password
roles: the role of the specified user, can be an empty array to the new user role empty set; roles in the field, can specify the built-in and user-defined roles roles. role in the role can be selected:
Specific roles:
Read: allows the user to specify the database to read
readWrite: allows the user to specify the database to read and write
dbAdmin: allows users to perform the specified database management functions, such as indexing to create, delete, view statistics or access system.profile
userAdmin: allowing a user to write to system.users, you can find specify the database to create, delete, and manage users
clusterAdmin: admin only available in the database, the user gives all privileges fragmentation and replication sets associated function.
readAnyDatabase: admin is only available in the database, giving users read access to all databases
readWriteAnyDatabase: only available in the database admin, read and write access gives the user all databases
userAdminAnyDatabase: admin is only available in the database, the user gives permission to all databases userAdmin
dbAdminAnyDatabase: admin is only available in the database, the user gives permission dbAdmin all databases.
root: admin is only available in the database. Super account, super powers.
Just created userAdminAnyDatabase role for managing users, can be created by this role, delete users.

Open --auth parameters in order to access the database after authentication
db.createUser(
  {
    user: "db",
    pwd: "123456",
    roles: [ { role: "readWrite", db: "test" } ]
  }
}
Create our own user database, this time there is no verification, leading to no authority.
db.auth ( "db", "123456") may be operated after the authentication database.
./mongo -u db -p 123456 127.0.0.1:27017/test
C # example:
using MongoDB.Bson;
using MongoDB.Driver;
// ...
var client = new MongoClient(
    "mongodb+srv://<username>:<password>@<cluster-address>/test?w=majority"
);
var database = client.GetDatabase("test");

  

Guess you like

Origin www.cnblogs.com/ruralcraftsman/p/11895700.html