Getting an Error trying to add a user to mongodb 2.6.5

Ryan picture Ryan · Nov 3, 2014 · Viewed 8.1k times · Source

I have spent a majority of my day trying to add a user to mongodb 2.6.5. I installed & re-installed Mongo via macports.

Try #1 ( addUser(); ):

1. # switching to the admin user
   > use admin

2. # db.addUser("admin", "password");

When use the addUser(); I get this error:

WARNING: The 'addUser' shell helper is DEPRECATED. Please use 'createUser' instead 
2014-11-03T15:19:16.193-0500 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

Try #2 ( createUser(); ):

1. # switching to the admin user
   > use admin

2. # db.createUser("admin", "password");

When use the createUser(); I get this error:

2014-11-03T15:25:38.243-0500 Error: couldn't add user: no such cmd: 0 at src/mongo/shell/db.js:1004

I have spent a lot of time looking up other people's questions and answers. Does anyone know how to fix this?

Answer

Amit Rai Sharma picture Amit Rai Sharma · Nov 3, 2014

createUser expect a document and not a string literal. for example:

db.createUser( { "user" : "accountAdmin01",
                 "pwd": "cleartext password",
                 "customData" : { employeeId: 12345 },
                 "roles" : [ { role: "clusterAdmin", db: "admin" },
                             { role: "readAnyDatabase", db: "admin" },
                             "readWrite"
                             ] },
               { w: "majority" , wtimeout: 5000 } )

Or

db.createUser(
   {
     user: "accountUser",
     pwd: "password",
     roles: [ "dbAdmin", "userAdmin" ]
   }
)

Refer db.createUser() for more information

However before you add the user check if the auth schema exists by executing:

db.system.users.find()

If it does not return anything then you need create the schema by executing:

db.system.users.insert({
  "roles" : [ "userAdmin", "dbAdmin"],
  "userSource":"$external", 
  "user" : "dbadmin"
})

You should not get error while adding the user once the schema is generated.

Or if you have upgraded to MongoDb 2.6 version, then you need to upgrade the auth schema by executing:

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