Ranjith Ramachandra
Ranjith Ramachandra

Reputation: 10774

How to add authentication to mongodb 2.6?

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?

Upvotes: 20

Views: 18049

Answers (4)

grepit
grepit

Reputation: 22402

I think the above answers are correct but what made it work for me were the followings steps (F.Y.I, I am on version 2.6.8):

1.Run in mongo:

db.system.version.update({}, {$set: {currentVersion: 3}});

2.Run in mongo:

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

3.Run in mongo:

db.createUser(
  {
    user: “your_user_name”,
    pwd: “your_password”,
    roles: [ { role: "userAdminAnyDatabase", db:"admin" } ]
  }
)
  1. Add the following to /usr/local/etc/mongod.conf

    auth=true

  2. restart mongo:

    mongod --dbpath /data/db

  3. Go to shell on Mac and login (also notice the single quotations around user name and password):

    mongo admin -u 'your_use_name' -p 'your_password'

Upvotes: 2

LHL
LHL

Reputation: 132

My mongoDb version 2.6.5 Issue below command

db.system.version.update({}, {$set: {currentVersion: 3}});

Upvotes: 0

study
study

Reputation: 5827

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

Upvotes: 30

Sobolev Eugene
Sobolev Eugene

Reputation: 358

Try this command in mongo console

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

Upgrade authorization schema.

Upvotes: 24

Related Questions