Created
August 13, 2023 18:26
-
-
Save BrianMwas/66657cfccef74ece830472280de6d59d to your computer and use it in GitHub Desktop.
Initialise script that initializes the databases
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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