简体   繁体   English

将猫鼬连接到 mongoDB 图集和 nodejs

[英]connecting mongoose to mongoDB atlas and nodejs

This is my mongoose connection code:这是我的猫鼬连接代码:

mongoose.connect("mongodb+srv://Sarthak:*******:Wb@cluster0-jli2a.mongodb.net/test?retryWrites=true",{ useNewUrlParser: true })
    .then(()=>{
        console.log("Connected to mongo database");
    })
    .catch((err)=>{
        console.log("Error connecting mongo database",err);
    });

I got the errors below, any idea how to fix this?我收到以下错误,知道如何解决这个问题吗?

Error connecting mongo database { MongoParseError: Unescaped colon in authority section at parseConnectionString (/home/sarthak/Projects/thePracticalGuide/node_modules/mongodb-core/lib/uri_parser.js:250:23) at QueryReqWrap.dns.resolveTxt [as callback] (/home/sarthak/Projects/thePracticalGuide/node_modules/mongodb-core/lib/uri_parser.js:126:7) at QueryReqWrap.onresolve [as oncomplete] (dns.js:240:10) name: 'MongoParseError', [Symbol(mongoErrorContextSymbol)]: {} }错误连接 mongo 数据库 { MongoParseError: Unescaped Colon in authority 部分 parseConnectionString (/home/sarthak/Projects/thePracticalGuide/node_modules/mongodb-core/lib/uri_parser.js:250:23) at QueryReqWrap.dns.resolveTxt [作为回调] (/home/sarthak/Projects/thePracticalGuide/node_modules/mongodb-core/lib/uri_parser.js:126:7) 在 QueryReqWrap.onresolve [as oncomplete] (dns.js:240:10) 名称:'MongoParseError', [符号(mongoErrorContextSymbol)]:{}}

这是我在连接时遇到的错误

Just go to atlas website security tab and edit the password of the user and make sure you do not use "@" or ":".只需转到 atlas 网站安全选项卡并编辑用户的密码,并确保您不使用“@”或“:”。 That's it.就是这样。

您需要在连接字符串中对密码进行编码:

const connectionString = `mongodb://yourUsername:${encodeURIComponent('yourPassword')}@127.0.0.1:27017/mydb`;

The error description is pretty clear - do you have a colon in your password?错误描述非常清楚 - 您的密码中有冒号吗? The typical connectionstring format is "mongodb+srv:[username:password@]host1..." so an unescaped colon would throw a parse error.典型的连接字符串格式是“mongodb+srv:[username:password@]host1...”,因此未转义的冒号会引发解析错误。

I had this same problem, also with "unescaped colons" even though they were very clearly escaped.我也有同样的问题,即使它们很明显地被转义,也有“未转义的冒号”。 Try this:尝试这个:

var uri = encodeURI('mongodb+srv://Sarthak:*******:Wb@cluster0-jli2a.mongodb.net/test?retryWrites=true');

Try to use this way of connection too也尝试使用这种连接方式

 /* I've removed the ":Wb" between your password and @clus... As mongoDB atlas website didn't use that in my generated connection string */
  mongoose.connect("mongodb+srv://Sarthak:*******@cluster0-jli2a.mongodb.net/test?retryWrites=true",{ useNewUrlParser: true });    

  mongoose.connection.on('error', (err) => {
    console.error(`Mongoose connection error: ${err}`);
    process.exit(1);
  });

Otherwise things to consider:否则需要考虑的事情:

  • Make sure that you've added your connecting device IP address in the IP whitelist in the security tap of your cluster in mongoDB atlas website (if you've already done that you might try giving permission to every IPs by adding 0.0.0.0/0 to check there's no issue regarding to this)确保您已将连接设备 IP 地址添加到 mongoDB atlas 网站集群安全分接头的 IP 白名单中(如果您已经这样做了,您可以尝试通过添加 0.0.0.0/0 来授予每个 IP 的权限检查没有问题)
  • Regarding to the password, you may got confused with the mongoDB atlas login password, than the user you made for the cluster (this is a common mistake that mongoDB atlas newbies make).关于密码,您可能会混淆 mongoDB atlas 登录密码,而不是您为集群创建的用户(这是 mongoDB atlas 新手常犯的错误)。
  • If not and you're using the right password, you can try to delete the user and re-create it again in the security tab (in the clusters view in mongoDB atlas website).如果没有并且您使用了正确的密码,您可以尝试删除用户并在安全选项卡中重新创建它(在 mongoDB atlas 网站的集群视图中)。 First consider giving the user a very basic password without any special character and try connecting again to ensure that it's not an encoding issue.首先考虑给用户一个没有任何特殊字符的非常基本的密码,然后再次尝试连接以确保它不是编码问题。
  • At the end if none of above worked with you try making connection via shell.最后,如果以上都不适合您,请尝试通过 shell 进行连接。 (you can see the connection string in the mongoDB atlas website, in the connect section of your cluster. There you can get better logs regarding to the cause of your problem. (您可以在 mongoDB atlas 网站的连接字符串中看到集群的连接部分。在那里您可以获得关于问题原因的更好的日志。

I was getting "Unescaped colon in authority section" using MongoDB Compass when entering the connection string.输入连接字符串时,我使用 MongoDB Compass 获得了“授权部分中未转义的冒号”

For which -其中——

I went into "Create Free Cluster" button and made a cluster.我进入“创建免费集群”按钮并创建了一个集群。

Then I got the connection string.然后我得到了连接字符串。 However, while entering the available connection string I got the error.但是,在输入可用连接字符串时出现错误。

I just changed the password by logging into mongodb atlas.我只是通过登录 mongodb atlas 更改了密码。

Here is the procedure I used -->这是我使用的程序-->

 [1]  To change the password - click on encode URI

在此处输入图片说明

 [2]   It will take you here -->

在此处输入图片说明

  [3]   Click on the edit button from the screen above and change your password.

在此处输入图片说明

I followed the above steps and was able to login.我按照上述步骤操作,并且能够登录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM