阿里雲 RDS MongoDB4.0新建庫,表,使用者和密碼步驟

chenfeng發表於2022-01-18

例子:

例項id:dds-2xxxxxxxx

超級管理員使用者名稱和密碼

root

xxxxxxxxxxxxxxx



連線:

# mongo -host dds-2xxxxxxxx.mongodb.rds.aliyuncs.com --port 3717  -uroot -p xxxxxxxxxxxxxxx  --authenticationDatabase admin

#切換庫

>use test_ee

# 建立表

>db.createCollection("test_1");

>db.createCollection("test_2");

>db.createCollection("test_3");

>db.createCollection("test_4");

#建立索引

>db.getCollection("test_1").createIndex("testID", 1);

>db.getCollection("test_1").createIndex({"testID":1,"userID":1});

>db.getCollection("test_2").createIndex({"testID", 1});

>db.getCollection("test_2").createIndex({"testID":1,"userID":1});

>db.getCollection("test_3").createIndex({"testID", 1});

>db.getCollection("test_3").createIndex({"testID":1,"userID":1});

>db.getCollection("test_4").createIndex({"testID", 1});

>db.getCollection("test_4").createIndex({"testID":1,"testID":1});

>db.getCollection("test_4").createIndex({"testID":1});

#建立使用者:

>use test_ee

>db.createUser(

{

user: "test_rw",

pwd: "xxxxxxxx",

roles: [

{ role: "readWrite", db: "test_ee" },

]

}

);

>use admin

>db.createUser(

{

user: "test_rw",

pwd: "xxxxxxxx",

roles: [

{ role: "readWrite", db: "test_ee" },

]

}

);

賦許可權:

>use admin

>db.grantRolesToUser( "test_rw", [ { role: "readWrite", db: "test_ee" } ])

連線測試:

# mongo dds-2xxxxxxxx.mongodb.rds.aliyuncs.com:3717/test_ee  -u test_rw -p xxxxxxxx

建立dbAdmin賬號許可權步驟:

>use test_ee

>db.createUser(

    {

      user: "test_rw_admin",

      pwd: "xxxxxxxxxxxx",

      roles: [

        { role: "readWrite", db: "test_ee" }

,    { role: "dbAdmin", db: "test_rw" } 

      ]

    }

)

>use admin

>db.createUser(

    {

      user: "test_rw_admin",

      pwd: "xxxxxxxxxxxx",

      roles: [

        { role: "readWrite", db: "test_ee" }

,    { role: "dbAdmin", db: "test_rw" } 

      ]

    }

)

>db.grantRolesToUser( "test_rw_admin", [ { role: "readWrite", db: "test_ee" } ])

>db.grantRolesToUser( "test_rw_admin", [ { role: "dbAdmin", db: "test_ee" } ])


升級test_rw賬號許可權為dbAdmin:

>db.updateUser( "test_rw",

  { roles : [

    { role : "dbAdmin", db : "test_ee"  }

  ] } )


>use admin

>db.grantRolesToUser( "test_rw", [ { role: "dbAdmin", db: "test_ee" } ])


回收許可權例子:

>db.revokeRolesFromUser( "test_rw_admin", [ { role: "dbAdmin", db: "test_ee" } ])


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/15498/viewspace-2852880/,如需轉載,請註明出處,否則將追究法律責任。

相關文章