2016-11-22 7 views
0

にMongoDBのインスタンスに接続するために)(mongoose.connectを使用して:は私のアプリはコマンドでEC2上の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つの可能性のある問題を見ています。 2 - あなたはdatabasenameのために設定されたユーザーを持っていますか?デフォルトでは、データベースに明示的に設定しない限り、ユーザーはデータベースに接続する必要はありません –

答えて

1

あなたが作成していないようだ

は私も問題はないとのmongoを経由して接続することができますよ接続しようとしているデータベースのユーザー。私はMongoDBをadminユーザと接続して作成します:

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 
関連する問題