How to compose MongoDB connection strings - Noteworthy at Compose

Published

This is your weekly summary of Compose news for those changes and updates which can make your life easier. In this September 10th 2018 edition, we look at how the visibility of MongoDB connection strings has changed and how have a quick course in creating them yourself. And we also take a browse through the past week's Compose Articles.

MongoDB and Database connection strings

When we updated our MongoDB connection strings, we set out to make things simpler. One thing we found was that users were not that sure which connection strings to use.

Other connection strings you ask? Yes, as well as the set of connection strings shown in the overview, there were connection strings shown in the data browser. You selected the Browser, then one of the databases, then selected the Admin tab and there were a pair of connection strings. All that was really changed in them though was the name of the database.

Refreshing connection strings

A MongoDB connection string might look like this:

mongodb://admin:myword@portal-ssl662-9.allthreefour.3268603687.composedb.com:27722/compose?authSource=admin&ssl=true  

It starts with the scheme - mongodb, followed by the username - admin, the password - myword, hostname - portal-ssl662-9.allthreefour.3268603687.composedb.com, port - 27722, database - compose and some other bits we'll come to in a moment.

That's what you'll see on the overview page, bar the password, which we leave for you to substitute in. Your connection strings will, obviously, have different host names and ports.

Now, with MongoDB, you can have multiple databases in one deployment. Your out-of-the-box MongoDB on Compose comes with two: admin which is only there for database user administration and compose a database we've created for you where you can start creating collections. It's that compose database that the connection strings point you to by default. Want to point to another database you've created? Just change the compose part to the name of the other database.

A note about users

Although the Compose connection string refers to the admin user, you can't actually use the admin user. That's because we don't reveal the password to you. It's all part of how we managed the database for you. You can, though, go to the admin database in the data browser and create a user of similar capabilities there. When you have done that, do remember to be careful - the admin user must be left in place. Use your newly created user with care.

The authSource of power

Now, each database can have its own users configured, but by default they don't. You can use MongoDB to add users to a database or you can add them through the Compose data browser. When you've got a fresh MongoDB though, you need to tell MongoDB where to look for the user you are working with.

That's what the ?authSource=admin bit of the MongoDB connection string is. It says "go look in the admin database for this user". As the user is the "admin" user which can manage all databases in a MongoDB deployment, its credentials live in the admin database. If you create a user within a database, then you can drop the ?authSource=admin - then MongoDB looks for the user in the database you specify.

Parameters and connection strings

A small note on that though. Notice that the connection string finishes with &ssl=true. The ? denotes a parameter list for the URL and each name=value pair is separated by an ampersand - & - so, when you do drop the authSource=admin, remember to replace the & with a ? as it's now the first parameter of the connection string.... :1234/mydbstuff?ssl=true. That ssl=true tells the driver to use TLS/SSL to encrypt the connection. If thats not there, the connection will be dropped as it'll be insecure.

So, now you should be ready to create any connection string you need for your Compose MongoDB deployment and its databases.

Compose Articles

Another quiet week for new articles. Last week's Noteworthy at Compose looked at MongoDB, connections and the new Lets Encrypt options that have been enabled.

And as always we had NewsBits which covered Redis 5 RC 5, MongoDB refreshes, Firefox 62 and the new ESR release, Node 10.10, the latest Visual Studio Code and Atom and what's next for Java.

That's it for this week's Noteworthy at Compose. Onwards to next week!

Dj Walker-Morgan
Dj Walker-Morgan is Compose's resident Content Curator, and has been both a developer and writer since Apples came in II flavors and Commodores had Pets. Love this article? Head over to Dj Walker-Morgan’s author page to keep reading.

Conquer the Data Layer

Spend your time developing apps, not managing databases.