Mongo network error

In this tutorial, we are going to resolve this error “MongoDB failed to connect to server" and we will also see the root cause of the issue.

There are a lot of people getting errors while connecting to the server. So, In this MongoDB tutorial, We are going to resolve this error while “MongoDB failed to connect to server”. We will also see the root cause of the issue.

We will also cover this in different operating systems. These are the following topics that we are going to cover in this tutorial:

  • MongoDB failed to connect to server
  • MongoDB compass failed to connect
  • MongoDB failed to connect to 127.0.0.1(localhost)
  • MongoDB failed to connect to bus host is down
  • MongoDB failed to connect to localhost 27017 ubuntu
  • MongoDB failed to connect to localhost 27017 mac

This problem occurs when you try to connect to your MongoDB server and you are failed to connect to your MongoDB server.

MongoDB failed to connect to server
MongoDB failed to connect to the server

As you can see, when we try to connect to the server we got the error “couldn’t connect to the server”. To resolve this error you have to start the server so you can easily connect to the server.

Follow the following steps so you can start the server and connect it:

  • Open the services folder in your system and search MongoDB server (MongoDB).
  • Here, services are the program that run in the background without a user interface and enable system features. A service is software that performs automated tasks, responds to hardware events, or listens for data requests from other software.
MongoDB failed to connect to server in windows
MongoDB failed to connect to the server
  • See, MongoDB server is not in running state that why we are failed to connect to the server.
  • Now to start the MongoDB server, you have to click on Start button on right side this will start you MongoDB server.
MongoDB connect to server
MongoDB connect to the server
  • After starting the MongoDB services, connect to MongoDB server.
  • Open command prompt and write command mongo in the shell.
Start MongoDB Server
Start MongoDB Server
  • Now, we successfully connect to the MongoDB server.

This way you can resolve the error when your system failed to connect to the server.

Read: MongoDB drop collection

MongoDB compass failed to connect

This problem occurs when the compass attempted to connect to your database on the specified host and port.

The error occurs when:

  • When you provide no hostname or invalid hostname to the compass connect
  • Incorrect port
  • MongoDB server has been shut down or server hostname has changed
  • firewall actively blocking connection to your local network

Solution:

Now, we discuss some of the solutions and the solution may depend on how your MongoDB environment is configured.

These are some of the solutions:

  1. Ensure that your MongoDB instance is running: Compass must connect to a running MongoDB instance. Also check you have installed MongoDB and have a running mongod process. You should also check that the port where MongoDB is running matches the port you provide in the compass connect.
  2. Hostname and Port: Varify that you have specified your Hostname and Port correctly in the compass conect.
  3. MongoDB shutdown or server moved: Sometimes it is possible that MongoDB server has shutdown or host name has changed. To resolve this update the hostname or if server is shutdown start the server.
  4. Firewall: If you believe that your firewall may be blocking your connection to the port, try to connect from a different loaction.

Read: How to store images in MongoDB

MongoDB failed to connect to 127.0.0.1 (localhost)

The MongoDB failed to connect error “Error: couldn’t connect to the server 127.0.0.1:27017” is a general error that means your client/server cannot connect to a server on the specified IP and port. Here, 127.0.0.1(localhost) is the hostname and 27017 is the port number.

There could be any reason for this error:

  1. Firewall or network configuration blocked you to connect to the server.
  2. The MongoDB server is not listening the IP or port.

Now, you have to analyse that what could be the reason behind the error and then solve the error:

  • If the problem occurs due to the firewall or network configuration then you have to stop it and then restart the server so you can easily connect to the MongoDB server.

How to stop firewall?

You need to follow the below steps to stop the firewall services:

  • Go to system settings and click on update & security.
  • Click on Windows Security right side and click on Firewall & network protection.
MongoDB failed to connect to 127.0.0.1 stop firewall
Stop the firewall services
  • Now, you can see different network settings and you can turn off all the them or either which want to turn off.
