Video Transcript (English)
Hello! In this video, you will learn how to troubleshoot common connection errors when using the Node.js MongoDB driver. These include Network access errors, and User authentication errors.
Connection issues can cause a lot of frustration for developers.
In this video, we hope to minimize that frustration by giving you a foundation to troubleshoot connection problems with MongoDB.
Before we begin, it’s important to remember that it’s common to experience connectivity issues when learning something new like MongoDB.
Connectivity issues may stem from systems put in place to protect your database. MongoDB has a lot of resources to help you understand and address these issues.
Let’s get started by opening a Node.js console app with the MongoDB driver setup.
We’ll open it in VSCode. If you're using a different IDE or text editor, the experience might be slightly different, but the code will be similar.
This application creates a MongoClient by using a connection string that we acquired from the Atlas dashboard.
Once connected to Atlas, it retrieves a list of the databases on the cluster.
Finally, it logs into the database.
Let’s run the app to see what happens.
Sometimes the error message will appear immediately, but it may also take up to 30 seconds.
It looks like we received an error message.
This message tells us that our connection has failed and that the connection closed.
A possible reason for this is that our IP address does not have network access to the Atlas cluster. By default, your Atlas cluster has NO access to the outside world. You need to enable access in the Atlas UI.
To confirm, we’ll go to the Atlas dashboard for our cluster and click the Network Access tab.
Our IP address isn’t on the IP Access List, so we need to add it.
To do so, click the “Add IP Address” button.
In this window, we can either type in an IP address or use one of these two buttons, “Add Current IP Address” or “Allow Access From Anywhere.”
We can add our current IP address or open our cluster to any IP address.
If you decide to allow connections from anywhere, be aware that this opens you up to security risks.
In our case, let’s add our current IP address.
Now let’s confirm the changes!
The changes take about 30 seconds to deploy to our cluster.
Now that we have updated our network access, let's run our app again.
First clear the timeout error from the previous app execution.
Then, open the application and run it again by typing “node app.js.”
Now it appears that we have an authentication error. What did we do wrong here?
We forgot to update the password in the connection string. MongoDB does not autofill the password portion of your connection string. This ensures that your data is protected at all times.
So, let’s update the password and then try to connect again. We’ll get an authentication error if any part of our connection string is misspelled.
Everything looks correct, so let’s run the app again!
We have successfully connected without any errors, and all of our databases have been logged. Nice work!
Now, imagine that we provided the wrong database user in the connection string, or no user at all. This would result in the same authentication error message.
To simulate this error, we changed the database user in the connection string from the correct database user, “acd37”, to an incorrect one, “acd3”.
Notice that we received the same authentication error message when we used the wrong password for the database user. So, be sure to check both your username and password.
We’ll correct the database username by setting it to acd37. Then we’ll run our app one more time!
Nice! We have successfully connected without any errors, and our databases have been logged.
Awesome job! We didn’t cover every type of connection issue in this video, but you now have the tools to identify and troubleshoot two of the most common connection errors.
To summarize, here’s what you learned how to troubleshoot in this video:
Fix a network access error by adding your IP address to the allowlist in the Atlas dashboard.
Fix authentication errors caused by an incorrect password or user.
Because MongoDB doesn’t auto-populate the password field in the connection string, be sure to check that your password is populated and correct.
Similarly, make sure to include the correct database user in your connection string.