Bobcares

Smart ways to fix MongoDB not connecting error

by | Jun 8, 2019

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.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

2 Comments

  1. Michael Perlbach

    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

    Reply
    • Hiba Razak

      Hi Michael,
      Please contact our support team via live chat(click on the icon at right-bottom)

      Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF