Lesson 3: Troubleshooting Connection Errors / Learn

Press O for more options
6:16

Video Transcript (English)

Welcome back, everyone.

In this video, you’ll learn how to troubleshoot some common connection errors, including: Network access errors, User authentication errors, and Errors due to too many open connections.

These errors can cause frustration. Luckily, they're easy to diagnose and fix. We’ll cover what causes these errors, how they behave, and how to fix them. Let’s get started.

Most connection issues stem from safeguards put in place by MongoDB to secure your data.

For example, we create user credentials for authentication to protect access to our data.

Let’s see an example.

First, we open the terminal and log in with our Atlas connection string.

We paste in the command that includes the connection string and press Enter.

We enter our password at the prompt.

It seems to be taking awhile to connect.

Now we get an error message telling us that the connection has timed out while trying to connect to the cluster.

This could be an issue with our network, which could take some time to diagnose. But the most common cause of this error is because our IP address doesn’t have network access to the Atlas cluster.

Note that while there are guardrails during cluster creation in Atlas to ensure that users can connect via at least one IP address, it’s possible that the access isn’t sufficient to allow our current IP address to connect.

Let’s confirm if this is the reason for the error. We go to the Atlas dashboard for our cluster and click the Network Access tab.

We see that one IP address can connect to this cluster - but it’s not our address. We need to enable access for our IP address. To do this, we click the “Add IP Address” button.

This is where we can add an IP address to the access list for our cluster. We can type in an IP address, add our current IP address, or open our cluster to any IP address.

Note that allowing connections from anywhere poses a security risk. MongoDB warns against doing this in our documentation.

For our purposes, we’ll add our current IP address.

We have the option to give this IP temporary access. But because this is our cluster, we’ll skip this step.

Now let’s confirm this update. It will take about 30 seconds for the update to be deployed to our cluster.

Our current IP address has been added to the network access list, and now its status is active. Great!

Let’s switch back into our terminal. We’ll try to log in again now that we’ve updated our network access.

Now it appears that we have an authentication error. What went wrong?

One possible cause of this authentication error is typing a password incorrectly.

Note that passwords are case sensitive.

Another possible cause is that the database user specified does not exist in Atlas.

To verify that the user exists, we'll need to check in Atlas. If the user does not exist, we will need to create it.

To start, we'll navigate to the Database Access tab.

We see that only one user exists, the dbaAdmin. The atlasUser that we are attempting to connect with doesn’t exist. We can create a new user by clicking "add a new database user".

We add atlasUser and auto-generate a secure password for it. We can also copy the auto-generated password to store safely so we can use it when we try logging in again.

Next we'll need to assign at least one role or privilege to the role.

In this case, we want to choose a built-in role. We select the "read and write any database" role. We’ll leave the other options in their default settings and click ‘Add user.’

Back at our database access list, atlasUser has been added to our list of users.

Existing users can also be edited from here. For example, we could change the password or modify our new user’s access privileges.

Now the atlasUser has been created, let’s try again to connect to our cluster using mongosh. We enter the new atlasUser password when prompted.

This time we see that the authentication succeeds, and we see the prompt to begin interacting with MongoDB. Nice!

Okay, we've covered common network access and authentication errors. Now, let's talk about the last common error - too many client connections.

Atlas clusters can become saturated with client connections and reach their maximum limits. This causes the server to refuse any new connections until existing connections are closed, triggering the "too many open connections" error.

Note that M0, M2, and M5 clusters can have a maximum of 500 open connections per node.

For a list of maximum connections for each cluster tier, refer to the MongoDB documentation.

There are a few ways to workaround the “too many client connections” error. But before we get into them, note that you should test these conditions in a development or staging environment before going to production so there aren't unexpected impacts on your live application.

To workaround this issue, we scale our cluster to a higher tier to support more concurrent connections.

Or, we restart our application.

And to prevent this issue in the future, consider using the maxPoolSize connection string option to limit the number of connections in the connection pool.

Nice work! Let's recap what you learned:

To fix a user network access error, use the Atlas dashboard to add your IP addresses to the Network Access tab.

To fix an authentication error, make sure to type in your password correctly when prompted. You can also make sure the user exists and has access to the cluster.

Finally, if you get an error because you have too many connections, you have a few options. You can scale your cluster, restart your application, or you can limit the number of connections in the connection pool.

For more details on troubleshooting connection issues, check out the MongoDB documentation.

We didn’t cover every type of error in this video, but this overview should help you identify and troubleshoot a couple of the most common connection issues. Great work, and see you next time!