Not able to configure MongoDb with Mongo Express username and password #5736
-
I am trying to setup a mongodb database with a username and password and also trying to add mongo express and protect it with a username and password.
Here is my code sample I was playing around with
Any idea whats going on here? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hi @picBuilderLabs Issue 1: Unescaped Special CharactersIn URLs (including MongoDB connection strings), special characters like Issue 2: Incorrect MongoDB Connection String FormatThe connection string format you're using has an issue. MongoDB connection strings shouldn't contain both
Given that you're trying to connect to a local MongoDB instance (port Fixes
Final Code:builder.AddMongoDB(name, port)
.WithImage("mongo")
.WithImageTag("7.0")
.WithEnvironment("MONGO_INITDB_ROOT_USERNAME", "root")
.WithEnvironment("MONGO_INITDB_ROOT_PASSWORD", "Password12!")
//.WithDataVolume("mongovolume")
.WithMongoExpress(config =>
{
config.WithHostPort(8091);
// Fixing connection string format and escaping the "!" character
config.WithEnvironment("ME_CONFIG_MONGODB_SERVER", "mongodb://root:Password12%21@mongodb:27017");
config.WithEnvironment("ME_CONFIG_MONGODB_ENABLE_ADMIN", "true");
config.WithEnvironment("ME_CONFIG_BASICAUTH_USERNAME", "admin");
// You should also escape the "!" character in the Mongo Express password
config.WithEnvironment("ME_CONFIG_BASICAUTH_PASSWORD", "Password12%21");
config.WithEnvironment("ME_CONFIG_SITE_BASEURL", "/mongo-express");
config.WithEnvironment("ME_CONFIG_BASICAUTH", "true"); // Enable basic auth
}); Key Changes:
Now, the connection string should be properly formatted, and the special characters in the password will no longer cause issues. This should resolve your connection and authentication problems for both MongoDB and Mongo Express. |
Beta Was this translation helpful? Give feedback.
Thank you for the reply, your post motivated me to get this darn thing resolved. These are the steps I took to resolve this issue:
Heres the code which works: