MongoDB password with “@” in it

前端 未结 16 1789
遇见更好的自我
遇见更好的自我 2020-11-27 14:03

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

<         


        
相关标签:
16条回答
  • 2020-11-27 14:12

    Use the options parameter of the mongoose.connect call to specify the password instead of including it in the URL string:

    mongoose.connect('mongodb://localhost/test',
                     {user: 'username', pass: 'p@ssword'},
                     callback);
    
    0 讨论(0)
  • 2020-11-27 14:12

    This solution requires an extra dependency, but it was what finally worked for me.

    Add mongodb-uri to your project and the following lines to your code:

    const mongoose = require('mongoose')
    const mongodbUri = require('mongodb-uri')
    let mongooseUri = mongodbUri.formatMongoose(config.mongo.uri)
    mongoose.connect(mongooseUri, config.mongo.options)
    

    I found this suggestion in mongoose's GitHub issue #6044.

    0 讨论(0)
  • 2020-11-27 14:13

    None of the solutions mentioned above worked for me. I researched it further and found out that I had to include the useNewUrlParser parameter.

    mongoose.connect(db, {
        useNewUrlParser : true
        },
        err => {
        if (err){
            console.error('Error: ' + err)
        }
        else{
            console.log('Connected to MongoDb')
        }
    })
    

    From what I understand, you need a specific version of MongoDB in order to use this. For more details, check Avoid “current URL string parser is deprecated” warning by setting useNewUrlParser to true

    It is to get rid of the warning but clearly the version also affect the required parameter.

    I haven't tested all special characters but it definitely works with '@#$'.

    Hope this helps.

    0 讨论(0)
  • 2020-11-27 14:13
    Also, if your password contains a percentage, %, 
     Because the percent ("%") character serves as the indicator for percent-encoded octets, it must be percent-encoded as "%25" for that octet to be used as data within a URI
    
    for example, if your password is John%Doe, the new transformed password will be John%25Doe or
    If password is Paul%20Wait, New Password will be Paul%2520Wait
    
    mongoClient.connect("mongodb://username:John%25Doe@host:port/dbname", function(err, db) {
    // password is John%Doe
        }`enter code here`
    );
    
    0 讨论(0)
  • 2020-11-27 14:15

    For those connecting with Mongo Compass.(MacOSx) Simply go to your cluster->Security(Tab)at mongodb.com

    Then

    edit your password(press the edit button on your username):

    You will get a modal/popup/dialog:Press edit password underneath your name(the button is greyed out by default but appears just underneath your name)->Then press Update User

    Next:

    Close your mongo db compass application if its running:(Quit Mongo)

    Next Step:

    Return to the Overview Tab in mongodb.com.and select Connect

    Return to OverView:

    Next Step:

    In the popup dialog select Connect with MongoDB Compass then in the next view select a version you want to use(preferably VersionNumber earlier):

    Then:

    Copy the URI String presented to you:

    Reopen MongoDB Compass application:

    And it will give you the option/popup to use the URI String detected:Click Yes

    Final step:

    Enter your new password and Connect. Now your connection should now be successful.

    0 讨论(0)
  • 2020-11-27 14:16

    sometimes you need to connect to the DB using other tools that accept string only as connection string. so just change the @ sign with %40

    0 讨论(0)
提交回复
热议问题