Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add authentication to mongodb 2.6?

Tags:

I have existing a test server running mongodb 2.6 on ubuntu. Same version is running on my macbook. In both machines, I upgraded from mongodb 2.4 using standard upgrade mechanisms. Now I need to set up username and password based authentication

On my mac, I updated mongodb to 2.6 using brew update. I

I tried the following command on my Mac

$mongo
>db.getSiblingDatabase('admin')
>db.createUser({user:"root", pwd:"mycomplexpassword", roles:[ "userAdminAnyDatabase", "readWrite" ] } )

I got the following error

Error: couldn't add user: User and role management commands require auth data to have schema version 3 but found 1 at src/mongo/shell/db.js:1004

Hence I am unable enable auth to mongodb. How to solve the issue? Googling for the error does not seem to return anything useful right now.

PS: Should I expect the same issue to appear when I add authentication to mongodb on my servers?

like image 981
Ranjith Ramachandra Avatar asked May 05 '14 08:05

Ranjith Ramachandra


People also ask

Does MongoDB have authentication?

MongoDB supports x. 509 certificate authentication for client authentication and internal authentication of the members of replica sets and sharded clusters.

How do I enable Kerberos authentication in MongoDB?

Add Kerberos Principal(s) to MongoDB.Add a Kerberos principal, <username>@<KERBEROS REALM> , to MongoDB in the $external database. Specify the Kerberos realm in ALL UPPERCASE. The $external database allows exe to consult an external source (e.g. Kerberos) to authenticate.

How do I give access to MongoDB?

MongoDB does not enable access control by default. You can enable authorization using the --auth or the security. authorization setting. Enabling internal authentication also enables client authorization.


1 Answers

The schema of db.system.users in mongodb 2.4 and 2.6 are different which caused you fail to create new user.

2.4 schema

db.system.users.find()
{
    "_id" : ObjectId("53675bc48ff842a0657e25ff"),
    "user" : "root",
    "pwd" : "c2ff9601c8590812f0d40b9f60869679",
    "roles" : [
        "userAdminAnyDatabase",
        "readWrite"
    ]
}

2.6 schema

db.system.users.find()
{
    "_id" : "admin.root",
    "user" : "root",
    "db" : "admin",
    "credentials" : {
        "MONGODB-CR" : "c2ff9601c8590812f0d40b9f60869679"
    },
    "roles" : [
        {
            "role" : "userAdminAnyDatabase",
            "db" : "admin"
        },
        {
            "role" : "readWrite",
            "db" : "admin"
        }
    ]
}

I did not find an official way to convert them. So, you may need to convert 2.4 data in db.system.users to 2.6 format manually, or drop all old users in 2.4 and recreate them in 2.6.

You can use the following command to upgrade.

db.getSiblingDB("admin").runCommand({authSchemaUpgrade: 1 });

References:

  1. Upgrade User Authorization Data to 2.6 Format
  2. Sobolev Eugene's answer
like image 191
study Avatar answered Oct 01 '22 18:10

study