mongodb+srv URI cannot have port number

Viewed 8232

I have (note: username and password are made up):

const CONNECTION_URL = "mongodb+srv://smith_bob:smith_bob@hotmail.com:T@llyHo!:@cluster0.r92qc.mongodb.net/myFirstDatabase?retryWrites=true&w=majority";
const PORT = process.env.PORT || 5000;

mongoose.connect(CONNECTION_URL,{useNewUrlParser: true, useUnifiedTopology: true})
    .then(()=>app.listen(PORT,()=> console.log(`Server running on port: ${PORT}`)))
    .catch((error)=> console.log(error.message));

but get the following when I run nodemon index.js:

mondodb+srv URI cannot have port number \
[nodemon] clean exit - waiting for changes before restart

Any ideas what's going on?

I tried using Percent Encoded characters but got errors as follows:

. . . smith_bob:smith_bob@hotmail.com:%40@llyHo!: . . .

mongodb+srv URI cannot have port number

. . . smith_bob%3Asmith_bob%40hotmail.com%3AT%40llyHo!%3A . . .

bad auth : authentication failed

. . . smith_bob:smith_bob%40hotmail.com%3AT%40llyHo!: . . .

Password contains unescaped characters

. . . smith_bob:smith_bob@hotmail.com%3AT%40llyHo!: . . .

mongodb+srv URI cannot have port number

2 Answers

Seems like it's because of the presence of multiple @ and : prior to the host cluster0.r92qc.mongodb.net without percent encoding. Try to provide it in the following format:

mongodb+srv://username:password@host

If the username or password has any one of : / ? # [ ] @ characters, it must be converted using percent encoding. Taking your connection string for example, if the username is smith_bob:smith_bob@hotmail.com and password is T@llyHo!:, it should be converted to smith_bob%3Asmith_bob%40hotmail.com and T%40llyHo!%3A, respectively, and the connection string would become:

"mongodb+srv://smith_bob%3Asmith_bob%40hotmail.com:T%40llyHo!%3A@cluster0.r92qc.mongodb.net/myFirstDatabase?retryWrites=true&w=majority"

To read more about the Connection String format, refer here.

Try percentage decoding as mentioned in the previous post. For me, it worked. if user_name is user and password is user@ then mongodb+srv://user:user%40@cluster0.r92qc.mongodb.net/myFirstDatabase?retryWrites=true&w=majority

where %40 is percentage encoded for @ in the password.

Related