MongoDB failed to connect to 127.0.0.1 turn off firewall
Stop the firewall services

This way you can stop the firewall services.

Now start the MongoDB server, this time you can easily start it without any error. Go to the command prompt write command mongo and this will start the server.

MongoDB failed to connect to 127.0.0.1
Start MongoDB server

Here, you successfully connected to the server(127.0.0.1:27017).

  • If the error occurs due to a port issue or the port is already occupied by some other services then you can change the port and start the MongoDB server on another port. Use below command to start the server on another port
C:Program FilesMongoDBServer5.0bin>mongo --port 4000

The default port of MongoDB is 27017, 27018, and 27019. If these ports are already used by some other services then you change the port.

MongoDB failed to connect port
MongoDB run server on another port

After changing the port you successfully connected to 127.0.0.1:4000.

In this topic, you have learned “stop to the firewall and change the port of MongoDB” and after that, you successfully connect to 127.0.0.1

Also Read: Import JSON and insert JSON into MongoDB

MongoDB failed to connect to bus host is down

In ubuntu, when I was connecting to the MongoDB server and checking the MongoDB server-status then my system couldn’t find the mongod.service and failed to connect due to the bus host is down.

So to fix this error, you have to execute the below command

sudo systemctl enable mongodb

This command will help you to restore all the services. After this, you have to copy the correct config to /etc/mongod.conf and execute the below command.

sudo service mongodb restart

This command will restart your MongoDB server. After this check the status of MongoDB.

sudo service mongodb status
MongoDB failed to connect to bus host is down
Restart MongoDB server and check the status

Now, you can see after restarting the service, MongoDB status is active(running). This way you resolve the error and connect to the MongoDB server.

Read: How to check if MongoDB is installed + MongoDB Version

MongoDB failed to connect to localhost 27017 ubuntu

When I tried to connect to the MongoDB server, received the error “MongoDB failed to connect“. This error may occur when the MongoDB server is not running or inactive.

MongoDB failed to connect to localhost
MongoDB failed to connect to localhost

Let’s get into this, Check the MongoDB server is running fine or not. In my case error came due to the MongoDB server was down.

To resolve this error, you have to follow some of the following steps:

  • Verify the MongoDB server status
sudo service mongodb status

This command will show you the status of the MongoDB database like it is active or inactive.

MongoDB failed to connect in ubuntu
MongoDB failed to connect in ubuntu

As you can see in the output it is inactive(dead), That is the reason behind the server was not running.

  • Now, start the MongoDB server by using command
sudo service mongodb start

This command will start the MongoDB server.

  • Again, verify the MongoDB server status
sudo service mongodb status
MongoDB failed to connect to localhost 27017 ubuntu
Start the MongoDB services

This command will help you to start the MongoDB server.

  • Now, type mongo in the shell and this will start MongoDB server
Start MongoDB Server in ubuntu
Start MongoDB Server in ubuntu

This way you can easily start the server and resolve the problem when your system failed to connect to the MongoDB server.

Read: Create tables in MongoDB

MongoDB failed to connect to localhost 27017 mac

When I start the MongoDB server in mac os then this error occurred “failed to connect to localhost”. This error occurs when the MongoDB service is not running on the mac.

To resolve this error, you have to start the MongoDB service using the below command:

brew services start mongodb

This command will start the MongoDB services and after this, you have to simply write in the shell mongod command.

mongod

This command will start the mongo daemon.

mongo

Now, again open the shell and write the mongo command. This will start the MongoDB interactive client. Now, you use the MongoDB database.

This way you can resolve the error in mac os and connect to the MongoDB server.

You may also like reading the following articles.

  • MongoDB sort by field
  • MongoDB sort by date
  • How does MongoDB create collection
  • Pros and cons of MongoDB
  • Unable to locate package mongodb-org
  • MongoDB is not recognized as an internal or external command

