After installing MongoDB for the first time, or spinning up a new cluster on MongoDB Atlas, one of the first challenges you may face is how to connect to your database so that you can start entering and querying data. To connect to your MongoDB cluster, you'll need a connection string.
A MongoDB connection string is a short piece of text that tells a MongoDB client how to connect to your MongoDB cluster. The URI can be provided to various tools, like MongoDB Compass and the MongoDB Shell, or it can be used in your own software built on one of the MongoDB drivers to specify a MongoDB cluster.
The URL can contain various information, such as the servers that make up the cluster (or how to find out that information), your database user's username and password, and the name of a default database to use for running queries, unless that's overridden in some other way.
There are three types of MongoDB connection string that you'll encounter: A MongoDB Atlas connection string, a connection string for an instance running on your own computer (localhost), and the connection string for a self-hosted cluster.
We'll explain how to obtain each of these connection strings below.
In the MongoDB Atlas web interface, find the cluster to connect to, and click on the "Connect" button next to its name. In the next screen, select the method you're using to connect to MongoDB, and you'll be provided with a suitable connection string for your cluster. There's a small “copy-paste” button next to the connection string to make your life even easier!
You can find more information about MongoDB Atlas connection strings in the MongoDB documentation.
Be aware that if you choose the MongoDB Shell option, you'll be provided with the whole command-line for starting mongosh, and not just the connection string!
The connection string won't be complete. There will usually be placeholders for your username and password, surrounded by angle-brackets, like this:
You'll need to find the username and password for your database user, or create a new database user to obtain these credentials. You can do that using the "Database Access" link on the left-hand side of the Atlas site, and clicking the "Add New Database User" button.
The MongoDB connection string for a local server would typically take the form of
<database> is the name of the database you want to connect to. If you are using a different port for MongoDB, you should replace
27017 with the port number you are using.
To get the connection string for a self-hosted MongoDB replica set, you will need to know the hostnames or IP addresses of the servers in the replica set, as well as the name of the replica set.
You can specify the individual servers in the replica set by including them in the connection string, like this:
If we had a replica set called
repl1, consisting of three hosts,
192.168.10.1-3, and we were running them all on the default port,
27017, our connection string would look like this:
An SRV connection string is a type of connection that starts with the prefix
mongodb+srv:// (instead of just
mongodb://). All MongoDB Atlas connection strings are SRV connection strings — it makes them shorter and easier to use. The SRV protocol allows for automatic discovery of the servers in a MongoDB replica set or sharded cluster, without the need to specify the individual server addresses.
The technical details of exactly how SRV connection strings work is a little out of scope. Fortunately, we’ve got a blog post — Here to SRV You with Easier Replica Set Connections — if you want to learn all about them.