如何修改MongoDB3.0的資料庫認證機制

chenfeng發表於2017-09-27
修改需求:MongoDB3.0當前認證機制為SCRAM-SHA-1,需要改為老的認證機制MONGODB-CR

方法如下:
> use admin
switched to db admin

> db.system.version.findOne({"_id" : "authSchema"})
{ "_id" : "authSchema", "currentVersion" : 5 }
currentVersion為5則表示當前資料庫的認證機制是SCRAM-SHA-1,需要改成MONGODB-CR,currentVersion的值為3

>  var schema = db.system.version.findOne({"_id" : "authSchema"})
> schema.currentVersion = 3
3
> db.system.version.save(schema)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.system.version.find()
{ "_id" : "authSchema", "currentVersion" : 3 }
在這表示已經修改成功了。

建立資料庫使用者
> use test
> db.createUser({ user: 'test', pwd: 'test123', roles: [ { role: "readWrite", db: "test" } ] });
Successfully added user: {
    "user" : "test",
    "roles" : [
        {
            "role" : "userAdminAnyDatabase",
            "db" : "admin"
        }
    ]


檢視認證方式:
> use admin
> db.system.users.find()
{ "_id" : "admin.admin", "user" : "admin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "kAyNTjjA56SjKNB+voW/ow==", "storedKey" : "S0QMFvrojLTl4fYN4zz6HL3rlRc=", "serverKey" : "m0eym4YYQikIufcR8JxcIRfdDrg=" } }, "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] }
{ "_id" : "test.test", "user" : "test", "db" : "test", "credentials" : { "MONGODB-CR" : "cf75a14725e3655983a84ea5f5b25438" }, "roles" : [ { "role" : "readWrite", "db" : "test" } ] }

見credentials字樣,發現test使用者的認證方式為MONGODB-CR,至此,認證機制方式修改完畢

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

相關文章