Can't connect to mongodb remotely - mongodb

I have a server on Digital Ocean running a MongoDB instance, if I ssh into the server, I can connect to the Mongo Shell with no problem. When I try to connect on my own machine, with PyMongo, it also connects with no problem.
The problem is when I try to connect from my machine via Mongo Shell, then I get
MongoDB shell version v3.6.3
connecting to: mongodb://<server_ip>:4131/?replicaSet=mongodb%3A
2018-06-26T11:50:05.799-0300 E QUERY [thread1] Error: '/<server_ip>:4131' in 'mongodb://<server_ip>:4131/?replicaSet=mongodb%3A' appears to be a unix socket, but does not end in '.sock' :
connect#src/mongo/shell/mongo.js:251:13
#(connect):1:6
exception: connect failed
So as some may have noticed, I did change the port to 4131, and yes, I pass this information on my connection string.
I have done a lot of research and already discarded some things:
No, there's no .lock file, and I have made sure to run mongod --repair just to be sure.
My /etc/mongod.conf has external ips enabled, (ie.: bindIp: 0.0.0.0)
Extra information:
There is a /tmp/mongodb-4131.sock file, which should be the socket mongo shell is looking for, right?
I looked up before changing the ports and made sure nothing was using 4131
Firewall is disabled (just to make sure that's not the problem)
Any help is appreciated.

Related

error mongo without mongod running first locally in win 10

I have this mongo.zip installed in my win 10.
I have set the environment path with bin.
Somehow whenever I start command line,
my mongo cannot start without mongod first.
C:\Users\asus>mongo
MongoDB shell version v4.4.1
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 :: No connection could be made because the target machine actively refused it. :
connect#src/mongo/shell/mongo.js:374:17
#(connect):2:6
exception: connect failed
exiting with code 1
but if I run mongod before mongo, mongo is running perfectly.
So, my question is: how do I running mongo without mongod locally.
PS. my compass is running and can connect to AWS cloud.
Please provide pictures if you can.
Thank you!
I think you, first of all, you have to know about the difference between them.
Mongod is the process of MongoDB.
Mongo is a shell. You can use it to connect and run any mongo command on any MongoDB.
So, your error means;
You are trying to connect your own MongoDB with typed mongo, because the default -host parameter is localhost. But your mongod process is not running. So you have to start it.
connecting to: mongodb://127.0.0.1:27017
127.0.0.1:27017 equals localhost.
If you have MongoDB (mongod process) on AWS. You should connect with below command
mongo --host mongodb0.example.com:28015

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.

Can't connect to MongoDB server on Google Compute Engine from second VM instance

I have MongoDb installed on two Ubuntu 16 VMs both hosted on the same network on Google Cloud's Compute Engine. The connecting instance has Internal IP 10.132.0.2, the database instance has 10.132.0.3.
(https://gyazo.com/59f9086591a1d6673e8194a50fff51e1)
I've opened tcp 27017 for the instance I wish to connect to. I added this using the gcloud console commands.
(https://gyazo.com/0d158aa735c6967e278fac63d598721f)
I have also tried opening tcp:1-65535;udp:1-65535 for testing purposes, with no result.
Running mongo on either instance will properly connect to it's own mongodb service, so they're installed, running and working.
On the database instance, netstat -a | grep :27017 displays that it's listening correctly.
(https://gyazo.com/0f4fb3c49a51f6886ff4ccb2d44a132a)
On the database instance, the config file at /etc/mongod.conf of the is edited so bind_ip also has the Internal IP of said instance.
(https://gyazo.com/20fb669506e7e67ef49fdcf9af6df144)
I have also tried 0.0.0.0 for testing purposes, with no result.
Running mongo 10.132.0.3 from the connecting instance results in Failed to connect to 10.132.0.3:27017, reason: errno:111 Connection refused.
(https://gyazo.com/9e5aec732e3f09cbfa62a4d78df3620f)
Running nmap -p 27017 10.132.0.3 on the connecting instance shows port 27017 is closed.
(https://gyazo.com/7ccd905db5b9946d616176b9ab75479c)
I'm at an utter loss, please help me out? :<
Your mongodb servers are only listening on the localhost address (127.0.0.1), rather than on INADDR_ANY (shows up as *:27017 in netstat).
Like this question, it looks like you need to set bind_ip = 0.0.0.0 in your mongod.conf.
Thanks to E. Anderson's link I managed to solve this issue; suddenly turning this into a duplicate. However, neither this answer nor the answer in the link were "correct".
The solution to set bind_ip=0.0.0.0 in mongod.conf is rather dangerous, besides, it didn't work. However, killing the mongodb service, and then manually running mongod --bind_ip 10.132.0.3 actually allowed my two instances to connect.
Which.. means that javadude's reply in the other thread saying "But I still dont get it. Why this did not work when I modified mongo.conf file to accept bind_ip 0.0.0.0"` also goes for me. I don't get it, but, it definitely worked to manually give it an ip-address to listen on.

Access MongoDB from other server

I have a MongoDB database on my Linux server. I want to access it from another server. I tried to make a connection from my local computer with the Robomongo. The connection is succesfull, but the authentication fails.
How can I get the authentication credentials? Or should I change something in MongoDB before I can acces the database from another server / pc?
Someone else have set up this database, and there is no possibility to ask him this questions.
I have found the solution by my self:
The File etc/mongod.conf has a line 'bind_ip'. In this line, you originally have to add the IP address which you want to access your database. But, it don't work! You should better comment this line.
But, you don't have any authentication now, so you have to add authentication. Here you have an tutorial about this: http://ghosttx.com/2012/03/how-to-connect-to-a-remote-mongodb-server-with-mongohub-for-mac/
When you have done that, you have to enable authentication. You can do this by editing etc/mongod.conf again, and uncomment the line 'Auth = true'.
Now you can connect with you Mongo Database ;)
Ive sorted it by adding ssh option to RoboMongo following this link:
http://www.mongovue.com/2011/08/04/mongovue-connection-to-remote-server-over-ssh/
Im on OSX and connecting to Ubuntu 14 / Mongo 2.6.7 on VPS and when Ive added my ssh details to the Robomongo all seem to work ok (Ive also changed the mongo config to remove the ip_bing and enabled port 27017)
If you do not like to bother with authentication and stuff just make an SSH Tunnel:
ssh -fN -l username -i .ssh/id_rsa -L 9999:localhost:27017 remote.com
Just connect to mongodb on localhost:9999 and it will establish a connection to your mongodb on port 27017 on your server at remote.com.
Run your mongodb with following command to access mongodb from other servers
mongod --port 10945 --bind_ip 0.0.0.0
I was not able to use Robomongo with MongoDB 3.0 too (connecting from a Windows machine to a Linux one, using SSH). The only tool that works for me is MongoChef (http://3t.io/mongochef/).

mongodb could not connect to server

I'm new to MongoDB. I'm trying to connect to my remote mongo database from my work machine( I tried for both mongohq as well as mongolab). I'm getting the error listed below. When I run the same command from my home machine it runs fine and does connect to remove mongo database. I'm not sure what is messed up on my work machine but I'd like to fix it. Thanks for you help.
MongoDB shell version: 2.4.9
connecting to: linus.mongohq.com:10097/mydb
Thu Jan 16 04:16:02.689 Error: couldn't connect to server linus.mongohq.com:10097
at src/mongo/shell/mongo.js:147
exception: connect failed
As per question comments, your mongo shell client can't connect to the mongo instance. Assuming that your internet connectivity is fine, the most common reason for the failure is that the ports are blocked on your network.
Some ISPs automatically block these ports by default as an additional security protection for users that don't need them open. Contact your ISP or network admin, they should be able to help you out.