Connecting to MongoDB through SSL

392 views
Skip to first unread message

Fabian Bernhard

unread,
Jan 14, 2014, 2:24:32 AM1/14/14
to mongoo...@googlegroups.com
Dear list,

I try to connect to the MongoDB Server through SSL, using the following code:

var config = {
mongo: {
host: "example.com",
port: 27017,
db: "my-db",
username: "webuser",
password: "secret"
}
}

var dbstring = "mongodb://" + config.mongo.host + ":" + config.mongo.port + "/"
+ config.mongo.db;

var dboptions = {
username: config.mongo.username,
password: config.mongo.password
};

mongoose.connect(dbstring, dboptions);

The connection fails, the MongoDB server log says:

Tue Jan 14 10:12:03.081 [initandlisten] connection accepted from XXX.XXX.XXX.XXX:49967 #15 (3 connections now open)
Tue Jan 14 10:12:03.105 [conn15] ERROR: no SSL certificate provided by peer; connection rejected
Tue Jan 14 10:12:03.105 [conn15] SocketException handling request, closing client connection: 9001 socket exception [CONNECT_ERROR] 



How can I provide an SSL certificate to the MongoDB connection through Mongoose Node.JS?

Thank you!



Fabian Bernhard

unread,
Jan 14, 2014, 7:10:53 AM1/14/14
to mongoo...@googlegroups.com
Reading through the code I have found the answer myself. The correct configuration is this:

var dboptions = {
username: config.mongo.username,
password: config.mongo.password,
server: { 
ssl: true,
sslCert: fs.readFileSync('./ssl/mongodb.crt'),
sslKey: fs.readFileSync('./ssl/mongodb.key')
}
};

With this the encrypted communication with the MongoDB works. 

Fabian Bernhard

unread,
Jan 15, 2014, 3:27:37 AM1/15/14
to mongoo...@googlegroups.com
Correction: It should be user/pass and not username/password.

var dboptions = {
user: config.mongo.username,
pass: config.mongo.password,
Reply all
Reply to author
Forward
0 new messages