Can not connect to the databaseMongoParseError: Unescaped at-sign in authority section

18,389

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 :)

Image

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:

MongoDB cluster interface (as of Feb 2019)

Share:
18,389
Suz Zan
Author by

Suz Zan

Updated on July 29, 2022

Comments

  • Suz Zan
    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 console

    Can 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
    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
    Suz Zan over 5 years
    I am doing the same as you can see in the question too.
  • bcg
    bcg over 5 years
    what happened if you write the connection like this: mongodb://user%40gmail.com:%[email protected]‌​/test?retryWrites=tr‌​ue
  • Suz Zan
    Suz Zan over 5 years
    getting an error : MongoParseError: Unescaped at-sign in authority section
  • bcg
    bcg over 5 years
    try to use this connection option: { uri_decode_auth: true }
  • bcg
    bcg over 5 years
    do you fix the problem?
  • Suz Zan
    Suz Zan over 5 years
    not yet... still the same error is showing up. I have tried many options still the same
  • bcg
    bcg over 5 years
    why not change username to another without @? is not good practice using @ to database username
  • Suz Zan
    Suz Zan over 5 years
    email 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
    Suz Zan over 5 years
    yeah... 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
    Admin over 5 years
    Getting 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
    Ahmad S Habibi about 4 years
    you 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-0‌​0-4iypj.mongodb.net:‌​27017,yourCluster-sh‌​ard-00-01-4iypj.mong‌​odb.net:27017,yourCl‌​uster-shard-00-02-4i‌​ypj.mongodb.net:2701‌​7/yourDatabase?ssl=t‌​rue&replicaSet=yourC‌​luster-shard-0&authS‌​ource=admin&retryWri‌​tes=true&w=majority