alkitony
alkitony

Reputation: 21

using a mongoose.connect() to connect to a mongodb instance on ec2

My app is trying to establish a connection to a mongoDB on EC2 with the command:

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

I have port 27017 and 28017 open on the server. Just to verify I also have all traffic open as well, which is not a good idea.

I am also able to connect via mongo with no problem:

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

When I run the app I get the following error:

/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)

Upvotes: 1

Views: 1614

Answers (1)

Juan Ariza
Juan Ariza

Reputation: 1058

It seems that you didn't create an user for the database you're trying to connect. I will to connect MongoDB using the admin user and create it:

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

Upvotes: 2

Related Questions