38 lines
1017 B
JavaScript
38 lines
1017 B
JavaScript
// Simple initialization script that only creates users
|
|
print("Starting MongoDB user initialization...");
|
|
|
|
// Wait for MongoDB to be ready
|
|
db = db.getSiblingDB('admin');
|
|
print("Connected to admin database");
|
|
|
|
// Create application database and user
|
|
db = db.getSiblingDB('appdb');
|
|
print("Switched to appdb database");
|
|
|
|
try {
|
|
db.createUser({
|
|
user: 'appuser',
|
|
pwd: 'apppassword',
|
|
roles: [
|
|
{ role: 'readWrite', db: 'appdb' }
|
|
]
|
|
});
|
|
print("Successfully created appuser");
|
|
} catch (err) {
|
|
print("Error creating appuser: " + err.message);
|
|
}
|
|
|
|
// Verify user was created
|
|
let users = db.getUsers();
|
|
print("Users in appdb: " + JSON.stringify(users));
|
|
|
|
// Also create a test collection and document to verify database is working
|
|
try {
|
|
db.test.insertOne({ name: "test", created: new Date() });
|
|
print("Successfully inserted test document");
|
|
} catch (err) {
|
|
print("Error inserting test document: " + err.message);
|
|
}
|
|
|
|
print("MongoDB initialization completed!");
|