Can't connect to MongoDb externally - mongodb

Some information:
The port is indeed open(i can bind it to apache2, for example, and access it externally)
I've tried setting the bind_up parameter to 0.0.0.0, to my localip, etc
I can access it through my local IP(192.167.10.xxx)
I didn't isntall mongo with apt-get, just downloaded it and ran mongod
I've spent a lot of time searching and trying different things, anyone has a idea on what could be causing it?
Mongo runnning
edit:
Mongo options: [initandlisten] options: { config: "config.conf", net: { bindIp: "0.0.0.0", port: 8008 }, security: { authorization: "enabled" }, storage: { dbPath: "/home/defense/mongo/data" } }
When i try to connect:
[thread1] Failed to connect to 146.164.x.xxx:8008, in(checking socket for error after poll), reason: Connection refused
Port check(also I used to connect to MySql using that same port):
Port 8008 is open on 146.164.x.xxx.
Any ideas?

Related

Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27

I want to change the default port of mongodb so I did :
sudo nano /etc/mongod.conf
# network interfaces
net:
port: 27042
bindIp: 127.0.0.1
and
sudo service mongod start
But now when I want to run "mongo" I got this error:
MongoDB shell version v4.0.24
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
2021-05-18T12:44:36.436+0200 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:356:17
#(connect):2:6
exception: connect failed
The message says " couldn't connect to server 127.0.0.1:27017" despite I changed the port in mongod.conf. I even tried to reboot my computer but it still not working.
UFW is not active si I don't think it's a firewall problem.
Based on your mongod.conf file, MongoDB will run on port 27042. When you run the command mongo without parameter --port, it will connect to the default port 27017 then you have the error.
I think this command mongo --port 27042 will work.

mongos Socket operation timed out

I've got a mongos router running on amazon ECS in an EC2 machine, and also hosted on a route53 hosted zone.
The thing is that I can reach the mongos service via HTTP with the record name:
https://mongos-router.awsqa.xxx.net/
With the response:
It looks like you are trying to access MongoDB over HTTP on the native
driver port.
Which means that mongos is running correctly.
The ecs logs I get shows its running correctly also.
But trying to connect to it via mongo command:
$ mongo --host mongos-router.awsqa.xxx.net
MongoDB shell version v4.4.4
connecting to: mongodb://mongos-router.awsqa.xxx.net:27017/?compressors=disabled&gssapiServiceName=mongodb
Error: couldn't connect to server mongos-router.awsqa.xxx.net:27017, connection attempt failed: NetworkTimeout: Error connecting to mongos-router.awsqa.xxx.net:27017 (xx.xxx.xx.xx:27017) :: caused by :: Socket operation timed out :
connect#src/mongo/shell/mongo.js:374:17
#(connect):2:6
exception: connect failed
exiting with code 1
I've confirmed that the container port is correct also:
"portMappings": [
{
"hostPort": 0,
"protocol": "tcp",
"containerPort": 27017
}
],
a part of mongos.conf file:
net:
bindIp: 0.0.0.0
port: 27017
Dockerfile final command:
CMD [ "mongos", "--config", "/etc/mongos.conf" ]
Any idea at all to what is causing this network timeout error will be helpful

MongoDB, remote connection failing

I'm trying to connect to mongoDB server running in another AWS Account from my other AWS account to refresh the content.
I'm getting an error.
I NETWORK [thread1] Socket recv() errno:104 Connection reset by peer mongo_db_server:27017
I NETWORK [thread1] SocketException: remote: (NONE):0 error: 9001 socket exception [RECV_ERROR] server [mongodb_db_server:27017]
2021-01-14T23:26:26.015-0500 E QUERY [thread1] Error: network error while attempting to run command 'isMaster' on host 'mongodb_db_server:27017' :
connect#src/mongo/shell/mongo.js:231:14
#(connect):1:6
Remote MongoDB server config has
net:
port: 27017
bindIp: 0.0.0.0
Security groups allow connection to 27017 from 0.0.0.0/0 fo testing purpose.
what am i missing?
It was due to network connectivity.

