i'm getting following error while connecting to mongodb server - mongodb

$ mongo
MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017
2018-10-18T17:09:03.003+0530 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused
2018-10-18T17:09:03.004+0530 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect#src/mongo/shell/mongo.js:251:13
#(connect):1:6
exception: connect failed

what OS are you using? In ubuntu you need to check your version:
service mongod status, mongod --version.
If mongod is off then just restart and write the error if possible.
service mongod restart

if you are using linux, try this command
$ sudo mongod
you are getting that because you are running
$ mongo

First go to your mongoDB directory and in that directory go to bin directory and open terminal in that directory(bin), then run the following command:
mongod.exe
After that open another terminal in same directory and run the following command.
mongo.exe
It will resolve this issue.

first you check it mongodb proper installed or not and also check it version compatibility
I am saying windows os setup,
set environment variable is path C:\Program Files\MongoDB\Server\3.6\bin;
then open your command prompt and type
mongod
after open another command prompt and type
mongo

A connection refused failure to connect reason may just be a matter of your network configuration and firewall or more specifically, your operating system's port restrictions. While you may be connecting to a mongoDB server on localhost (127.0.0.1) which can't be blocked by a firewall, it is possible for a firewall to block access to specific ports on a localhost.
If you're on a linux-based system, this can be addressed by using iptables as described here with the applicable commands shown below:
iptables -A INPUT -s 127.0.0.1 -p tcp --destination-port 27017 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -d 127.0.0.1 -p tcp --source-port 27017 -m state --state ESTABLISHED -j ACCEPT
However, in reading the comments on another answer, it may be that you aren't able to connect because the server isn't being launched and ran properly due to a lack of of the /data/db directory. But, a presumption based on the lack of reply to other answers suggesting to check your server is running properly, tells me you're sure that it is. In which case, try changing your hostname to localhost instead of 127.0.0.1 when connecting, as in:
mongoose.connect('mongodb://localhost:27017');
# or as more applicable for connecting to a deployment:
mongosh "mongodb://localhost:27017"
# alternatively, by default, MongoDB Shell already connects to localhost
# so you could simply use the command line option --port such as:
mongosh --port 27017
You can also try specifying more parameters in the url as it applies.
For example, connecting to an instance and authenticate against the foobar database as user rohan:
mongosh "mongodb://localhost:27017" --username rohan --authenticationDatabase foobar
To provide a password with the command instead of using the masked prompt, simply add the --password command line option.
For Windows, you may also try the solutions offered on this page.

Related

Error: network error while attempting to run command 'isMaster' on host '127.0.0.1:27017'

