Mongoose connection error: MongoError: failed to connect to server - mongodb

I was running mongo 3.4 on centOS. It was using authorization. I needed to upgrade it to mongo 3.6. I upgraded it and now I'm not able to connect it through any means remotely. Neither with the shell nor with the node server itself.
Here is the mongoose connection.
const uri = 'mongodb://admin:12345#host:27017/db?authSource=admin';
mongoose.connect(uri);
Here is mongod.conf
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# Where and how to store data.
storage:
dbPath: /var/lib/mongo
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
# network interfaces
net:
port: 27017
# bindIp: 127.0.0.1 # Listen to local interface only, comment to listen on all interfaces.
security:
authorization: enabled
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options
#auditLog:
#snmp:

Probably the upgrade was not successful, and the restart of the mongod service failed.
View the logs in /var/log/mongodb/mongod.log and check for any inconsistency in the mongod.conf.
Check if the service is up and if it is listening on port 27017.
service mongod status
netstat -tl | grep 27017 # or using the ss command
ss -tl | grep 27017
From the official documentation:
Starting in MongoDB 3.6, mongod and mongos instances bind to localhost by default. Remote clients cannot connect to an instance bound only to localhost. To override and bind to other ip addresses, use the net.bindIp configuration file setting or the --bind_ip command-line option to specify a list of ip addresses.
Try the following setting to enable the service to listen on all the interfaces
net:
port: 27017
bindIp: 0.0.0.0

Related

unable to connect mongod from remote and local on IP address

My mongod servers runs on IP 67.219.110.71 and default port 27017
Below is the command to start mongod
mongod --dbpath /data/db --fork --logpath /dev/null
After login to the linux server 67.219.110.71 I'm able to telnet successfully like below:
telnet localhost 27017 ----> SUCCESS
However, when I telnet using the IP address it does not connect from the same host 67.219.110.71 or from a remote hots both fail
telnet 67.219.110.71 27017 ----> FAILS
Note:
have restarted the mongod service several times and after any configuration change.
port 27010 was opened on the firewall using firewall-cmd command.
I'm able to connect on ssh port telnet 67.219.110.71 22 ----> SUCCESS
Below is my mongod configuration file /etc/mongod.conf:
# mongod.conf
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# Where and how to store data.
storage:
dbPath: /var/lib/mongo
journal:
enabled: true
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1,67.219.110.71,0.0.0.0,:: # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
# bindIpAll: true
# bindIp: 0.0.0.0
security:
authorization: "enabled"
Can you please suggest?

How to connect mongoDB Compass to mongoDB container which runs in azure VM?

I have mongoDB container which runs on azure VM, and I'm trying to connect it to my mongoDB compass.
I have Public IP address to my VM, the port 27017 is open in my vm and also in my mongo container.
I have authentication, so to connect my mongo I'm Enter the mongo container and write the command "mongo -u username -p password --authenticationDatabase admin" (Relevant).
When I'm trying to connect I get "connection timed out" error message.
docker container ls
Open ports on the VM
My compass login page
I solved it by changing the configuration file of the mongoDB container.
Step 1:, make sure port 27017 is open on the VM:
Step 2:, create a mongoDB configuration file as below, name it mongod.conf, and change the bindIp field to your host IP (change < Host IP> to your host IP).
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: /data/db
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: 127.0.0.1,<Host IP>
# how the process runs
processManagement:
timeZoneInfo: /usr/share/zoneinfo
security:
authorization: enabled
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:
Step 3: Copy the mogod.conf file into your VM, and save it wherever you want.
Step 4: Run the command:
docker run -d -v <FolderPathOnTheVM>/mongod.conf:/etc/mongod.conf -p 27017:27017 mongo -f /etc/mongod.conf
Make sure you changed the < FolderPathOnTheVM> to the path of the mongod.conf file on the VM (The path of step 3).
Do netstat on 27017 and check if its running on localhost or public IP. If its running on localhost, then change it to your public IP in mongodb config file and then retry. Also, telnet from your local machine to the mongodb IP and port to check if its working locally.

Mongo auth fails to login when using mongod.conf

