Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"db.createUser is not a function" and "password can't be empty"

Tags:

mongodb

I am trying to setup the authentication on MongoDB. I've uncommented auth=true in configuration file and restarted the server. The first connection via mongo was rejected. The second one (immediately after the first one) went through and I got this in the logs:

Sat Apr 12 14:56:46 [initandlisten] connection accepted from 127.0.0.1:44001 #1 Sat Apr 12 14:56:46 [conn1] note: no users configured in admin.system.users, allowing localhost access 

While in console, I am able to execute commands, however if I try to follow any of the numberous tutorials (including the official ones), I get either db.createUser is not a function when executing db.createUser() or password can't be empty when executing db.addUser():

Mongo output

The object which I am passing is:

{     user: "username",     pwd: "passphrase",     roles: [          {              role: "userAdminAnyDatabase",              db: "admin"          }     ] } 
like image 615
Xeos Avatar asked Apr 12 '14 19:04

Xeos


2 Answers

What version of mongodb are you running? db.createUser was introduced in version 2.6

Deprecated since version 2.6: Use db.createUser() and db.updateUser() instead of db.addUser() to add users to MongoDB.

In 2.6, MongoDB introduced a new model for user credentials and privileges, as described in Security Introduction. To use db.addUser() on MongoDB 2.4, see db.addUser() in the version 2.4 of the MongoDB Manual.

http://docs.mongodb.org/manual/reference/method/db.addUser/

like image 145
Dave Albert Avatar answered Oct 02 '22 18:10

Dave Albert


Regarding my experience, this issue also happens when you have an old version of Mongo Shell.

So, be sure to have the Mongo Shell according to the version of your MongoDB Server.

$ mongo

MongoDB shell version v3.4.10

connecting to: mongodb://127.0.0.1:27017

MongoDB server version: 3.4.10

Below:


$ mongo MongoDB shell version v3.4.10 connecting to: mongodb://127.0.0.1:27017 MongoDB server version: 3.4.10 Server has startup warnings:  2018-01-16T11:43:00.195-0600 I STORAGE  [initandlisten]  2018-01-16T11:43:00.195-0600 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine 2018-01-16T11:43:00.195-0600 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem 2018-01-16T11:43:00.687-0600 I CONTROL  [initandlisten]  2018-01-16T11:43:00.687-0600 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database. 2018-01-16T11:43:00.687-0600 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted. 2018-01-16T11:43:00.687-0600 I CONTROL  [initandlisten]  2018-01-16T11:43:00.687-0600 I CONTROL  [initandlisten]  2018-01-16T11:43:00.687-0600 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. 2018-01-16T11:43:00.687-0600 I CONTROL  [initandlisten] **        We suggest setting it to 'never' 2018-01-16T11:43:00.687-0600 I CONTROL  [initandlisten]  > > use admin switched to db admin >   > db.createUser( {user:"mongoloide", pwd:"mongoloide*2017", roles:["root"]} ) Successfully added user: { "user" : "mongoloide", "roles" : [ "root" ] } 
like image 29
ivanleoncz Avatar answered Oct 02 '22 18:10

ivanleoncz