Connecting to MongoDB fails

If you are connecting to a MongoDB Database on Enclave, either through your app or a Database Tunnel, you might hit an error such as this one:

MongoDB shell version: 3.2.1
connecting to: 172.17.0.2:27017/db
2016-02-08T10:43:40.421+0000 E QUERY    [thread1] Error: network error while attempting to run command 'isMaster' on host '172.17.0.2:27017'  :
connect@src/mongo/shell/mongo.js:226:14
@(connect):1:6

exception: connect failed

Cause

This error is usually caused by attempting to connect without SSL to a MongoDB server that requires it, which is the case on Enclave.

Resolution

To solve the issue, connect to your MongoDB server over SSL.

Clients

Connection URLs generated by Enclave include the ssl=true parameter, which should instruct your MongoDB client to connect over SSL. If your client not connect over SSL despite this parameter, consult its documentation.

CLI

Tip

Make sure you use a hostname to connect to MongoDB databases when using a database tunnel.

If you use an IP address for the host, certificate verification will fail.

You can work with --sslAllowInvalidCertificates in your command line, but using a hostname is simpler and safer.

The MongoDB CLI client does not accept database URLs. Use the following to connect:

mongo --ssl \
        --username aptible --password "$PASSWORD" \
        --host "$HOST" --port "$PORT"