In this tutorial, we have learned “How to resolve the error when MongoDB failed to connect” in different operating systems. These are the following topics that we covered in this tutorial

  • MongoDB failed to connect to server
  • MongoDB compass failed to connect
  • MongoDB failed to connect to 127.0.0.1(localhost)
  • MongoDB failed to connect to bus host is down
  • MongoDB failed to connect to localhost 27017 ubuntu
  • MongoDB failed to connect to localhost 27017 mac

Bijay

I am Bijay having more than 15 years of experience in the Software Industry. During this time, I have worked on MariaDB and used it in a lot of projects. Most of our readers are from the United States, Canada, United Kingdom, Australia, New Zealand, etc.

Want to learn MariaDB? Check out all the articles and tutorials that I wrote on MariaDB. Also, I am a Microsoft MVP.

Using the official mongo image from the docker hub, I run the following command to get it running:

docker run --name api -p 127.0.0.1:27017:27017 -p 127.0.0.1:28017:28017 -d mongo

Then from another terminal shell, I run mongo

However, I keep getting the following error:

MongoDB shell version: 3.2.3
connecting to: test
2016-02-18T13:52:08.110-0700 I NETWORK  [thread1] Socket recv() errno:104 Connection reset by peer 127.0.0.1:27017
2016-02-18T13:52:08.110-0700 I NETWORK  [thread1] SocketException: remote: (NONE):0 error: 9001 socket exception [RECV_ERROR] server [127.0.0.1:27017] 
2016-02-18T13:52:08.110-0700 E QUERY    [thread1] Error: network error while attempting to run command 'isMaster' on host '127.0.0.1:27017'  :
connect@src/mongo/shell/mongo.js:224:14
@(connect):1:6

exception: connect failed

I checked the logs for the container, and everything seemed right:

