5

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?

ouflak
  • 2,458
  • 10
  • 44
  • 49
Ryan
  • 71
  • 2
  • 3

1 Answers1

11

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 })
Amit Rai Sharma
  • 4,015
  • 1
  • 28
  • 35
  • Thanks for responding so quickly. I'm new to mongo and maybe you might see something I am doing wrong. I have posted a gist of my terminal window : https://gist.github.com/rvirvo/622891629c946535b23f – Ryan Nov 03 '14 at 22:00
  • What roles do you want to assign to this user? – Amit Rai Sharma Nov 03 '14 at 22:03
  • I would like to assign the role of admin, or userAdmin. – Ryan Nov 03 '14 at 22:10
  • 1
    Thanks ARS! I ended up getting it. I had to update the schema db.getSiblingDB("admin").runCommand({authSchemaUpgrade: 1 }); then I could create the user – Ryan Nov 03 '14 at 22:38