.> mongo
MongoDB shell version v3.6.5
connecting to: mongodb://127.0.0.1:27017
2018-06-26T17:37:13.313+0530 I NETWORK [thread1] Socket recv() An established connection was aborted by the software in your host machine. 127.0.0.1:27017
2018-06-26T17:37:13.313+0530 I NETWORK [thread1] SocketException: remote: (NONE):0 error: SocketException socket exception [RECV_ERROR] server [127.0.0.1:27017]
2018-06-26T17:37:13.313+0530 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:251:13
#(connect):1:6
exception: connect failed
This problem can happen when trying to connect to a database which requires SSL.
This error is usually caused by attempting to connect without SSL to a MongoDB server that requires it. Use the following command to connect:
mongo --ssl --username "$USERNAME" --password "$PASSWORD" --host "$HOST" --port "$PORT"
See Mongod logs to find the root cause. For me it was leaking connections from one of the clients, this is what I found in logs:
NETWORK [listener] connection refused because too many open connections:
(SSL causing the issue)
You can connect with it using this command :
mongo --host 127.0.0.1:27017 --tls --username fake --password fakepassword --authenticationDatabase admin --sslAllowInvalidHostnames
Disables the validation of the hostnames in TLS/SSL certificates.
Allows mongo to connect to MongoDB instances even if the hostname in
their certificates do not match the specified hostname.
Deprecated since version 4.2: Use --tlsAllowInvalidHostnames instead.
WARNING :
Although available, avoid using the --sslAllowInvalidCertificates
option if possible. If the use of --sslAllowInvalidCertificates is
necessary, only use the option on systems where intrusion is not
possible.
If mongosh (and other MongoDB Tools) runs with the
--sslAllowInvalidCertificates option, mongosh (and other MongoDB Tools) will not attempt to validate the server certificates. This
creates a vulnerability to expired mongod and mongos certificates as
well as to foreign processes posing as valid mongod or mongos
instances. If you only need to disable the validation of the hostname
in the TLS/SSL certificates, see --sslAllowInvalidHostnames.
See MongoDB official Doc
just go simply on task manager and click on service
see given image below to copy the link on the URL
find MongoDB and click on open Service.
see given image below to copy the link on the url
then again find MongoDB and start stopped DB to running mode just click on green point button and you MongoDB is again started connecting.
[see given image below to copy the link on the url]
In my case , I was running several applications on other ports. The above solutions did not worked well for me .
Finally I closed all the tabs of code editor and web-app , and restarted it . Now it works fine .
Well! You can just restart the system, that will also work with least hassle.
As stated, this can happen because the server requires TLS in the connection. You will see the reason in server logs.
For someone who ends here without knowing how to fix it (like me) it's worth noting how to connect to those servers:
In command line you can use the --tls option (https://docs.mongodb.com/manual/tutorial/configure-ssl-clients/)
mongo --tls [other params and/or connection string]
If using a connection string alone, you can add tls=true param:
mongo://[user]:[pass]#[host]/[db]?tls=true[&others]
I was getting error in the mongod logs that read
WiredTiger metadata corruption detected
To fix the error I did the following:
As per suggestion here I ran command mongod --dbpath /data/db --repair.
The logs from command above complained that my version of mongod was too recent (I was on 4.2.x) and I had to try on mongod 4.2 or earlier.
Downgraded to mongod 4.2
Ran command mongod --dbpath /data/db --repair again, and this fixed issue.
I do run mongod inside Docker, so I had to take an extra step to find out path of the mongo DB file, docker inspect <id of mongod container>, and grab the path from the HostConfig.Binds[0] property of the resulting JSON, and stick that in the --dbpath option of the repair command mongod --dbpath /data/db --repair
In my case it is net.maxIncomingConnections: 10 in the config file and MongoDB Compass.
All available connection slots the server has, are consumed by other clients and that causes new connection requests to just drop.
I was not aware that Compass would open so many connections. When I closed it, my problem was solved.
The default connection limit is 65536 as stated in config options
I don't know why this happens but sometimes my mongo instance fails to start properly. Just shutting it down and restarting it worked for me.

How do I access a remote aws lightsail mongodb over ssh tunnel

I have a Lightsail AWS instance up and running with a MEAN stack. I have an existing MEAN stack running on a different network. At the moment the node server.js connects to localhost for the mongo bit (on same machine) and all I want to do is replace the localhost with a connection to my mongo running on my AWS remote server.
I understand, that for security reasons, it is best to ssh tunnel this connection, which I think I am familiar with.
What I have done so far is this:
In a console on the machine hosting the node server (remote to the db) I have run:
ssh -L 8181:127.0.0.1:80 -i ~/LightsailDefaultPrivateKey-eu-west-2.pem bitnami#31.16.56.125 -N
I can then browse to the RockMongo UI from the local machine using localhost:8181/rockmongo ...yay.
If I then run the following:
ssh -L 8181:127.0.0.1:27017 -i ~/LightsailDefaultPrivateKey-eu-west-2.pem bitnami#31.16.56.125 -N
(27017 being the mongo port)
Then try and access the db from my remote machine using:
mongo --username XXXXXX --password XXXXXX 31.16.56.125:8181/testdata
I get the following error:
~]
2017-12-28T22:11:09.791+0000 Error: couldn't connect to server 31.16.56.125:8181 (31.16.56.125), connection attempt failed at src/mongo/shell/mongo.js:148
exception: connect failed
Am I doing this wrong? i.e. is the tunnel only for http connections and not mongo command line use? Do I need to test the connection some other way?
I've Googled all over the place for this and not had much luck (a lot of the AWS docs suggest punching a hole in the firewall - which one can no longer do!)
OK I've (partially) solved this, there were a few things wrong.
1) The mongo client was 2.6 and mongo running on AWS was 3.4. Upgrading this solved some issues - in that I was getting a more meaningful error message.
One thing I did have trouble with is that apt-get seemed to perform an update, yet the version reported when issuing the mongo command was still 2.6.
To solve this I had to run sudo apt-get purge mongodb-org* (note the asterisk). Then perform the update.... If you need to do this then follow these instructions:
https://docs.mongodb.com/v3.2/tutorial/install-mongodb-on-ubuntu/
2) This command
mongo --username XXXXXX --password XXXXXX 31.16.56.125:8181/testdata
won't work as I've omitted 'admin' from it and not specified localhost!
but
mongo admin --username XXXXXX --password XXXXXX localhost:8181/testdata
doesn't work either and gives the following output.
2018-01-03T22:00:42.380+0000 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: errno:111 Connection refused
2018-01-03T22:00:42.380+0000 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect#src/mongo/shell/mongo.js:229:14
#(connect):1:6
The only command I could get to work is:
mongo admin --username XXXXXX --password XXXXXX --port 8181
The default host is localhost, so in this case it uses the tunnel, this will also just connect to the test db, you can then admin from there.
What I haven't got to the bottom of is the specification of the host:port/db as an argument as per the output from running mongo --help
usage: mongo [options] [db address] [file names (ending in .js)]
db address can be:
foo foo database on local machine
192.169.0.5/foo foo database on 192.168.0.5 machine
192.169.0.5:9999/foo foo database on 192.168.0.5 machine on port 9999

Docker-Mongodb - How to connect to the mongo image in local(windows)

I am trying to connect to my docker mongo image, I installed mongodb in local, and added the mongodb bin path to windows path environment variable. And I am doing below steps
cd <docker-location>
docker login
docker ps
-- to check already mongo running
docker pull myrepo/mymongo-image:1.0
docker run -p 27017:27017 -d --net=host --name=mytestDB myrepo/mymongo-image:1.0
docker logs mytestDB
Output: MongoDB starting : pid=6 port=27017 dbpath=/data/db 64-bit host=moby
docker ps
Output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
34598734598 myrepo/mymongo-image:1.0 "/bin/sh -c /usr/bin/" 12 hours ago Up About a minute mytestDB
start mongo in local:
mongo --port 27017
But I am getting error like this:
MongoDB shell version: 3.2.1
connecting to: 127.0.0.1:27017/test
2016-10-13T20:04:12.273+0530 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, reason: errno:10061 No connection could be made because the target machine actively refused it.
2016-10-13T20:04:12.277+0530 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect#src/mongo/shell/mongo.js:226:14
#(connect):1:6
exception: connect failed
Please let me know where I did mistake.
I got solution, I like to share it.
'--noauth --bind_ip=0.0.0.0' options are missed in my commands.
This is the blog bring me out.
Tutorial: Setting up MongoDB Image Instance with Docker Toolbox
if you have already installed docker-toolbox and you have created an image but unable to visualize your mongodb database through robo3t
for this illustration i created my image with command
docker run --name blogA -p 27017:27017 -d mongo
Note: since 27017 is port for mongodb
Open up VirtualBox. Right click the "Default" machine.
Select settings from the menu.
Choose the Network tab in the settings menu.
Choose port forwarding at the bottom of the network option.
Click the plus icon to the right of the port forwarding menu.
Make the new entry with the name mongo.
Protocol should be TCP. Host and Guest IP can be left empty.
Set the host port to the port you make use in creating your
image I made use of port 27017. Set the guest port to 27017.
install robo3t from http://robomongo.org/download.html
click icon below file tab to create a new connection
Choose to create a new connection.
Name your connection and set the address to localhost and port to the port you set your Host port too in VirtualBox, in my case is 27017

Mongo Shell couldn't connect to the local server

I'm starting MongoDB using the following command:
sumeet#sumeet-acer:~$ sudo service mongod start
I get a reply as:
mongod start/running, process 7209
sumeet#sumeet-acer:~$
But when I try to enter MongoDB shell by typing mongo I get the following error:
sumeet#sumeet-acer:~$ mongo
MongoDB shell version: 2.0.4
connecting to: test
Fri Jun 12 14:01:59 Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84
exception: connect failed
By default, mongod and mongos use the 27017 port. Without any option, the client will try to connect to a server running on localhost and listening at that port.
A few general advices here:
check if the mongod server is really running (ps -edf | grep mongod);
check if it uses the default port (look in the config file1, or use nmap localhost -p0-65535 or netstat | grep 27017);
if mongod is not running, check in the log2 for clues about what goes wrong.
1 /etc/mongodb.conf (on Debian-like systems at least)
2 /var/log/mongodb (on Debian-like systems at least)
The default IP and Port for MongoDB is 127.0.0.1 & 27017.
If you are using config file for specifying settings than re-verify IP & Port mentioned in it.
If you are going with default option than mongod should function as expected.
try mongo 127.0.0.1:27017
this worked for me
For MongoDB 2.2.2 running on Ubuntu 12.10, it's in /var/lib/mongodb/mongod.lock
and after that running the repair command
sudo -u mongodb mongod --repair --dbpath /var/lib/mongodb/
courtesy: https://stackoverflow.com/a/13700262/4907105

Unable to connect to mongolab host

I am trying to connect to mongolab from terminal via below command
mongo ds061158.mongolab.com:61158/order_it -u <dbuser> -p <dbpassword>
I am getting the below error.
MongoDB shell version: 2.6.3
connecting to: ds061158.mongolab.com:61158/order_it
2014-07-09T13:52:44.890+0530 Error: couldn't connect to server ds061158.mongolab.com:61158 (23.22.170.205), connection attempt failed at src/mongo/shell/mongo.js:148
exception: connect failed
What has to be done in this case?
Thanks in Advance.
It looks like your network is blocking access to that port. I'd recommend contacting your network administrator or trying from a different network.
To test your network connectivity alone (no credentials necessary) you can run this command. This example was run from my unprivileged laptop just now and demonstrates a successful test.
% mongo ds061158.mongolab.com:61158
MongoDB shell version: 2.6.1
connecting to: ds061158.mongolab.com:61158/test
rs-ds061158:PRIMARY> db.runCommand({ping:1});
{ "ok" : 1 }
rs-ds061158:PRIMARY> exit
bye
Our full connectivity troubleshooting guide is here: http://docs.mongolab.com/connecting/#help
Also, feel free to contact us as support#mongolab.com if you'd like us to dig into the specifics of your server or code. We're always happy to help!
Regards,
Jared
I know this question is old, but in case someone still faces a similar issue, this is what helped me:
sudo rm /var/lib/mongodb/mongod.lock
sudo service mongodb restart
You should try to explicitly specify the port you want to use with the --port option:
mongo ds061158.mongolab.com/order_it --port 61158 -u <dbuser> -p <dbpassword>
From the mongo man pages:
--port <port>
Specifies the port where the mongod or mongos instance is listening. Unless specified mongo connects to mongod instances on port 27017, which is the default mongod port.