Cannot run mongod.service and therefore cannot connect to mongo - mongodb

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

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?

MongoDB replica set on single machine

I have a single instance (I.e. no virtualisation) of Linux running on a server. I have a single mongod instance that is running. Moving to production I want to implement replica sets. Everything I've read talks about running mongod on multiple machines.
I understand it might not be best practice however is it possible to run replica sets of the same machine. Also, the machine has two hard drives. I want the primary DB to be on the first HD and the replica set on the second hard drive.
Is this setup possible?
Here's how I did it on Ubuntu. I wanted to use systemd for the second instance.
Copy our existing MongoDB conf:
sudo cp /etc/mongod.conf /etc/mongod_repl.conf
sudo pico /etc/mongod_repl.conf
Change these lines:
storage:
dbPath: /var/lib/mongodb_repl
systemLog:
path: /var/log/mongodb/mongod_repl.log
net:
port: 27018
replication:
replSetName: rs0
Create the dirs and files, and assign permissions
sudo mkdir /var/lib/mongodb_repl
sudo chown mongodb:mongodb /var/lib/mongodb_repl
sudo touch /var/log/mongodb/mongod_repl.log
sudo chown mongodb:mongodb /var/log/mongodb/mongod_repl.log
Copy our existing startup file:
sudo cp /lib/systemd/system/mongod.service /etc/systemd/system/mongod_repl.service
If you can't find mongod.service, sudo systemctl status mongod. It looks like:
Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
sudo pico /etc/systemd/system/mongod_repl.service
Change these lines:
ExecStart=/usr/bin/mongod --config /etc/mongod_repl.conf
PIDFile=/var/run/mongodb/mongod_repl.pid
Let systemctl know we've made some changes:
sudo systemctl daemon-reload
Try start it our second server:
sudo systemctl start mongod_repl
sudo systemctl status mongod_repl
If it's not running, have a look at the logs:
sudo tail -n100 /var/log/mongodb/mongod_repl.log
sudo journalctl -n100 -u mongod_repl
(You can add a -f to either of those lines to tail the log)
Log in
mongo --port 27018
Great! Now you have a second MongoDB server up and running.
Configure your original server to use the same replication set:
sudo pico /etc/mongod.conf
Add this:
replication:
replSetName: rs0
Restart MongoDB
sudo systemctl restart mongod
Now you should be able to go through the process of rs.initiate() on your primary, and then add 127.0.0.1:27018 as your secondary.
Yes it is possible to get the replica set up and running on a single machine and this is how I do it for testing:
Install mongodb-org on your machine by following the official instructions and make sure it is starting fine.
Then stop the instance process: sudo systemctl stop mongod
You can now start a new instance with --replSet option by following link. Also, in order to have it running in the background you can use --fork and --logpath to create a child process:
mongod --fork --port 27017 --dbpath /var/lib/mongodb --logpath /var/log/mongodb/mongo_0.log --replSet rs0 --bind_ip localhost,<Your machine's IP address>
Then connect to the instance and initiate the replica set in Mongo shell:
mongo --port 27017
>rs.initiate()
In case you want to stop the process do ps aux | grep mongod and then kill -9 <pid>
Yes, it is possible.
Create a script: sudo nano mongod.sh
Copy and paste:
sudo mongod --port 27017 --dbpath /var/lib/mongodb --replSet rs0 --bind_ip_all &
sudo mongod --port 27018 --dbpath /var/lib/mongodb2 --replSet rs0 --bind_ip_all &
sudo mongod --port 27019 --dbpath /var/lib/mongodb3 --replSet rs0 --bind_ip_all &
Permit the script to execute:
sudo chmod +x mongod.sh
Run the script:
./mongod.sh
Initialize replica set in primary mongo server:
$ mongo
> rs.initiate()
output
{
"info2" : "no configuration specified. Using a default configuration for the set",
"me" : "<<YOUR_IP>>:27017",
"ok" : 1
}
rs0:SECONDARY> rs.add('<<YOUR_IP>>:27018')
{ "ok" : 1 }
rs0:PRIMARY> rs.add('<<YOUR_IP>>:27019')
{ "ok" : 1 }
Now, test with your connection string.
Connection String
mongodb://<YOUR_IP>:27017,<YOUR_IP>:27018,<YOUR_IP>:27019/<DB>?replicaSet=rs0&readPreference=secondary
Note: If you're testing in a local system, then remove --bind_ip_all option for security reasons.
Options
--port: Specify port
--dbpath: Path where the mongod instance stores its data
--replSet: Replica set name
--bind_ip_all: Allow remote connections

