-
-
Save foull/0834e34ea3d9174e2c4bfe36a44a14d0 to your computer and use it in GitHub Desktop.
var MongoClient = require('mongodb').MongoClient | |
// | |
// config | |
// | |
var mongoPort = '27017' | |
var mongoHost = 'localhost' | |
var dbName = 'dbName' | |
var userName = 'admin' | |
var userPassword = 'pass1' | |
// | |
// start | |
// | |
MongoClient.connect('mongodb://'+mongoHost+':'+mongoPort+'/'+dbName, | |
function(err, db) { | |
if (err){ | |
return console.log('Error: could not connect to mongodb') | |
} | |
// Use the admin database for the operation | |
var adminDb = db.admin() | |
// Add the new user to the admin database | |
adminDb.addUser(userName, userPassword, { | |
roles: [{ | |
role : "userAdmin", | |
db : dbName | |
}] | |
}, | |
function(err, result) { | |
if (err){ | |
return console.log('Error: could not add new user') | |
} | |
// Authenticate using the newly added user | |
adminDb.authenticate(userName, userPassword, function(err, result) { | |
if (err){ | |
return console.log('Error: could authenticate with created user') | |
} | |
console.log('Ok') | |
db.close() | |
}) | |
}) | |
}) |
Since the mongodb driver version 3 you cannot access the admin db directly anymore. You either have to connect using the API v3 or install the old driver.
I'm getting this error below at line:
adminDb.authenticate(userName, userPassword, function(err, result) {
TypeError: adminDb.authenticate is not a function
at eval (eval at (C:\Users\Deli\AiCAN\03_RD\GIC\LEARN_MONGODB_AND_JS\LEARN_02\kerberos\myproject\mongo-create-user.js:42:5), :1:13)
at C:\Users\Deli\AiCAN\03_RD\GIC\LEARN_MONGODB_AND_JS\LEARN_02\kerberos\myproject\mongo-create-user.js:42:5
at result (C:\Users\Deli\AiCAN\03_RD\GIC\LEARN_MONGODB_AND_JS\LEARN_02\kerberos\myproject\node_modules\mongodb\lib\operations\execute_operation.js:75:17)
at session.endSession (C:\Users\Deli\AiCAN\03_RD\GIC\LEARN_MONGODB_AND_JS\LEARN_02\kerberos\myproject\node_modules\mongodb\lib\operations\execute_operation.js:64:11)
at ClientSession.endSession (C:\Users\Deli\AiCAN\03_RD\GIC\LEARN_MONGODB_AND_JS\LEARN_02\kerberos\myproject\node_modules\mongodb\lib\core\sessions.js:135:41)
at executeCallback (C:\Users\Deli\AiCAN\03_RD\GIC\LEARN_MONGODB_AND_JS\LEARN_02\kerberos\myproject\node_modules\mongodb\lib\operations\execute_operation.js:59:17)
at handleCallback (C:\Users\Deli\AiCAN\03_RD\GIC\LEARN_MONGODB_AND_JS\LEARN_02\kerberos\myproject\node_modules\mongodb\lib\utils.js:129:55)
at execute (C:\Users\Deli\AiCAN\03_RD\GIC\LEARN_MONGODB_AND_JS\LEARN_02\kerberos\myproject\node_modules\mongodb\lib\operations\add_user.js:86:16)
at handleCallback (C:\Users\Deli\AiCAN\03_RD\GIC\LEARN_MONGODB_AND_JS\LEARN_02\kerberos\myproject\node_modules\mongodb\lib\utils.js:129:55)
at db.s.topology.command (C:\Users\Deli\AiCAN\03_RD\GIC\LEARN_MONGODB_AND_JS\LEARN_02\kerberos\myproject\node_modules\mongodb\lib\operations\command.js:115:7)
I am getting below error
node:24066) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
/home/laxman/CARTOS/main/node_modules/mongodb/lib/operations/mongo_client_ops.js:439
throw err;
^
TypeError: db.admin is not a function
at /home/laxman/CARTOS/main/deploy/singleClickTenant/mongo-create-user.js:26:26
at result (/home/laxman/CARTOS/main/node_modules/mongodb/lib/utils.js:414:17)
at executeCallback (/home/laxman/CARTOS/main/node_modules/mongodb/lib/utils.js:406:9)
at err (/home/laxman/CARTOS/main/node_modules/mongodb/lib/operations/mongo_client_ops.js:285:5)
at connectCallback (/home/laxman/CARTOS/main/node_modules/mongodb/lib/operations/mongo_client_ops.js:240:5)
at process.nextTick (/home/laxman/CARTOS/main/node_modules/mongodb/lib/operations/mongo_client_ops.js:436:7)
at _combinedTickCallback (internal/process/next_tick.js:132:7)
at process._tickCallback (internal/process/next_tick.js:181:9)