2016-02-18T20:51:20.538+0000 I CONTROL  [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=73334e5089e1
2016-02-18T20:51:20.538+0000 I CONTROL  [initandlisten] db version v3.2.3
2016-02-18T20:51:20.538+0000 I CONTROL  [initandlisten] git version: b326ba837cf6f49d65c2f85e1b70f6f31ece7937
2016-02-18T20:51:20.538+0000 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1e 11 Feb 2013
2016-02-18T20:51:20.538+0000 I CONTROL  [initandlisten] allocator: tcmalloc
2016-02-18T20:51:20.538+0000 I CONTROL  [initandlisten] modules: none
2016-02-18T20:51:20.538+0000 I CONTROL  [initandlisten] build environment:
2016-02-18T20:51:20.538+0000 I CONTROL  [initandlisten]     distmod: debian71
2016-02-18T20:51:20.538+0000 I CONTROL  [initandlisten]     distarch: x86_64
2016-02-18T20:51:20.538+0000 I CONTROL  [initandlisten]     target_arch: x86_64
2016-02-18T20:51:20.538+0000 I CONTROL  [initandlisten] options: {}
2016-02-18T20:51:20.542+0000 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=8G,session_max=20000,eviction=(threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2016-02-18T20:51:20.615+0000 I CONTROL  [initandlisten] 
2016-02-18T20:51:20.615+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2016-02-18T20:51:20.615+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-02-18T20:51:20.615+0000 I CONTROL  [initandlisten] 
2016-02-18T20:51:20.615+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-02-18T20:51:20.615+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-02-18T20:51:20.615+0000 I CONTROL  [initandlisten] 
2016-02-18T20:51:20.616+0000 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2016-02-18T20:51:20.616+0000 I NETWORK  [HostnameCanonicalizationWorker] Starting hostname canonicalization worker
2016-02-18T20:51:20.633+0000 I NETWORK  [initandlisten] waiting for connections on port 27017

What’s wrong with docker or Mongo? I’ve searched everywhere for an answer, and nothing has worked so far. Is docker or Mongo broken?

Edit:
I did a little more investigation, and I found that I cannot access the internet from inside the container. I connect to it by running docker exec -ti api /bin/bash and then try to ping google or something, and the command just hangs.

I can only seem to get an internet connection from the container to the outside world by using --net=host in the docker run command.

I have a project which includes a docker setup that I’m trying to get working.
My Dockerfile is:

    FROM node:8-alpine
    
    EXPOSE 3000
    
    ARG NODE_ENV
    ENV NODE_ENV $NODE_ENV
    
    RUN mkdir /app
    WORKDIR /app
    ADD package.json yarn.lock seed.js /app/
    RUN yarn --pure-lockfile
    ADD . /app
    
    CMD ["yarn seed"]
    CMD ["yarn", "docker:start"]

and my docker-compose:

version: "2"
 services:
   isawyou-server:
     build: .
     environment:
       - MONGO_URI=mongodb://mongodb:27017/isawyou
     volumes:
       - .:/app
     ports:
       - "3000:3000"
     depends_on:
       - mongodb
 
   mongodb:
     image: mongo
     ports:
       - "27017:27017"


Now, for the docker-compose.test I am trying to set it up according to the instructions of https://docs.docker.com/docker-cloud/builds/automated-testing/ . I basically want to have a different database for testing. I tried different configs but none works. The error I get is:

   directory '/data/db/diagnostic.data'
   mongodb_1         | 2017-11-21T21:29:27.518+0000 I NETWORK  [thread1] waiting for connections on port 27017
   mongodb_1         | 2017-11-21T21:29:33.500+0000 I NETWORK  [thread1] connection accepted from 172.18.0.4:39460 #1 (1 connection now open)
   mongodb_1         | 2017-11-21T21:29:33.509+0000 I NETWORK  [conn1] received client metadata from 172.18.0.4:39460 conn1: { driver: { name: "nodejs", version: "2.2.33" }, os: { type: "Linux", name: "linux", architecture: "x64", version: "4.9.49-moby" }, platform: "Node.js v8.9.1, LE, mongodb-core: 2.1.17" }
   mongodb_1         | 2017-11-21T21:29:33.527+0000 I NETWORK  [thread1] connection accepted from 172.18.0.4:39462 #2 (2 connections now open)
   mongodb_1         | 2017-11-21T21:29:33.548+0000 I INDEX    [conn1] build index on: isawyou.users properties: { v: 2, unique: true, key: { email: 1 }, name: "email_1", ns: "isawyou.users", background: true }
   mongodb_1         | 2017-11-21T21:29:33.568+0000 I INDEX    [conn2] build index on: isawyou.refreshtokens properties: { v: 2, key: { token: 1 }, name: "token_1", ns: "isawyou.refreshtokens", background: true }
   mongodb_1         | 2017-11-21T21:29:33.568+0000 I INDEX    [conn2] build index done.  scanned 0 total records. 0 secs
   mongodb_1         | 2017-11-21T21:29:33.568+0000 I INDEX    [conn1] build index done.  scanned 0 total records. 0 secs
   isawyou-server_1  | yarn run v1.3.2
   isawyou-server_1  | $ cross-env NODE_ENV=production pm2-docker start ./src/index.js
   isawyou-server_1  | 0|index    | server started on port 3000 (production)
   isawyou-server_1  | 0|index    | ::ffff:172.18.0.1 - - [21/Nov/2017:21:32:26 +0000] "GET / HTTP/1.1" 404 60 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36"
   sut_1             | yarn run v1.3.2
   sut_1             | $ cross-env NODE_ENV=test nyc --reporter=html --reporter=text mocha --timeout 20000 --recursive src/tests
   sut_1             |
   sut_1             |
   sut_1             | server started on port 3000 (test)
   sut_1             |   Authentication API
   sut_1             | MongoDB connection error: MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]

my docker-compose.test currently looks like:

    version: "2"
    services:
      sut:
        build: .
        command: yarn test

Понравилась статья? Поделить с друзьями:
  • Mondial forni коды ошибок
  • Monast error module twisted not found
  • Mom exe ошибка инициализации платформы net framework как исправить
  • Mom exe net framework initialization error что делать
  • Mohw exe системная ошибка msvcp100 dll windows 10