From the course: MongoDB Python Developer Associate Cert Prep
Troubleshooting a MongoDB connection in Python applications
From the course: MongoDB Python Developer Associate Cert Prep
Troubleshooting a MongoDB connection in Python applications
- Hello. In this video you'll learn how to troubleshoot common connection issues when using the PyMongo 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 issues 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 result from systems put in place to protect your database, like IP access restrictions. MongoDB has a lot of resources to help you understand and address these issues. Let's work through a few examples of connection issues that you may experience. We'll demo these issues by using a simple Python app. We'll open the application in VS Code. If you're using a different IDE or text editor, the experience might be slightly different, but the code will be the same. We have a virtual environment set up and activated. In the connection.py file we have the rest of the code for this simple app. The application creates a Mongo client instance by using the connection string which we load in from the dotenv file. Once connected to Atlas, the application prints the list of the databases on the cluster. Let's run the app and see what happens. It seems to be taking a while to connect. We've received an error message. This message tells us that a server selection timeout error has been raised, and the connection was closed. A possible reason for this error 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 log into Atlas and select the Network Access tab from the Atlas dashboard. 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 the buttons. Add Current IP Address, or Allow Access From Anywhere. Using these options, we can add our current IP address or open our cluster to access from any IP address. If we decide to allow connections from anywhere, we need to be aware that this poses a security risk. In our case, we'll 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've updated our network access, let's run our app again. First, we'll clear the terminal, then we run the app. Now we get an error that says, operation failure. Authentication failed. What went wrong here? Let's check the .env file. 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. 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 clear the terminal and run the app again. Great. We've successfully connected without any errors, and the database names have been printed to the terminal. Nice work. Now, imagine that we provided the wrong database username in the connection string, or no username at all. This would result in the same authentication error message. To simulate this error, let's go back into the .env file. We'll change the database user in the connection string from the correct database user, user1, to an incorrect user, user2. Then we'll clear the terminal and run the app again. Notice that we received the same authentication error message as when we used the wrong password for the database user. So be sure to check both your username and password. Let's correct the database username by setting it back to user1. Then we'll clear the terminal and run our app one more time. Nice. We've successfully connected without any errors and the database names have been printed to the terminal. While we didn't cover every type of connection issue in this video, now you have the tools to identify and troubleshoot two of the most common connection errors. To summarize, in this video you learned to fix a network access error by adding your IP address to the IP Access List in the Atlas dashboard, and fix authentication errors caused by an incorrect database username or password. 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.