MongoDB exception: connect failed

When tried to connect to MongoDB server, recieved the error: “MongoDB exception: connect failed”. This error occurs if MongoDB server is offline or It may be connecting with default port 27017.

MongoDB shell version v4.0.10 connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb 2019-07-09T22:18:34.660-0700 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:344:17 @(connect):2:6 exception: connect failed

Reason:

Possible reason check the MongoDB server is up and running fine or not. In my case this error came because MongoDB server is down.

MongoDB exception connect failed

Solution 1:

1. Lets verify the MongoDB server status:

service mongod status

● mongod.service - MongoDB Database Server Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: Active: inactive (dead) Docs: https://docs.mongodb.org/manual Jul 09 22:23:32 ubuntu systemd[1]: /lib/systemd/system/mongod.service:10: PIDFil Jul 09 22:24:44 ubuntu systemd[1]: /lib/systemd/system/mongod.service:10: PIDFil

MongoDB exception connect failed

2. Now, start the MongoDB server by using the command:

sudo service mongod start

Then, verify the status:

service mongod status

3. Now connect to MongoDB server by typing mongo in the shell.

root@ubuntu:/home/r2schools# mongo MongoDB shell version v4.0.10 connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb Implicit session: session { "id" : UUID("40c7ccd1-a431-478e-ad16-1a251a26fa08") } MongoDB server version: 4.0.10 Server has startup warnings: 2019-07-09T22:33:14.894-0700 I STORAGE [initandlisten] 2019-07-09T22:33:14.894-0700 I STORAGE [initandlisten] ** WARNING: Using the XF S filesystem is strongly recommended with the WiredTiger storage engine 2019-07-09T22:33:14.894-0700 I STORAGE [initandlisten] ** See http://d ochub.mongodb.org/core/prodnotes-filesystem 2019-07-09T22:33:26.357-0700 I CONTROL [initandlisten] 2019-07-09T22:33:26.358-0700 I CONTROL [initandlisten] ** WARNING: Access contr ol is not enabled for the database. 2019-07-09T22:33:26.358-0700 I CONTROL [initandlisten] ** Read and wri te access to data and configuration is unrestricted. 2019-07-09T22:33:26.358-0700 I CONTROL [initandlisten] --- Enable MongoDB's free cloud-based monitoring service, which will then receive an d display metrics about your deployment (disk utilization, CPU, operation statistics, etc) . The monitoring data will be available on a MongoDB website with a unique URL acc essible to you and anyone you share the URL with. MongoDB may use this information to make prod uct improvements and to suggest MongoDB products and deployment options to you. To enable free monitoring, run the following command: db.enableFreeMonitoring() To permanently disable this reminder, run the following command: db.disableFreeM onitoring() --- >

Solution 2:

Check port mongod running and connect with that port.

mongo --port port_number

For the same video tutorial:

Leave a Reply

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