Mongodb problem_could not connect to server - mongodb

I use ubuntu 14.04LTS and with mongodb version 2.4.9.When I type "mongo" in terminal, it always show "connecting to: test
Tue Jun 3 10:03:57.911 Error: couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:145
exception: connect failed".
But if i run mongod in one terminal, and run mongo in another, all is ok.
If I close the window of mongod, the connection fail again.
I need to use mongodb without open a terminal of mongod, how can I do with it ?
Thank you!

You need to start mongodb as a service or daemon(depending on the OS,in your case daemon), check the documentation
Documentation

sudo service mongod start
Or
To run a mongod process as a daemon (i.e. fork), and write its output to a log file, use the --fork and --logpath options. You must create the log directory; however, mongod will create the log file if it does not exist.
The following command starts mongod as a daemon and records log output to /var/log/mongodb.log.
mongod --fork --logpath /var/log/mongodb.log

Related

Start replSet automatically

I am pretty new to installing MongoDB on Server. It was for a MERN stack project. And in order to use a library Pusher. I need to convert the Stand Alone Server to Replication set.
So I used the script sudo mongod --port 27017 --dbpath /var/lib/mongodb --replSet rs1 --bind_ip localhost,<my server ip> and the terminal will start logging. And then I can use another terminal to access the MongoDB database. But the problem is the moment I close the logging terminal (where I used the --replSet script) I will be prevented from accessing the MongoDB database.
So is there any way to automatically execute the --replSet script as soon as the server starts and keep it running?

Cannot run mongod.service and therefore cannot connect to mongo

I accidentally killed my mongo local server, so every time I want to connect I have to run this command:
mongod --port 27017 --dbpath /var/lib/mongodb --logpath /var/log/mongod.log
How can I run it automatically?
I'm missing for some reason the mongod.service file, so I cannot run systemctl start mongod.
If anyone can please provide a solution (or the missing file) it will be helpful.
If you run this, I think the service will be modified and restored.
sudo systemctl enable mongod
Then run it again.
sudo service mongod restart

Laravel 5 and MongoDB connection

No suitable servers found (serverselectiontryonce set): [Failed connecting to '127.0.0.1:27107': Connection refused]
I install "jenssegers/mongodb": "^3.0" using composer, but am getting error, i can't connect mongodb.
I got this error when I had mistaken the IP of my machine. I was in VM and used 127.0.0.1, when I needed to use 10.0.2.2, IP of my host machine. Wasted an hour for this. Maybe it saves someone's time...
According to MongoDB documentation
mongod is the primary daemon process for the MongoDB system
According to above mentioned description ,it seems that mongod process is not running on your server and 27017 is default port for mongod instance
Please try executing following command to verify if mongod process is running on server
pgrep mongod
It will return process ID if mongod process is running on server otherwise nothing.
Issue following command in shell to start mongod process
mongod --noauth --dbpath /var/lib/mongodb
I had this error.
I had to restart mongodb to make this work.
I used the command below
sudo service mongod restart

mongodb error. how can I solve the erro in mongoDB?

MongoDB shell version: 3.2.6
connecting to: test
2016-05-08T10:46:12.023+0530 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2016-05-08T10:46:12.023+0530 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
exception: connect failed
https://docs.mongodb.com/v3.0/tutorial/install-mongodb-on-ubuntu/
Do you have the mongodb service running? Run sudo service mongod status and see what it returns. If it is stopped, run sudo service mongod start to start the mongodb and try connecting to it.
You can also, check whether the mongodb is listening on 27017 or not by running: sudo netstat -lnp | grep 27017.
If sudo service mongod start is giving a error, you may need to check the configuration. We can help you better if you can post the output of the above commands.
Update:
Create the file /etc/systemd/system/mongod.service with the following content:
[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target
[Service]
User=mongodb
ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf
[Install]
WantedBy=multi-user.target
Just restart the service in terminal, verbose mode helps you to understand the background configuration
$brew services restart mongodb --verbose
and open another terminal (don't use same terminal) and type mongod, you should be able to see process running.
Excuse have the same problem . Just installed Ubuntu 16.04 LTS . The installation process , the same as in the official website of mongo . When I do:
sudo service mongod status; appear
● mongod.service
Loaded: not-found (Reason: No such file or directory)
Active: inactive (dead)
I guess the error will be in the configuration of "data directory" and the port, but not as configure it properly
Fork your mongodb process as a daemon so that it keeps running in background. E.g
./mongod --port 27017 --dbpath /home/db_directory --logpath /logs/mongod.log --fork

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