Add user permission method sharing to MongoDB

  • 2020-05-13 03:44:42
  • OfStack

Using Mongodb database, it is necessary to add user privileges for each database. After checking 1, the following code is found:


use test2
db.addUser( { user: "test",
              pwd: "admin",
              roles: [ "readWrite", "dbAdmin" ]
            } )

After execution, it was found that:


$ mongo 192.168.1.111/test2 -u test -p admin
MyMongo:PRIMARY>


Error: 18 { ok: 0.0, errmsg: "auth failed", code: 18 } at src/mongo/shell/db.js:228

Detected version discovery


$mongo --help
MongoDB shell version: 2.4.9

I used version 2.6 of Mongodb, but version 2.4.9 of Shell. I felt that there was a text problem, so I adopted the following scheme:

Delete the old version of Client


sudo apt-get remove mongodb-clients
sudo apt-get autoremove
sudo apt-get autoclean

Install the new Shell


sudo apt-get install mongodb-org-shell=2.6.1
$mongo --help
MongoDB shell version: 2.6.1

Add users with the new (Mongodb 2.6 code)


use test2
db.createUser(
   {
     user: "test",
     pwd: "admin",
     roles:
       [
         { role: "readWrite", db: "test2" },
       ]
   }
)


$mongo 192.168.1.111/test2 -u test -p admin
MyMongo:PRIMARY>

Login successful!!

That's all for this article, and I hope it will help you understand the Mongo database.

Please take a moment to share this article with your friends or leave a comment. We will sincerely appreciate your support!


Related articles: