mongodb+srv URI 不能有端口号

问题描述 投票:0回答:3

我有(注:用户名和密码是虚构的):

const CONNECTION_URL = "mongodb+srv://smith_bob:[email protected]: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));

但是当我运行 nodemon index.js 时得到以下信息:

mondodb+srv URI 不能有端口号 \
[nodemon] 干净退出 - 重新启动之前等待更改

有什么想法吗?

我尝试使用百分比编码字符,但出现如下错误:

. . . smith_bob:[email protected]:%40@llyHo!: . . .

mongodb+srv URI 不能有端口号

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

bad auth:身份验证失败

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

密码包含未转义字符

. . . smith_bob:[email protected]%3AT%40llyHo!: . . . 

mongodb+srv URI 不能有端口号

node.js mongodb express
3个回答
9
投票

似乎是因为在主机

@
之前存在多个
:
cluster0.r92qc.mongodb.net
而没有百分比编码。尝试以以下格式提供:

mongodb+srv://username:password@host

如果用户名或密码包含

: / ? # [ ] @
字符之一,则必须使用百分比编码进行转换。 以您的连接字符串为例,如果用户名是
smith_bob:[email protected]
,密码是
T@llyHo!:
,则应分别转换为
smith_bob%3Asmith_bob%40hotmail.com
T%40llyHo!%3A
,连接字符串将变为:

"mongodb+srv://smith_bob%3Asmith_bob%40hotmail.com:T%40llyHo!%[email protected]/myFirstDatabase?retryWrites=true&w=majority"

要了解有关连接字符串格式的更多信息,请参阅此处


0
投票

尝试上一篇文章中提到的百分比解码。对我来说,它有效。 如果用户名是

user
并且密码是
user@
那么
mongodb+srv://user:user%[email protected]/myFirstDatabase?retryWrites=true&w=majority

其中 %40 是密码中 @ 的百分比编码。


0
投票

如果您的用户名或密码包含以下任意一项: / ? #[ ] @ 字符,必须转换为百分号编码。请检查以下链接并根据您的符号进行更改。

https://developer.mozilla.org/en-US/docs/Glossary/Percent-encoding

© www.soinside.com 2019 - 2024. All rights reserved.