mongo db

Please indicate the source of the reprint: http://www.mrco.cn/article/5735b04fc49f11e762327991.html I

have just encountered a problem when building this blog, and make a diary. I believe that students who have upgraded from Mongodb 2.X to Mongodb 3.X Later, I found that the way of setting users is different, and there is relatively little information on the Internet. Since I am new to Mongodb, I have also consulted a lot of articles and materials before I know how to solve it. Hope it will help other children's shoes who encounter this problem.
  In the version of Mongodb 2.X, there are many posts on the Internet. In the version 2.X, there is an admin table by default, and to create a user, you only need to add it through db.addUser(), and then use db.auth(). Just give permission.
  but. . Since I am using the version of Mongodb 3.X, it will not work to follow the 2.X post. I found that there is no admin table in the system when I use show dbs, what should I do? It turns out that admin is not displayed by default in the 3.X version. We can manually switch to admin through use admin, and then create users through the db.createUser() method. Finally figured out how to create users. But the problem came again. At first I thought that Mongdb would create a unified sa user like a relational database such as Sql Server, but it turned out that my idea was wrong. Mongodb users are associated with collections. And to set a user for the collection, it must be authorized by the super user of admin. Anyway, this series of authorization relationships tossed me for a long time. alright, do not piffle any more. Or directly paste the code steps directly.

Step 1: Create a system administrator called testUser for the admin library. Users of other libraries need to rely on this administrator to create   
01
> show dbs
02
admin 0.000GB
03
local  0.000GB
04
test   0.000GB
05
> use admin
06
switched to db admin
07
> db.createUser({ user:'testUser',pwd:'test123',roles:[{ role:'userAdminAnyDatabase',db:'admin' }] })
08
Successfully added user: {
09
    "user" : "testUser",
10
    "roles" : [
11
        {
12
            "role" : "userAdminAnyDatabase",
13
            "db" : "admin"
14
        }
15
    ]
16
}
17
> db.auth('testUser','test123')
) 14 1 Step 3: Then our user and role are created. Next we need to shut down the Mongodb service.





























1
>db.shutdownServer()
Step 4: Restart our Mongodb
1
./mongod --dbpath=../data --port=12121 --auth
Step 5: After restarting, we will be prompted to connect to Mongodb User name and password are required.
1
./mongo localhost:12121/test -u myTestUser -p 123456
This will log in. This user only has read and write permissions after logging in. Let's go back and look at the creation code, the role inside It is readWrite, this is the permission given.

Permission list of
roles 1. Database user roles: read, readWrite;
2. Database management roles: dbAdmin, dbOwner, userAdmin;
3. Cluster management roles: clusterAdmin, clusterManager, clusterMonitor, hostManager;
4. Backup and recovery roles: backup, restore;
5. All database roles: readAnyDatabase, readWriteAnyDatabase, userAdminAnyDatabase, dbAdminAnyDatabase
6. Super user role: root
// There are several roles here that indirectly or directly provide access to system super users (dbOwner, userAdmin, userAdminAnyDatabase)
7. Internal roles: __system

Please indicate the source of the reprint: http://www.mrco.cn/article/5735b04fc49f11e762327991.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326707468&siteId=291194637