2016-11-22 93 views
0

我的應用程序正在試圖建立到MongoDB的EC2上使用下面的命令連接:使用mongoose.connect()連接到MongoDB實例在EC2上

mongoose.connect("mongodb://username:[email protected]:27017/databasename") 

我有口27017和28017打開服務器。爲了驗證我也打開了所有的流量,這不是一個好主意。

我也能夠通過蒙戈沒有問題連接:

mongo admin --username username -p --host ec2-xx-xxx-x-xxx.us-west-2.compute.amazonaws.com --port 27017 

當我運行應用程序,我得到以下錯誤:

/home/ubuntu/workspace/website.com/appname/node_modules/mongoose/node_modules/mongodb/lib/utils.js:99 
process.nextTick(function() { throw err; }); 
          ^
MongoError: Authentication failed. 
at Function.MongoError.create (/home/ubuntu/workspace/website.com/appname/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/error.js:31:11) 
at /home/ubuntu/workspace/website.com/appname/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/pool.js:483:72 
at authenticateStragglers (/home/ubuntu/workspace/website.com/appname/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/pool.js:429:16) 
at null.messageHandler (/home/ubuntu/workspace/website.com/appname/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/pool.js:463:5) 
at Socket.<anonymous> (/home/ubuntu/workspace/website.com/appname/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:309:22) 
at emitOne (events.js:77:13) 
at Socket.emit (events.js:169:7) 
at readableAddChunk (_stream_readable.js:153:18) 
at Socket.Readable.push (_stream_readable.js:111:10) 
at TCP.onread (net.js:536:20) 
+1

我看到2個可能的問題:1 - 您的用戶名:密碼未在應用程序中設置。 2 - 你有一個用戶設置爲數據庫名?默認情況下,除非明確設置,否則不需要用戶連接到數據庫 –

回答

1

看來你沒有創建一個用戶嘗試連接的數據庫。我將使用管理員用戶連接MongoDB並創建它:

mongo admin --username root --password rootpassword --host ec2-xx-xxx-x-xxx.us-west-2.compute.amazonaws.com --port 27017 

MongoDB shell version: XXX 
connecting to: x.x.x.x:27017/admin 

> db = db.getSiblingDB('databasename') 
databasename 
> db.createUser({ user: "username", pwd: "password", roles: [ "readWrite", "dbAdmin" ]}) 
{ 
"user" : "username", 
"pwd" : "...", 
"roles" : [ 
    "readWrite", 
    "dbAdmin" 
], 
"_id" : ObjectId("...") 
} 
> exit 
相關問題