使用MongoDB API连接到CosmosDB时发生超时错误 [英] Timeout error when connecting to CosmosDB with MongoDB API
问题描述
我目前正在研究以下教程,以学习通过mongodb API与Azure的cosmosdb一起使用: https://docs.microsoft.com/zh-CN/azure/cosmos-db/tutorial-develop-mongodb-react
I'm currently working through the following tutorial to learn to work with Azure's cosmosdb via the mongodb API: https://docs.microsoft.com/en-us/azure/cosmos-db/tutorial-develop-mongodb-react
本教程的最终版本位于此处: https://github.com/Azure- Samples/react-cosmosdb ,但到目前为止,我只是在第5部分中,您实际上尝试连接到cosmos数据库.
The finished repo for this tutorial is here: https://github.com/Azure-Samples/react-cosmosdb but so far I'm just on part 5 where you actually try to connect to the cosmos database.
I ran into an issue with the mongoose version and substituted the repo code with this found here: Error connecting to Azure: Illegal character in password with mongoose 5.0.1 but works in 4.13.9
我也将所需的mongodb版本降级到2.2.33,因为似乎新版本的mongo无法正常工作. (请参阅以下内容底部的评论: https://docs .microsoft.com/en-us/azure/cosmos-db/mongodb-mongoose )
I also downgraded my required mongodb version to 2.2.33 as it seems that the newer version of mongo doesn't work. (See the comments at the bottom of: https://docs.microsoft.com/en-us/azure/cosmos-db/mongodb-mongoose)
这两个似乎可以解决我的一些错误,但是我仍然坚持如何解决以下问题:
These two seemed to solve some of my errors, however I'm stuck on how to solve the following:
{ MongoError: failed to connect to server [react-cosmos-db-tori.documents.azure.com:10255] on first connect [MongoError: connection 0 to react-cosmos-db-tori.documents.azure.com:10255 timed out]
[1] at Pool.<anonymous> (/Users/vhenri/Documents/dev/azure-app-service/azure-heros/server/node_modules/mongoose/node_modules/mongodb-core/lib/topologies/server.js:336:35)
[1] at emitOne (events.js:116:13)
[1] at Pool.emit (events.js:211:7)
[1] at Connection.<anonymous> (/Users/vhenri/Documents/dev/azure-app-service/azure-heros/server/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:280:12)
[1] at Object.onceWrapper (events.js:317:30)
[1] at emitTwo (events.js:126:13)
[1] at Connection.emit (events.js:214:7)
[1] at TLSSocket.<anonymous> (/Users/vhenri/Documents/dev/azure-app-service/azure-heros/server/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:199:10)
[1] at Object.onceWrapper (events.js:313:30)
[1] at emitNone (events.js:106:13)
[1] at TLSSocket.emit (events.js:208:7)
[1] at TLSSocket.Socket._onTimeout (net.js:407:8)
[1] at ontimeout (timers.js:475:11)
[1] at tryOnTimeout (timers.js:310:5)
[1] at Timer.listOnTimeout (timers.js:270:5)
[1] name: 'MongoError',
[1] message: 'failed to connect to server [react-cosmos-db-tori.documents.azure.com:10255] on first connect [MongoError: connection 0 to react-cosmos-db-tori.documents.azure.com:10255 timed out]' }
据我所知这只是一个超时,因为它无法连接到我的数据库.
As far as I can tell this is just a timeout because it can't connect to my database.
这是我的代码:
const mongoose = require('mongoose');
const env = require('./env/environment');
mongoose.Promise = global.Promise;
function connect() {
return(
mongoose.connect('mongodb://react-cosmos-db-tori.documents.azure.com:10255/react-cosmos-db-tori?ssl=true', {
auth: {
user: 'react-cosmos-db-tori',
password: '<mypasswordhere>'
}
})
.then(() => console.log('connection successful'))
.catch((err) => console.log(err))
)
}
module.exports = {
connect,
mongoose
};
以下是我在Azure中的连接字符串页面的屏幕截图:
Heres a screenshot of my connection string page in Azure:
这是我的package.json:
and here's my package.json:
{
"name": "express-react-starter",
"version": "0.1.0",
"private": true,
"dependencies": {
"body-parser": "^1.17.2",
"cookie-parser": "^1.4.3",
"debug": "~2.6.3",
"express": "^4.15.3",
"jade": "^1.11.0",
"mongodb": "^2.2.33",
"mongoose": "^5.0.7",
"morgan": "^1.8.2",
"react": "^15.6.1",
"react-dom": "^15.6.1"
},
"devDependencies": {
"concurrently": "^3.5.0",
"nodemon": "^1.12.0",
"react-scripts": "1.0.10"
},
"scripts": {
"start": "concurrently \"react-scripts start\" \"nodemon server/server.js\"",
"build": "react-scripts build && mv build server",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"proxy": "http://localhost:3001"
}
我在启动时也遇到以下错误:
I also get the following error on start:
DeprecationWarning: `open()` is deprecated in mongoose >= 4.11.0, use `openUri()` instead, or set the `useMongoClient` option if using `connect()` or `createConnection()`. See http://mongoosejs.com/docs/connections.html#use-mongo-client
当我尝试刷新希望看到一个空数组的页面时,我仅看到一个chrome错误:localhost didn’t send any data. ERR_EMPTY_RESPONSE
,并且控制台中显示以下内容:GET /api/heroes - - ms - -
,但我没有得到上面的大MongoError.但是我也没有获得成功的连接".
When I try to refresh the page that I'm expecting to see an empty array, I only see a chrome error: localhost didn’t send any data. ERR_EMPTY_RESPONSE
and the following is displayed in the console: GET /api/heroes - - ms - -
and I don't get the big MongoError above. But I also don't get my 'connection successful' either.
我当时想这可能与连接URL中的数据库名称有关,因此我创建了一个名为heroes_test的集合,并尝试使用mongodb://react-cosmos-db-tori.documents.azure.com:10255/heroes_test?ssl=true
进行操作,但还是发生了同样的事情.
I was thinking that it might perhaps be an issue with what I put as the databasename in the connect url, so I created a collection called heroes_test and tried using mongodb://react-cosmos-db-tori.documents.azure.com:10255/heroes_test?ssl=true
but the same thing happens.
有人知道这是怎么回事吗?我可能使用诺言东西有误吗? 任何帮助,将不胜感激!
Anyone know what's going on here? Am I maybe using the promise thing wrong? Any help would be appreciated!
推荐答案
对于可能偶然发现此类似问题的任何人,我终于弄明白了!问题出在我的防火墙上-它阻止了我与CosmosDB的连接.当我通过家庭互联网尝试使用时,效果很好.
For anyone who might stumble across this similar issue, I finally figured it out! The issue was with the firewall at my work - it was blocking my connection to the CosmosDB. When I tried it from my home internet it worked fine.
这篇关于使用MongoDB API连接到CosmosDB时发生超时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!