本文共 4298 字,大约阅读时间需要 14 分钟。
use admin> db.createUser({user:"root",pwd:"root",roles:[{role:"root",db:"admin"}]})Successfully added user: { "user" : "root", "roles" : [ { "role" : "root", "db" : "admin" } ]}> db.auth("root","root")1
认证用户创建成功后退出修改mongodb配置文件然后重启
在行尾加入vim mongod.confsecurity: authorization: enabled
mongo> show dbs> show tablesWarning: unable to run listCollections, attempting to approximate collection names by parsing connectionStatus> exit
输入验证用户
[root@localhost conf]# mongo -uroot -proot admin> show dbsadmin 0.000GBconfig 0.000GBlocal 0.000GB
> use testswitched to db test> db.createUser({user:"test-read",pwd:"123456",roles:[{role:"read",db:"test"}]})Successfully added user: { "user" : "test-read", "roles" : [ { "role" : "read", "db" : "test" } ]}
> db.system.users.find(){ "_id" : "admin.root", "userId" : UUID("d0781985-a9ff-454a-bc60-cd4ac45902f8"), "user" : "root", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "PuLTpRJAnK2MdVwBj5KAlw==", "storedKey" : "tejQhliRyhTn+XDwTSO8vfzZJJY=", "serverKey" : "yRw+MD/ryymGrgGbsDNmnt9yYoE=" }, "SCRAM-SHA-256" : { "iterationCount" : 15000, "salt" : "/EeWxFocy/cxdnrD+XZ0Tx+z/FRnZv8ybpJdUg==", "storedKey" : "WjMLjd7zHwIKbyEiM/iaBsOWv75jRPUoN3m/tOONLFo=", "serverKey" : "UcYveQMY/k4TXhr10xBy+voU21kt4Mu3G52BrHDTdsE=" } }, "roles" : [ { "role" : "root", "db" : "admin" } ] }{ "_id" : "test.test-read", "userId" : UUID("6fad7d9d-9987-43a0-8fe5-63937251d9f7"), "user" : "test-read", "db" : "test", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "iiJEuG/mHAnhTjIpGY9jKw==", "storedKey" : "1KVPCa1Qv9rsWE0NHj5GFAxoz3E=", "serverKey" : "yiOmZzS5Y/y/mSyBI2dbjxgVjnI=" }, "SCRAM-SHA-256" : { "iterationCount" : 15000, "salt" : "BiE7N07G0pBdCkThO65OpZTiOoHkLn/tD1aDZg==", "storedKey" : "n+H+kStdlin08wFrqQn2Ab/iqDvYNzMIs2bRnkrVkO8=", "serverKey" : "oEftyIyv8RobzLlHtOPilWusXQdhu1hbnrZgZ2xYh/U=" } }, "roles" : [ { "role" : "read", "db" : "test" } ] }
mongo -u"test-read" -p123456 test > db.test.insert({name:"mongo"})WriteCommandError({ "ok" : 0, "errmsg" : "not authorized on test to execute command { insert: \"test\", ordered: true, lsid: { id: UUID(\"8c9e94c8-d9bf-4378-b9eb-2f811162c374\") }, $db: \"test\" }", "code" : 13, "codeName" : "Unauthorized"})
> use testswitched to db test> db.createUser({user:"test-rw",pwd:"123456",roles:[{role:"readWrite",db:"test"}]})Successfully added user: { "user" : "test-rw", "roles" : [ { "role" : "readWrite", "db" : "test" } ]}
> db.test.insert({name:"mongo"})WriteResult({ "nInserted" : 1 })> show tablestest> db.test.find(){ "_id" : ObjectId("5f0bd0c96409669331de55cd"), "name" : "mongo" }
> show tablestest> db.test.find(){ "_id" : ObjectId("5f0bd0c96409669331de55cd"), "name" : "mongo" }
> use testswitched to db test> db.dropUser("test-read")true> use adminswitched to db admin> db.system.users.find(){ "_id" : "admin.root", "userId" : UUID("d0781985-a9ff-454a-bc60-cd4ac45902f8"), "user" : "root", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "PuLTpRJAnK2MdVwBj5KAlw==", "storedKey" : "tejQhliRyhTn+XDwTSO8vfzZJJY=", "serverKey" : "yRw+MD/ryymGrgGbsDNmnt9yYoE=" }, "SCRAM-SHA-256" : { "iterationCount" : 15000, "salt" : "/EeWxFocy/cxdnrD+XZ0Tx+z/FRnZv8ybpJdUg==", "storedKey" : "WjMLjd7zHwIKbyEiM/iaBsOWv75jRPUoN3m/tOONLFo=", "serverKey" : "UcYveQMY/k4TXhr10xBy+voU21kt4Mu3G52BrHDTdsE=" } }, "roles" : [ { "role" : "root", "db" : "admin" } ] }{ "_id" : "test.test-rw", "userId" : UUID("87ced292-7002-4aa8-97f4-51fe2b2092eb"), "user" : "test-rw", "db" : "test", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "fm8yHKrzyzjZYu9z+zoKSA==", "storedKey" : "IGTHdZqX323gaeSubUnArB1CCfY=", "serverKey" : "3CfFP2T4LSxkuoQ43aJQbQiPQwA=" }, "SCRAM-SHA-256" : { "iterationCount" : 15000, "salt" : "sBhYrXGUIhP5M9CtTAiDrsR1Ph+B1RxIlC7xVw==", "storedKey" : "Ml88HbnDzTtt9swZZamPAL9TIWOeKIj7gdoLrTHhh54=", "serverKey" : "AFxnf9A3rQlmr8O5y3sTGID8aMAiHpteOOuFLgVNpP0=" } }, "roles" : [ { "role" : "readWrite", "db" : "test" } ] }
转载地址:http://wgpb.baihongyu.com/