我正在尝试在ec2实例上运行我的应用程序。每次启动它都会抛出错误。好像它无法连接到我的mongodb数据库。我通过npm安装了mongoose和mongodb。我在与服务器相同的ec2实例上运行mongodb。我已经为我的ec2安全组添加了一个自定义tcp规则,以允许端口27017上的传入流量。
谁能帮我弄清楚什么是错的?我一直试图在过去3个小时内修复它。这是我连接到mongodb的代码:
var mongoose = require('mongoose');
var db = mongoose.createConnection('localhost', 'app');
这是我一直收到的错误:
ubuntu@:~/app$ node app.js
info - socket.io started
connect.multipart() will be removed in connect 3.0
visit https://github.com/senchalabs/connect/wiki/Connect-3.0 for alternatives
connect.limit() will be removed in connect 3.0
Express server listening on port 8080
events.js:72
throw er; // Unhandled 'error' event
^
Error: failed to connect to [localhost:27017]
at null.<anonymous> (/home/ubuntu/app/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:553:74)
at EventEmitter.emit (events.js:106:17)
at null.<anonymous> (/home/ubuntu/app/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:140:15)
at EventEmitter.emit (events.js:98:17)
at Socket.<anonymous> (/home/ubuntu/polls/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:512:10)
at Socket.EventEmitter.emit (events.js:95:17)
at net.js:441:14
at process._tickCallback (node.js:415:13)
答案 0 :(得分:0)
npm实际上并没有为您安装mongo服务器,因此请确保它已安装并正在运行。
要验证它是否正在运行,请作为您实例上的root用户:
service mongod status
接下来,确认您可以连接到它:
mongo
> show dbs
我怀疑如果你能达到这一点,你的应用程序就可以运行。