Error: couldn’t connect to server 127.0.0.1:27017

Everything works fine when I used shell v3.6. After updating to v4.2.3 I got this error. Tried several commands from stackoverflow but no luck. I am using linux mint 19.3 (based on Ubuntu 18.04)

MongoDB shell version v4.2.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
2020-03-05T00:30:14.956+0600 E QUERY [js] 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:341:17
@(connect):2:6
2020-03-05T00:30:14.958+0600 F - [main] exception: connect failed
2020-03-05T00:30:14.958+0600 E - [main] exiting with code 1

Output of ss -tlnp please.

@ShawonSaha Welcome to the forum!

This error message indicates the mongo shell is unable to connect to a MongoDB server on the requested IP & port.

How did you upgrade from MongoDB 3.6 to 4.2? If you have existing data, the normal upgrade path between major releases involves upgrading through adjacent major releases (eg 3.6 to 4.0, then 4.0 to 4.2). The final step of each major release upgrade is to explicitly set a Feature Compatibility Version to enable persisting backwards-incompatible data changes.

The MongoDB documentation includes specific instructions for upgrading between versions depending on your deployment type (standalone, replica set, or sharded cluster).

For example, for a standalone server see:

If you have skipped over 4.0 and gone directly from running 3.6 binaries to 4.2, it is likely that your MongoDB server instance will be unable to start without manual intervention.

You can check the MongoDB logs for more info. If you have installed the official MongoDB server package for Ubuntu, the default log location will be /var/log/mongodb/mongod.log. If the MongoDB process isn’t able to start (or unexpectedly shuts down) the last lines in the log file should provide some context.

If you’re not sure how to interpret the log information, please post the relevant lines here. I would start by reviewing the expected upgrade procedures to make sure you have not skipped any essential steps.

Regards,
Stennie

@StennieThanks for your response. Luckily the problem is fixed automatically by itself. Last night I’ve gone back and forth v3.6 to 4.2 several times. When I run mongo on 3.6 it works perfectly fine but an error occurred whenever I update it to 4.2.

I lose hope and started using v3.6 until I saw your reply. So, I uninstall current version and update it to v4.2 again for the mongod.log file. After the update, I still got this error. So I go to /var/log folder and found it empty. So I reboot my laptop and run the mongo command to generate some log into the log file. Surprisingly after restart when I hit mongo in the terminal everything working as expected. I don’t have any clue what just happened. How does it fix! Note that, I have restarted my laptop several times before when I got this issue last night without any luck. I didnt have the log file when the error occoured because I have switched to 3.6. Here is last few lines of log file after error was fixed.

2020-03-05T20:10:30.284+0600 I  SHARDING [initandlisten] Marking collection local.startup_log as collection version: <unsharded>

2020-03-05T20:10:30.284+0600 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory ‘/var/lib/mongodb/diagnostic.data’
2020-03-05T20:10:30.289+0600 I SHARDING [LogicalSessionCacheRefresh] Marking collection config.system.sessions as collection version:
2020-03-05T20:10:30.290+0600 I SHARDING [LogicalSessionCacheReap] Marking collection config.transactions as collection version:
2020-03-05T20:10:30.290+0600 I NETWORK [listener] Listening on /tmp/mongodb-27017.sock
2020-03-05T20:10:30.290+0600 I NETWORK [listener] Listening on 127.0.0.1
2020-03-05T20:10:30.290+0600 I NETWORK [listener] waiting for connections on port 27017
2020-03-05T20:10:31.003+0600 I SHARDING [ftdc] Marking collection local.oplog.rs as collection version: