Not able to connect to mongodb database - mongodb

Every time if my server restarts by mistake or I stopped and then restart I'm not be able to connect to my mongodb. I get this below error and according to this 27017 port is not open but in my security group I opened this port.
MongoDB shell version v5.0.2
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 :: Connection refused :
connect#src/mongo/shell/mongo.js:372:17
#(connect):2:6
exception: connect failed
exiting with code 1```
So, I don't know what I'm doing wrong please help me because I'm facing this issue from very long time and to rid of this I've to always setup new mongodb

You need to define the MongoDB as service and set to automatic start.
On Windows add lines similar to
processManagement:
windowsService:
serviceName: mongod
displayName: MongoDB
description: MongoDB Server
to your configuration file (typically mongod.conf or mongod.cfg), if not already existing.
Then the easiest way to install the service is running
mongod.exe --config <your config file> --install
On Linux (Redhat, Centos, etc.) it works like this.
Copy default service file
/usr/lib/systemd/system/mongod.service to /etc/systemd/system/mongod.service
Customize service file /etc/systemd/system/mongod.service, if needed
Enable the service: systemctl enable mongod
Start the service: systemctl start mongod

Related

Mongod authentication makes GCP VM Unreachable

I'm using GCP VM to run a MongoDB instance.
It works very well, with authentication turned off, but once I turned it on "enabled" by editing
sudo vi /etc/mongod.conf, and then restart service using sudo service mongod restart.
Once I did this, and try to connect to my DB using my credentials, I've got the following error from mongo:
mongo mongodb://<gcp vm statcic ip>:<mongodb static port>
result in:
connecting to: mongodb://<myservererIP>:<mongodbport>
2020-04-04T19:30:36.644+0200 E QUERY [js] Error: couldn't connect to server
<myservererIP>:<mongodbport>, connection attempt failed: SocketException:
Error connecting to <myservererIP>:<mongodbport> :: caused by :: Connection refused :
connect#src/mongo/shell/mongo.js:257:13
#(connect):1:6
exception: connect failed
My VM IP is Static and account in mongo were created according to mongoDB document, they should be fine, even if they were not, I suppose that I should have an "Authorization required" or something, and not connection refused or Network Unreachable.
I've actually found the problem.
In /etc/mongod.conf , I thought that #security: was here for informative purpose , for file structuring, but you actually have to also uncomment it, along with authentication: enable.
Otherwise, mongod will detect the mongod.conf file as corrupted/bad format, and run without it (probably using a default conf).
I went from:
#security:
authorization: enabled
to
security:
authorization: enabled
Probably a very poor mistake from a begginer, but I'll keep it here, maybe it will help someone.

MongoDB, NodeJs. No connection could be made because the target machine actively refused it. :

I am trying to connect to my MongoDB database in localhost. While I type mongo in command shell I am getting an error in the command shell:
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 :: No connection could be made because the target machine actively refused it. :
connect#src/mongo/shell/mongo.js:341:17
This will start the mongo server as a service:
brew services start mongodb-community
command mongod will run the MongoDB server using the default configuration. You can change these configurations by locating the default path of the configuration files.
The primary daemon process for the MongoDB database system is called mongod. You need to start this service before making connection attempts.
You can do this in Ubuntu by:
sudo service mongod start
or,
sudo systemctl start mongod
Start mongod on system boot by:
systemctl enable mongodb.service
I had the same issue.
When running mongod command, it mentioned the C:/data/db as E:/data/db path.
After I created the folder in mentioned E:/data/db path it works!
For windows user
1. Type ctrl + r
2. Scroll to mongodb server
3. start the server
do you have a linux distro? If you do, the solution is a bit annoying as you have to do it everytime you start your pc. The solution is:
Type "mongo" and "mongod" once in the terminal. It will show an error, don't worry, that's the whole point
type this:
chown -R mongodb:mongodb /var/lib/mongodb
chown mongodb:mongodb /tmp/mongodb-27017.sock
sudo service mongod restart
tip: copy the whole thing and paste on the terminal using ctrl+shift+v. You're welcome :-)

Unable to connect to MongoDB on EC2

Unable to connect MongoDB (Version 4.0.5) on EC2 (Windows Server 2018)
I have tried all the options.
1) Modify mongod.cfg with
net:
port: 27017
bindIp: 0.0.0.0
bindIpAll: true
2) start with mongod --bind_all_ip
3) My security group on EC2 has allowed 27017 port inbound for 0.0.0.0/0.
I'm trying to connect through terminal
mongo mongodb://*********.compute.amazonaws.com:27017
However, I get following exception when connecting through mongo shell
mongodb://e*******.compute.amazonaws.com:27017/?
gssapiServiceName=mongodb
2019-01-21T15:51:11.044-0800 E QUERY [js] Error: couldn't connect
to server *****.compute.amazonaws.com:27017, connection attempt
failed: SocketException: Error connecting to
******.compute.amazonaws.com:27017 (54.244.203.203:27017) :: caused by
:: Operation timed out :
connect#src/mongo/shell/mongo.js:328:13
#(connect):1:6
exception: connect failed
What else I need to do?
Also, it seems my mongodb.cfg has no effect on the server. When I start server with above change in cfg file, I get the warning in server console that the server is bound to localhost, regardless. Starting the server with mongod --bind_all_ip gets rid of that warning.
Firewall was the problem. Need to open port 27017 by running WF.msc from run command on Windows. Spent a whole day trying to figuring out all other options. I still don't know how to get the mongod.cfg working for all ip addresses. I need to run mongod with --bind_all_ip option for that.

Already running Mongo db start up issue in ubuntu

I have issue with Mongo DB start up in ubuntu 14.04 64 bit,its working fine when i installed first time but suddenly from now when i type mongo in ubuntu terminal its showing error:
MongoDB shell version: 3.0.7
connecting to: test
2015-11-26T11:59:03.888+0530 W NETWORK Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2015-11-26T11:59:03.889+0530 E QUERY Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed
at connect (src/mongo/shell/mongo.js:179:14)
at (connect):1:6 at src/mongo/shell/mongo.js:179
exception: connect failed
I tried many solution
i.e
1.deleting lock files, and restart mongo,
2.Change bind ip to 0.0.0.0 in mongod.conf
but my issue still not resolved.
Try to execute the following command to grep process id of mongod to determine if mongo service is running or not
pgrep mongod
If process id exists then please follow the steps as mentioned below
(a) Kill existing mongod process using following command
sudo kill -9 [process_id]
(b) Start mongod process using
sudo mongod --noauth --dbpath /var/lib/mongodb

can't run mongo as a service

So I've got a virtual machine running on windows azure and installed mongoDB. However mongod works fine when I do: mongod --dbpath /mnt/datadrive/data but when I end the process and do mongo, I get:
2015-01-10T14:23:17.474+0000 warning: Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2015-01-10T14:23:17.476+0000 Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed at src/mongo/shell/mongo.js:146
I've edited the config /etc/mongod.config however when I try and run sudo service mongo start I get:
Job for mongod.service failed. See 'systemctl status mongod.service' and 'journalctl -xn' for details.
What I'm initially trying to do is have mongoDB running as a service on my virtual machine so I can remotely can connect to it at anytime.
When you typed in the mongod command, did you also give it a path? This is usually the issue. You don't have to bother with the conf file. simply type
mongod --dbpath="put your path to where you want it to save the working area for your database here!! without these silly quotations marks I may also add!"
example: mongod --dbpath=C:/Users/Kyle-3/Desktop/DEV/dangerzonearea/test/mongodb
That is my path and don't forget if on windows to flip the slashes forward if you copied it from the or it won't work!
Solution:
Don't end the service. Let it continue while you use mongo from another cmd or shell.