No connection could be made because the target machine actively refused it (despite mongod running) and unable to insert data into MongoDB

9,628 views
Skip to first unread message

Arjun S

unread,
Jan 21, 2016, 9:47:21 AM1/21/16
to mongodb-user
Hello everyone,

I have been trying to insert data (a 'hello world' string) from PLC into MongoDB using Python API (which pulls the data from PLC and pushes it into MongoDB). I have been getting the error message 'line 222, in meth  return getattr(self._sock,name)(*args) error: [Errno 10061] No connection could be made because the target machine actively refused it' despite having mongod running in the Services background for the code I have written below. Also, the server IP address on which MongoDB is present is 10.52.124.186 and address of PLC (which I am using it on my PC) is 10.52.124.135. I am have tried almost everything to sort it out and yet I haven't got a clue as to how to get past it. Where am I going wrong?



 
#!/usr/bin/python          

import socket
import socket
import pymongo
from pymongo import MongoClient
import datetime

# Connection to server (PLC) on port 27017
server = socket.socket()         
host = '10.52.124.135' 
port = 27017

server.connect((host, port))
print server.recv(1024)

server.close 

#Connection to Client (Mongodb) on port 27017
IP = '10.52.124.186'
PORT = 27017
BUFFER_SIZE = 1024

client = MongoClient('10.52.124.186', 27017)
db = client.RXMMongoDB

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((IP, PORT))
s.listen(1)

#connections loop
while True:
conn, addr = s.accept()
print 'Connection address:',addr
try:
# read loop
while True:
data = server.recv(BUFFER_SIZE)

if not data: 
break
# send to Mongo
mongodoc = { "data": data, "date" : datetime.datetime.utcnow() }
db.AAAA.insert(mongodoc)
finally:
conn.close()                      

A

unread,
Jan 21, 2016, 10:21:27 AM1/21/16
to mongodb-user
Check if the port is open
If i block my port via firewall, I get the same error

Arjun S

unread,
Jan 21, 2016, 10:33:48 AM1/21/16
to mongodb-user
I have disabled the firewall as well as opened the 27017 port. It still shows the same error. 

Rhys Campbell

unread,
Jan 21, 2016, 10:44:52 AM1/21/16
to mongodb-user
Check which ip(s) you are binding to in mongodb...


Changed in version 2.6.0: The deb and rpm packages include a default configuration file (/etc/mongod.conf) that sets net.bindIp to 127.0.0.1.



Arjun S

unread,
Jan 21, 2016, 11:40:53 AM1/21/16
to mongodb-user
But I don't have a /etc folder which contains a mongod.conf file. The MongoDB version I have installed is 2.6.4 Standard. 

Rhys Campbell

unread,
Jan 22, 2016, 4:11:37 AM1/22/16
to mongodb-user
You'll need to find it. Something like...

ps -ef | grep mongod

Will show you your running monod processes and the config file used.

Arjun S

unread,
Jan 22, 2016, 4:35:47 AM1/22/16
to mongodb-user
Is there a similar command for Windows? I don't have Linux running on my system. 

Rhys Campbell

unread,
Jan 22, 2016, 5:01:51 AM1/22/16
to mongodb-user
For powershell you have the Get-Process cmdlet. I don't have a running windows system for this.


Your config file is possibly C:\mongodb\mongod.cfg.


varsh...@gmail.com

unread,
Jul 8, 2017, 8:27:03 PM7/8/17
to mongodb-user
Hello Everyone,

I am trying to pull out the data from MongoDB using Python.

I am using RoboMongo as its Client and pymongo API.
I am getting the same error as ServerSelectionTimeoutError: localhost:27017: [Errno 10061] No connection could be made because the target machine actively refused it.

Can someone tell me how to get rid of this error.

Kevin Adistambha

unread,
Jul 17, 2017, 1:44:39 AM7/17/17
to mongodb-user
Hi,

>[Errno 10061] No connection could be made because the target machine actively refused it.

The error means that your connection attempt to the specified server did not succeed. Typical causes could be:
1. Wrong server address/port. Are you connected to the right server using the right port number?
2. The `mongod` or `mongos` process was not running in the destination server.
3. Firewall protecting the target server was not configured to let connection through on the specified port.

The actual cause of the error depends on your environment and your network setup, and the error was not caused by MongoDB. I would encourage you to check the three points above and see if any of them is not correctly setup.

Also, in the future please create a new thread with a detailed description of your issue. This is because even though the error message may look similar at a glance, the actual cause could be entirely different.

Best regards,
Kevin
Reply all
Reply to author
Forward
0 new messages