Unable to connect mongodb running on ec2

I am trying to connect with mongodb, But it is giving me error.
MongoDB shell version: 3.2.22 connecting to: test
2019-09-07T05:17:09.460+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 2019-09-07T05:17:09.461+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
Tried:
sudo nano /etc/mongod.conf
Commenting bindip
#network interfaces
net:
port : 2701
#bindip = [0.0.0.0]
Setting up this to
#network interfaces
net:
port : 2701
bindip : 0.0.0.0
Setting up this to
#network interfaces
net:
port : 2701
bind_ip = 0.0.0.0
Setting up this to
#network interfaces
net:
port : 2701
bindip = [0.0.0.0]
Saved this file then :
sudo service mongod restart mongo
Again connection refused giving same error :
MongoDB shell version: 3.2.22 connecting to: test
2019-09-07T05:17:09.460+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 2019-09-07T05:17:09.461+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
Any Help?
Just curious, Did you enable the AWS security group for that port since you are connecting remotely?
https://docs.aws.amazon.com/vpc/latest/userguide/VPC_SecurityGroups.html#VPCSecurityGroups
try with below script :
mongod --fork --logpath /opt/app/mongo/mongod.log --auth --port 27017 --dbpath /var/lib/mongodb --bind_ip_all

Can't connect to MongoDB remote server

I have setup mongodb on a Google cloud compute instance and am trying to connect to it remotely. My mongod.conf file looks like this:
# 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: 0.0.0.0
#processManagement:
security:
authorization: 'enabled'
I have setup a firewall rule in my Google Cloud console that tags the instance and opens tcp:27017 for ip ranges 0.0.0.0/0.
Checking on the port 27017 it looks like mongo is listening:
sudo netstat -tulpn | grep 27017
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 3781/mongod
Overall it also seems like port 27017 is open:
netstat --listen
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 *:27017 *:* LISTEN
...
On the instance I setup an admin database with admin user:
>use admin
>db.createUser(
{
user: 'admin',
pwd: 'somepass',
roles: [{ role: "userAdminAnyDatabase", db: "admin" }]
}
)
I also setup a user for a test database:
>use test
>db.createUser(
{
user: 'tester',
pwd: 'apassword',
roles: [{ role: "readWrite", db: "test" },
{ role: "read", db: "reporting" }]
}
)
This user works locally:
>use test
>db.auth('tester', 'apassword')
1
However when I try to connect remotely it fails:
$ mongo -u tester -p apassword 12.345.67.890/test
MongoDB shell version v3.4.1
connecting to: mongodb://12.345.67.890/test
2017-11-14T12:47:07.369-0700 W NETWORK [main] Failed to connect to 12.345.67.890:27017 after 5000ms milliseconds, giving up.
2017-11-14T12:47:07.370-0700 E QUERY [main] Error: couldn't connect to server 12.345.67.890:27017, connection attempt failed :
connect#src/mongo/shell/mongo.js:234:13
#(connect):1:6
exception: connect failed
I'm not a networking expert so I've pretty much exhausted my knowledge at this point and don't know how to proceed.
Am I missing something in mongod.conf? Did i setup the firewall incorrectly? Any help is appreciated.
I came up with a solution for this, but I'm unclear why it works and not my original approach.
My instance has http/https enabled. This uses the firewall rules default-allow-http and default-allow-https. These rules enable connections from anywhere (0.0.0.0/0) through tcp ports 80 and 443 respectively. I edited the http rule and added tcp port 27017.
Now I can connect to the server.
As a test, I reset the http rule and added another rule applied to all instances opening up tcp:27017 to 0.0.0.0/0. Essentially everything is the same as the http rule save for the name and the target tags. With this change I cannot connect to the server.
It seems rather strange and it doesn't feel like an intended behavior, unless my understanding of the firewall rules in incomplete.
In the end it looks like either setting up mongo to use one of the http/https ports or adding port 27017 to those rules is the way to go.