Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB password with "@" in it

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?

like image 557
iZ. Avatar asked Sep 20 '11 14:09

iZ.


People also ask

What is my MongoDB password?

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.

What is my MongoDB Atlas password?

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)

How does MongoDB store passwords in encrypted format?

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.


2 Answers

Use this syntax:

// use %40 for @ mongoClient.connect("mongodb://username:p%40ssword@host:port/dbname?authSource=admin", {          useNewUrlParser: true     }, function(err, db) {      } ); 
like image 109
Andrey Hohutkin Avatar answered Sep 25 '22 22:09

Andrey Hohutkin


If your password has special characters:

const dbUrl = `mongodb://adminUsername:${encodeURIComponent('adminPassword')}@localhost:27017/mydb`; 
like image 24
vanduc1102 Avatar answered Sep 24 '22 22:09

vanduc1102