Error : couldn't connect to server 127.0.0.1:27017, connection attempt failed : Socket Exception

Hello Dear Community,

I am facing this error :

MongoDB shell version v4.4.14
connecting to : mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Error: couldn’t connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:374:17
@(connect):2:6
exception: connect failed
exiting with code 1

I am on Ubuntu 16.04 and I am basically trying to encrypt data from client to server using TLS/SSL. So, I created a CA file and I am trying to connect to mongodb using that certificate with this command :

mongo --tls --tlsCAFile rootCA.pem --host 127.0.0.1

However even the command “mongo” gives me this error. I am new to Linux so I do not know how to verify that mongodb is on the right IP and port could you please show step by step ?

Is your mongod up and running on port 27017?
ps -ef|grep mongo or check from service status depending on your install method
Is it running as service or manually started from command line?

Hello, thank you for your response : when I run “ps -ef|grep mongo” I get this output :

Capture

And I am sorry but I don’t know weather mongod is running as a service but I installed it manually.

Your mongod is up & running
Please show contents of your config file

This is the content of my mongod.conf file :

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: /var/lib/mongodb
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1
  tls :
       mode : requireTLS
       certificateKeyFile : /home/youssef/mongodb.pem
       CAFile : /home/youssef/rootCA.pem

# how the process runs
processManagement:
  timeZoneInfo: /usr/share/zoneinfo

#security:

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:

Thank you for your time

It is running on port 1687
So connect by
mongo --port 1687

No sorry I just changed the port to try out some tests. the actual port is 27017. And even when I specify port 1687 it gives me the same error

In this post : Error: couldn't connect to server 127.0.0.1:27017 - #4 by Stephen_Fuller it says I need to create some directories because me too when I run the command : " netstat -an | grep 27017` " I don’t get any output. But I don’t know what I have to do since it is not stated clearly in the other post

Check your mongod.log for more errors
Cd to your dbpath dir if it exists no need to create first
The command i gave is to connect assuming your mongod is running with no acces control
Since you say your mongod runs on port 27017 please check step by step first whether you can connect without security parms enabled
Shutdown your mongod edit config file and comment the security params.Start mongod and try to connect issuing just mongo
If you can connect then add back tls and other params and investigate why it is not working

This is the actual error I am getting from the logs :

“msg”:“Error receiving request from client. Ending connection from remote”,“attr”:{“error”:{“code”:141,“codeName”:“SSLHandshakeFailed”,“errmsg”:“SSL handshake received but server is started without SSL support”},“remote”:“127.0.0.1:34766”,“connectionId”:4}}

But how do I start the server with ssl support ?

See https://www.mongodb.com/docs/manual/reference/configuration-options/#net-options.

If you are planning to manage your own server, I strongly recommend you take M103: Basic Cluster Administration | MongoDB University. Question like this one are covered.

I already took that course and although it mentions TLS encryption it doesn’t describe how to implement it :confused: