Skip to content

Instantly share code, notes, and snippets.

@BrianMwas
Created August 13, 2023 18:26
Show Gist options
  • Save BrianMwas/66657cfccef74ece830472280de6d59d to your computer and use it in GitHub Desktop.
Save BrianMwas/66657cfccef74ece830472280de6d59d to your computer and use it in GitHub Desktop.
Initialise script that initializes the databases
// Read values from the environment variables
var username = 'user';
var password = 'secret';
var database = 'admin';
// set the authentication database
db = db.getSiblingDB(database)
// Create a user with the specified username, password, and database
db.createUser({
user: process.env.MONGO_INITDB_ROOT_USERNAME || username,
pwd: process.env.MONGO_INITDB_ROOT_PASSWORD || password,
roles: [{ role: 'readWrite', db: process.env.MONGO_INITDB_DATABASE || 'myapp' }]
});
// Enable authentication in the MongoDB configuration
db.adminCommand({ setParameter: 1, authenticationMechanisms: { 'SCRAM-SHA-1': true } });
// Exit the mongo shell
quit();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment