Can not connect to the databaseMongoParseError: Unescaped at-sign in authority section
Solution 1
It's okay to encode the password like so:
const DB_USER = 'user';
const PASSWORD = encodeURIComponent('hello@123');
const DB_URL = `mongodb://${DB_USER}:${PASSWORD}@ds331735.mlab.com:31772/any_db`;
If there's an @
in your password, you should change it to %40
.
Solution 2
const dbName = 'your db name';
const dbUser = 'your db username';
const dbPassword = 'your db user password';
const MONGODB_URI = `mongodb://${dbUser}:${dbPassword}@ds047207.mlab.com:47207/${dbName}`;
Note : @ds047207.mlab.com:47207 its provide u in your account check. its not connect try to simple username and password. create user ex: username :asif , password :asif123 Bcoz i had create user with username :asif-vora and password :asif@1234 its getting error in database connection when i try with username :asif , password :asif123 its work fine :)
Solution 3
Found the same issue, but after all resulted that is not the mongodb atlas account user (which has email as the username) the one needed for login.
The user needed is one which has access to the cluster created in mongodb atlas.
Project > clusters > [Select cluster]
Security tab > select/create new db user
These users need not have special characters (there's even a message indicating that such chars will be encoded)
See image below:
Suz Zan
Updated on July 29, 2022Comments
-
Suz Zan over 1 year
I am trying to connect to mongodb server in my nodeJS project. I have a db config file as DB.js
module.exports = { DB: 'mongodb+srv://user%40gmail.com:%[email protected]/test?retryWrites=true' };
username contains @ (%40) mark as it is an email id and the password contains $(%24) characters.
And i have connected that in my server.js file as
mongoose = require('mongoose'), config = require('./config/DB'); const app = express(); mongoose.Promise = global.Promise; mongoose.connect(config.DB, { useNewUrlParser: true } ).then( () => {console.log('Database is connected') }, err => { console.log('Can not connect to the database'+ err)} );
and I have added the custom
serve
option in my package.json file to run the nodemon."scripts": { "ng": "ng", "start": "ng serve", "build": "ng build", "test": "ng test", "lint": "ng lint", "e2e": "ng e2e", "serve": "nodemon server.js" },
But when i tried to run the project using the command
npm run serve
, i am getting an error message in the consoleCan not connect to the databaseMongoParseError: Unescaped at-sign in authority section
I have searched many questions in the stack-overflow also but nothing worked for me.
Any help is appreciated. Thank You
-
Suz Zan over 5 years
{ useNewUrlParser: true }
is needed as i am getting in console ` DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect. ` -
Suz Zan over 5 yearsI am doing the same as you can see in the question too.
-
bcg over 5 yearswhat happened if you write the connection like this:
mongodb://user%40gmail.com:%[email protected]/test?retryWrites=true
-
Suz Zan over 5 yearsgetting an error : MongoParseError: Unescaped at-sign in authority section
-
bcg over 5 yearstry to use this connection option:
{ uri_decode_auth: true }
-
bcg over 5 yearsdo you fix the problem?
-
Suz Zan over 5 yearsnot yet... still the same error is showing up. I have tried many options still the same
-
bcg over 5 yearswhy not change username to another without @? is not good practice using @ to database username
-
Suz Zan over 5 yearsemail is used as an username by default while signing up in the mongodb server. and we can not change it after that. Thats the reason
-
Suz Zan over 5 yearsyeah... that is also my problem. I had some special characters in my username and password. and now i can not change the username. And still struggling to connect to mongodb
-
Admin over 5 yearsGetting the same error. The temporary solution is just to delete that user and make a new user with some other simple username and password. I tried it.
-
Ahmad S Habibi about 4 yearsyou have 3 shard when create db, change it with yours db name shard and also username and password. DB: mongodb://yourUsername:<yourpassword>@yourCluster-shard-00-00-4iypj.mongodb.net:27017,yourCluster-shard-00-01-4iypj.mongodb.net:27017,yourCluster-shard-00-02-4iypj.mongodb.net:27017/yourDatabase?ssl=true&replicaSet=yourCluster-shard-0&authSource=admin&retryWrites=true&w=majority