MongoDB set user password on Windows

By default, mongod is listening on 127.0.0.1 of, any client can be connected directly to 27017, and there is no certification.

Benefit is that users can immediately get started, do not worry about a bunch of configuration to get upset.

The downside is that the public network server set up MongoDB, so everyone can directly access and modify the database data.

By default, mongod is no administrator account. So unless you use db.createUser in admin database () command to add an administrator account, and start mongod use -auth parameters , otherwise anyone can execute all commands without authentication in the database.

 

First, create all the database administrator user:

1, an administrator run cmd.exe, first cd to Mongodb installation directory bin directory,

Enter the command mongo.exe, enter the command interface mongodb:

 

2, create a database test1

Insert a piece of data, and then use the command: show dbs to see

I can see test1.

 

3, enter admin database:

Command: use admin

 

4, create an administrative account:

First look mongodb built-in roles:

    1. Database user roles: Read, readWrite;
    2. database administration roles: dbadmin, dbowner, useradmin;
    3. Cluster management role: clusterAdmin, ClusterManager, clusterMonitor, hostManager;
    4. Recovery backup role: Backup, Restore;
    5. The all databases role: readAnyDatabase, readWriteAnyDatabase, userAdminAnyDatabase, dbAdminAnyDatabase
    6. The root role: the root  
    7. The internal roles: __ system

使用命令:db.createUser({user:"testAdmin",pwd:"123456",roles:[{role:"userAdminAnyDatabase",db:"admin"}]})

 

5, restart mongodb service,

Reopen cmd, in the bin directory mongodb path, --dbpath d execute the mongod: \ mongodb \ the Data - auth    (the Data is installed mongodb create database folder)

1 to start the way --auth, the way identity permission to start the service, you must first set up an account and password; and access to the service must be verified, the account is what authority can only use what authority;

2, may not be added --auth, use authority is not started directly, and so can be used for direct testing;

 

6, add the user to verify successful:

The result is 1, indicating success.

 

7, using Robomongo tool to connect:

Administrators can see the user testAdmin (role userAdminAnyDatabase) just created, have access to all databases.

 

Second, a single database provided the following user password

 Ibid create test1 way to create a database test2

Run command: db.createUser ({user: 'test2admin', pwd: '123456', roles: [{role: 'readWrite', db: 'test2'}]})

Use Robomongo tool to connect:

Users can only see test2admin database test2.

 
 
Tags:  MongoDB

Guess you like

Origin www.cnblogs.com/tkzc2013/p/11203353.html