Mongodb problem_could not connect to server

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

Mongodb can't connect to localhost but can connect to localhost's IP address

If I try to run mongodb shell with the mongo command alone, I get:
Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84
exception: connect failed
However, if I stipulate localhost's LAN IP address like this:
mongo 10.10.5.90
...it connects fine.
Any clues??
Do you have a bind_ip set in your mongodb.conf (or startup script)? edit for clarity A bind_ip setting limits the IP it will listen on to that IP only.
See the IP Address Binding section: http://www.mongodb.org/display/DOCS/Security+and+Authentication
If not, do you have any firewall rules blocking the localhost access? That would be kind of strange, but I can't think of another reason why it wouldn't work while the LAN IP would work.
This error could also appear if mongodb was not properly shutdown. If you type sudo start mongodb and if it gives a new process id on every execution then your mongodb was not properly shutdown. To resolve this type
sudo rm /var/lib/mongodb/mongod.lock
sudo -u mongodb mongod -f /etc/mongodb.conf --repair
sudo start mongodb
Just follow all these steps to solve this problem
Step 1: Remove lock file.
sudo rm /var/lib/mongodb/mongod.lock
Step 2: Repair mongodb.
mongod --repair
Step 3: start mongodb.
sudo start mongodb
or
sudo service mongodb start
Step 4: Check status of mongodb.
sudo status mongodb
or
sudo service mongodb status
Step 5: Start mongo console.
mongo
I added localhost along with the ip I had in bind_ip while starting mongo and it solved my problem, for example:
bin/mongod --dbpath data --logpath mongo.log --fork --bind_ip localhost,10.100.1.2
For people using MongoDB Compass on VMWare windows:
C:\Program Files\MongoDB\Server\5.0\bin by default is the Mongo server directory. You can change bindIp and port in mongod.cfg
Also make sure to check if the mongo server service is running:
win + r > services.msc > MongoDB Server (MongoDB) > Rightclick > Start.
Should save some time, hope it helps.
Edit your mongod.conf as follow
bindIp: 0.0.0.0,localhost
It works for me.

Configuring Master Slave config for MongoDB on single Ubuntu Machine

I want to setup Master Slave config for Mongo DB on my Ubuntu Machine. I can see the setting to do this in /etc/mongodb.conf but that would make my DB either Master or Slave.
Is there any way I can run two different server on same machine and use one as Master and other as slave. I want to do this for testing purpose.
The best way would be to not use /etc/init.d/* scripts and use the good old command line. Make sure mongodb is not running on your machine. Locate the folder where binaries are installed, and then from command line run:
./mongod --dbpath /path_to_master_db_files --master --logpath /path_to_logs/master.log --port 27017 --fork
Then open up another terminal, navigate to the same folder and run this:
./mongod --dbpath /path_to_slave_db_files --slave --logpath /path_to_logs/slave.log --port 27018 --source=localhost:27017 --fork
And there you go - you should have a master running on 27017, and slave running on 27018. Hope this helps.
BTW, I am assuming you are not running this configuration in production, and only want to try it out on your local instance.
For testing purposes you can start both instances manually, take a look at this link. Quoting from the docs:
IMPORTANT
Replica sets replace master-slave replication for most use cases. If possible, use replica sets rather than master-slave replication for all new production deployments. This documentation remains to support legacy deployments and for archival purposes only.
Locate the folder where the MongoDB binaries are located:
mkdir -p ../master
./mongod --dbpath ../master --master --logpath ../master/master.log --port 27017
Then open up another terminal, navigate to the same folder and run this:
mkdir -p ../slave
./mongod --dbpath ../slave --slave --logpath ../slave/slave.log --port 27018 --source=localhost:27017