How to create a previously non-existing mongo database and new user from the nodejs mongodb driver using the admin account.
- Connect as admin user to new database, using
authSource: admin
(assumingadminDbName === admin
) - Create new user
const { MongoClient } = require('mongodb') | |
/* | |
Create a new database and a user for that database, when mongo is protected | |
by authentication. After calling this method, you will be able to connect | |
to the new database with the new user. | |
mongoHost: The mongo host to connect to | |
dbName: The name of the new database to create | |
username: The name of the new user to create | |
password: The password for the new user | |
adminUser: The username of the admin user with necessary privileges | |
adminPass: The password for the admin user | |
adminDbName: The name of the admin database to use as authSource | |
*/ | |
const createDbAndUser = ({ | |
mongoHost, | |
dbName, | |
username, | |
password, | |
adminUser, | |
adminPass, | |
adminDbName = 'admin' | |
} = {}) => { | |
const isMissingArg = | |
!mongoHost || | |
!dbName || | |
!username || | |
!password || | |
!adminUser || | |
!adminDbName | |
if (isMissingArg) { | |
return Promise.reject({ message: 'Missing argument' }) | |
} | |
var client | |
const newDbAdminUri = `mongodb://${adminUser}:${adminPass}@${mongoHost}/${dbName}` | |
return MongoClient.connect( | |
newDbAdminUri, | |
{ authSource: adminDbName } | |
) | |
.then(newDbAdminClient => { | |
client = newDbAdminClient | |
return Promise.resolve() | |
}) | |
.catch(err => { | |
console.error( | |
`Error connecting as admin '${adminUser}' to '${dbName}'' via authSource '${adminDbName}'` | |
) | |
return Promise.reject(err) | |
}) | |
.then(() => { | |
return client | |
.command({ | |
createUser: username, | |
pwd: password, | |
roles: [{ role: 'dbOwner', db: dbName }] | |
}) | |
.catch(err => { | |
if ( | |
err && | |
err.message && | |
err.message.includes('already exists') | |
) { | |
console.warn( | |
`Warning: User '${username} already exists on database '${dbName}'` | |
) | |
return Promise.resolve() | |
} else { | |
console.error( | |
`Error creating user '${username}' on database '${dbName}': ${err}` | |
) | |
client.close() | |
return Promise.reject(err) | |
} | |
}) | |
}) | |
.then(() => { | |
client.close() | |
}) | |
} | |
const test_createDbAndUser = () => { | |
return createDbAndUser({ | |
mongoHost: 'mongo', | |
dbName: 'newnewdb', | |
username: 'jinyang1', | |
password: 'piedpiper1', | |
adminUser: 'ffdevadmin', | |
adminPass: 'tCX9Qa1ZSsypp39eSeRD', | |
adminDbName: 'admin' | |
}) | |
.then(() => { | |
console.log('Successfully created new DB and user') | |
const newUri = 'mongodb://jinyang:piedpiper@mongo/newnewinternet' | |
return MongoClient.connect(newUri) | |
.then(client => { | |
console.log( | |
'Successfully connected as jinyang to newnewinternet' | |
) | |
client.close() | |
}) | |
.catch(err => { | |
console.error( | |
'createDbAndUser successful, but failed to connect' | |
) | |
return Promise.reject(err) | |
}) | |
}) | |
.catch(err => { | |
console.error('Failed with error:', err) | |
}) | |
} |