MongoDB gets an upper edge over MySQL while handling large unstructured data.
Also, it is very easy to install and set up the MongoDB on the server. It allows database connection using tools like MongoDB Compass, Mongo, or using APIs.
However, the improper installation of client and problems with URI string may lead to MongoDB not connecting error.
At Bobcares, we often get requests to fix MongoDB not connecting error as part of our Server Management Services.
Today, we’ll see how our Support Engineers connect MongoDB via different methods and fix related errors.
Methods for connecting to MongoDB
Applications handling a large amount of data usually make use of MongoDB for better performance. And, for the proper working, website owners need to connect to MongoDB frequently. Therefore, it’s really critical to have a proper method of MongoDB connection.
Fortunately, MongoDB support many methods of connection. Let’s check on the popular methods to connect to MongoDB.
1. MongoDB Compass
MongoDB Compass is the graphical user interface for MongoDB. Also, it is available on Linux, Mac, or Windows. MongoDB Compass uses a simple point and click interface to create and modify rules that validate your data.
In addition, we can easily identify a bottleneck or slow query that could cause performance problems. Therefore, we can get a faster solution for issues by identifying the data behavior via MongoDB Compass.
2. Mongo interactive shell
One of the quick methods for connecting MongoDB is Mongo. It has a JavaScript shell interface which is used to manage MongoDB. Also, the interface comes handy to system administrators as well as developers. They can test queries and operations directly with the database.
An example for Mongo shell with a connection string is given below. This describes host, domain name and port.
mongo "mongodb://mongodb.example.com:27017/testdb?ssl=true"
We strongly recommend using TLS 1.1+ encryption with mongo. Because Mongo doesn’t support for TLS 1.0 encryption on systems.
3. Using APIs
Most app owners prefer to use an application programming interface to make their work easy.
Fortunately, MongoDB allows connecting using programming API. It supports a vast number of drivers based on common program language like C, Python, PHP, Java, etc.
In addition, Mongoid supported Object-Document-Mapper framework for MongoDB in Ruby.
How we fixed MongoDB not connecting error
From our experience in managing servers, we’ve seen customers facing problems while connecting to MongoDB. Let’s take a closer look at how our Support Engineers fixed these problems.
1. Mongod service down
Recently, one of our customers contacted us with a MongoDB not connecting issue. When he tried to connect to MongoDB resulted in an error. The following error was what he got.
2019-04-25T07:31:28.528+0000 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: errno:111 Connection refused
2019-04-25T07:31:28.530+0000 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:229:14
@(connect):1:6
exception: connect failed
Firstly, our Support Engineers checked the status of the Mongod service and saw that it was not running.
service MongoDB status
mongodb.service - High-performance, schema-free document-oriented database
Loaded: loaded (/etc/systemd/system/mongodb.service; disabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Tue 2019-04-25 07:45:32 UTC; 1min 8s ago
Process: 1721 ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf (code=exited, status=14)
Main PID: 1721 (code=exited, status=14)
From the above log, we found that MongoDB not connecting error happened due to the Mongod service down.
Therefore, we started and stopped the Mongod service by using the following command.
service mongod restart
And, that fixed the error.
2. Firewall blocks connection
Sometimes, website owners may face error like “MongoDB connection error: MongoError: failed to connect to server [localhost:27017]”. This error occurs when a firewall blocks the connection from an IP address.
In order to solve this error, our Support Engineers allow IP address in the firewall configuration file based on the client request. It may vary from server to server depending on the firewall installed on the server.
3. Authentication Failure
Similarly, authentication failure also can be a reason for MongoDB connection errors.
If the connection string password contains special characters, it must encode those characters. Otherwise, MongoDB not connecting error occurs due to authentication Failure. Also, the special character needs conversion into corresponding Hex value by using ASCII Codes Table.
In such scenario, our Support Engineers help our customers to generate connection string password based on their needs.
[Getting MongoDB not connecting error? We’ll fix it for you.]
Conclusion
In short, MongoDB not connecting error happens due to failed MongoDB server, firewall restrictions, auth failure, etc. Today, we saw various methods for connecting to MongoDB and how our Support Engineers fixed MongoDB not connecting errors.
Unfortunately, when trying to connect with Compass to a MongoDB instance running in a Docker container the error message I get is “connection to 127.0.0.1:xxxx closed”. This error message does not tell me anything. Also the MongoDB log ist not vey clear on what exactly is going wrong here. How can I debug such a situation?
Cheers
Michael
Hi Michael,
Please contact our support team via live chat(click on the icon at right-bottom)