MongoDB Connection Errors — Authentication Failed and Connection String Issues
About MongoDB Connection Errors
Fix MongoDB connection errors including authentication failures, connection string parsing issues, replica set connectivity, and TLS configuration problems. This guide covers everything you need to know about this topic, including common causes, step-by-step solutions, and answers to frequently asked questions.
Here are the key things to understand: MongoDB uses connection strings in URI format: mongodb://user:pass@host:port/database. Authentication database may differ from the application database (authSource parameter). Replica sets require all members listed in the connection string or SRV DNS records. MongoDB Atlas uses SRV connection strings: mongodb+srv://user:pass@cluster.xxxxx.mongodb.net/. Special characters in passwords must be URL-encoded in the connection string. Understanding these fundamentals will help you diagnose and resolve this issue more effectively.
The most common reasons this occurs include: Authentication database mismatch — user created in 'admin' but connecting to application database. Special characters in password not URL-encoded (@ becomes %40, : becomes %3A). Replica set member unreachable from the application server. MongoDB Atlas IP whitelist not including the application server's IP address. Identifying the root cause is the first step toward finding the right solution.
To resolve this, follow these recommended steps: URL-encode special characters in the password: use encodeURIComponent() in Node.js or equivalent. Add authSource parameter: 'mongodb://user:pass@host:27017/mydb?authSource=admin'. For MongoDB Atlas: whitelist your server IP in Atlas > Network Access > Add IP Address. Test connection from command line: 'mongosh "mongodb://user:pass@host:27017/mydb?authSource=admin"'. Check MongoDB logs: '/var/log/mongodb/mongod.log' for authentication and connection error details. If these steps do not resolve the issue, consider consulting additional resources or a qualified professional.
This article is part of our Linux Error Codes collection on Error Codes Wiki. We provide comprehensive, up-to-date information to help you find solutions quickly.
Quick Answer
What is authSource in MongoDB?
authSource specifies which database contains the user credentials. By default, users are created in the 'admin' database. Add ?authSource=admin to your connection string if your user is in admin but you are connecting to a different database.
Overview
Fix MongoDB connection errors including authentication failures, connection string parsing issues, replica set connectivity, and TLS configuration problems.
Key Details
- MongoDB uses connection strings in URI format: mongodb://user:pass@host:port/database
- Authentication database may differ from the application database (authSource parameter)
- Replica sets require all members listed in the connection string or SRV DNS records
- MongoDB Atlas uses SRV connection strings: mongodb+srv://user:pass@cluster.xxxxx.mongodb.net/
- Special characters in passwords must be URL-encoded in the connection string
Common Causes
- Authentication database mismatch — user created in 'admin' but connecting to application database
- Special characters in password not URL-encoded (@ becomes %40, : becomes %3A)
- Replica set member unreachable from the application server
- MongoDB Atlas IP whitelist not including the application server's IP address
Steps
- 1URL-encode special characters in the password: use encodeURIComponent() in Node.js or equivalent
- 2Add authSource parameter: 'mongodb://user:pass@host:27017/mydb?authSource=admin'
- 3For MongoDB Atlas: whitelist your server IP in Atlas > Network Access > Add IP Address
- 4Test connection from command line: 'mongosh "mongodb://user:pass@host:27017/mydb?authSource=admin"'
- 5Check MongoDB logs: '/var/log/mongodb/mongod.log' for authentication and connection error details