So this is the weirdest thing.
I have two centOS 7 servers running mongo. I now wanted to enforce authentication so I added the security.authorization: enabled to the mongod.conf file.
I already have a user on database "buzzztv".
So when I ran mongod --conf /etc/mongod.conf on the first server everything went fine.
Then I did the exact same thing on the second server and whenever I try to connect with one of the users I get the following error:
connecting to: mongodb://127.0.0.1:27017/?authSource=buzzztv&compressors=disabled&gssapiServiceName=mongodb
2020-02-20T13:02:35.166+0000 E QUERY [js] Error: Authentication failed. :
connect#src/mongo/shell/mongo.js:341:17
#(connect):2:6
2020-02-20T13:02:35.168+0000 F - [main] exception: connect failed
2020-02-20T13:02:35.168+0000 E - [main] exiting with code 1
Now if I run mongod --fork --logpath /var/log/mongodb/mongod.log --auth the login works perfectly fine.
So obiously I could just run this command, but I want to use the mongod.conf.
Here is my mongod.conf file, I checked and it is a perfect copy of the file from the server in which it does work.
Any ideas?
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# Where and how to store data.
storage:
dbPath: /var/lib/mongo
journal:
enabled: true
# engine:
# wiredTiger:
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0 # 127.0.0.1 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
security:
authorization: enabled
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options
#auditLog:
#snmp:
So after several hurtful hours of looking into it, I neede to change the
storage:
dbPath: /var/lib/mongo
journal:
enabled: true
Apparently something was wrong with my /var/lib/mongo so I backed up the data, and created a new folder /var/lib/mongodb
Then edited the mongod.conf file to:
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
Then it all worked fine. Now I'll just re-create the users and re-insert all the data and I'm good to go.
Hope this saves someone the wasteful hours I've lost

Could not connect to replica set in MongoDB with pymongo

I am trying to connect to replica set in MongoDB with the following command:
client = MongoClient(
"PublicIP:27017,PublicIP:27017,PublicIP:27017,PublicIP:27017,PublicIP:27017",
replicaSet="rs0", readPreference='nearest',
localThresholdMS=500)
db = client.sampledb
# checks the connection to RS in Mongo
try:
db.command("serverStatus")
except Exception as e:
print(e)
else:
print("\n Connection established to the Replica Set!)
I have setup 5 nodes in AWS (1 master, 4 slaves) and the /etc/mongod.conf file in all nodes looks like this:
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# Where and how to store data.
storage:
dbPath: /var/lib/mongo
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
# network interfaces
net:
port: 27017
#bindIp: 127.0.0.1 # Listen to local interface only, comment to listen on all interfaces.
security:
authorization: "disabled"
#operationProfiling:
replication:
replSetName: rs0
When I try to connect from my local machine I get a timed out like this:
ip:27017: timed out,ip:27017: timed out,ip:27017: timed out,ip:27017:
timed out,ip:27017: timed out
Does anyone know why?
Starting 3.6 bindIp defaults to localhost only. So, assuming you are running 3.6 or later your configuration is not accessible from outside, since you commented out bindIp.
This post might be helpful to understand some of the nuances: https://www.mongodb.com/blog/post/enabling-ip-security-for-mongodb-36-on-ubuntu
Obviously, don't forget to make sure AWS Security Group on your instances allows Ingress connection on port 27017.

Cannot connect remotely to mongodb installed on AWS instance

I have installed and started mongodb on my RHEL AWS instance. Now I want to connect to the mongodb server from my local machine but am not able to do so.
I have made changes to the bind_ip parameter in the mongodb.conf file and also have added rules to the security group for the AWS instance to allow connections on port 27017 for any inbound request. Am I missing anything ??
MongoDB Version - 3.4.14
AWS Instance - RHEL
Thanks in advance !!!
Adding the mongod.conf file
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# Where and how to store data.
storage:
dbPath: /home/ec2-user/AutoMaTics/setup/dbData
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
# network interfaces
net:
port: 27017
# bindIpAll: true
# bindIp: 127.0.0.1,13.127.31.137 # Listen to local interface only, comment to listen on all interfaces.
#security:
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options
#auditLog:
#snmp:

Categories