I'm trying to connect to a MongoDB database with a username and password using Mongoose in Node.js. All the docs say that the connection string should look like
mongodb://username:password@host:port/db
However, the password contains the '@' character in it. How can I make a connection string out of this that mongoose will understand? Can I escape the '@' in the password or is there another method of connecting I have to use?
By default mongodb has no enabled access control, so there is no default user or password. To enable access control, use either the command line option --auth or security.
MongoDB Atlas does not have a default user/password combination. To enable access to a cluster you need to: Add a new database user with appropriate permissions. Configure whitelist entries to allow remote access to your cluster from trusted IP(s)
As of version 3.2, MongoDB has no native support for password hashing like some SQL databases provide, so you will have to implement it in Java. To generate a new account or change the password of an existing account: generate a cryptographically secure random salt value with java. security.
Use this syntax:
// use %40 for @ mongoClient.connect("mongodb://username:p%40ssword@host:port/dbname?authSource=admin", { useNewUrlParser: true }, function(err, db) { } );
If your password has special characters:
const dbUrl = `mongodb://adminUsername:${encodeURIComponent('adminPassword')}@localhost:27017/mydb`;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With