MongoDB replica set on single machine - mongodb

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

Related

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

How the lock file is shared among the various mongods

I am trying to setup a shard and replica set.
My assumption procedure be like this :
Start a replica set (let it be only one, just for testing)
mongo then initiate replica
Start a config server (again let it be one, just for testing)
Start a shard server (again let that be just one)
Add the shard and enable sharding via mongo
What i did :
mongod --replSet rs0 --dbpath data/rs0-0 --unixSocketPrefix data/rs0-0
mongo then rs.initiate()
mongod --configsvr --dbpath data/rs0-0 --unixSocketPrefix data/rs0-0
-- now stucked at step 3.
Error i find is
2017-05-22T20:00:13.857+0530 [initandlisten] exception in initAndListen: 10310 Unable to lock file: data/rs0-0/mongod.lock. Is a mongod instance already running?, terminating
What i have tried :
i have tried with different directories for --unixSocketPrefix options, but each time it hits data/rs0-0/mongod.lock the same file. So it did not worked
It seems simple issue but unable to figure out how the lock file is shared among the various mongods (whether it be config server, or replica set, or shard server)
I am on mongodb 2.6.12
You need to start mongod and config server with different dbpath options.
You can follow the following steps:
mongod --replSet rs0 --dbpath data/rs0-0 --unixSocketPrefix data/rs0-0
or
mongod --replSet rs0 --dbpath data/rs0-0
mongo then rs.initiate()
mkdir -p /data/configdb (grant required permission recursively)
mongod --configsvr --dbpath /data/configdb --port 27019

How to get user list from replicaset instance?

I have setup a replica set which includes three members. Below is the three instances launch command:
501 36057 1 0 4:22pm ?? 0:05.02 mongod --replSet replset --dbpath /Users/joey/tmp/replica/replset/rs1/db --logpath /Users/joey/tmp/replica/replset/rs1/mongod.log --port 28017 --logappend --fork
501 36060 1 0 4:22pm ?? 0:04.82 mongod --replSet replset --dbpath /Users/joey/tmp/replica/replset/rs2/db --logpath /Users/joey/tmp/replica/replset/rs2/mongod.log --port 28018 --logappend --fork
501 36063 1 0 4:22pm ?? 0:04.86 mongod --replSet replset --dbpath /Users/joey/tmp/replica/replset/rs3/db --logpath /Users/joey/tmp/replica/replset/rs3/mongod.log --port 28019 --logappend --fork
When I connect to one of this member and run db.getUsers(), I will get an empty array as below:
$ mongo --port 28017
MongoDB shell version v3.4.0
connecting to: mongodb://127.0.0.1:28017/
MongoDB server version: 3.4.0
replset:PRIMARY> db.getUsers()
[ ]
I wander how to get user list in replicaset.
When I connect to one of this member and run db.getUsers(), I will get an empty array
This indicates you haven't set up any users yet. See Enable Authentication in the MongoDB manual.
Below is the three instances launch command
Your launch commands don't include the --keyfile parameter required to enable access control with a replica set. See Enforce Keyfile Access Control in a Replica Set.
For a full reference of recommended security measures (and links to relevant tutorials), see: MongoDB Security Checklist.

How to shut down replica set in mongodb?

I use ubuntu.
I start three mongod replica set as follows:
$ mongod --replSet setname --logpath "1.log" --dbpath /data/rs1 --port 27017 --smallfiles --fork
$ mongod --replSet setname --logpath "2.log" --dbpath /data/rs2 --port 27018 --smallfiles --fork
$ mongod --replSet setname --logpath "3.log" --dbpath /data/rs3 --port 27019 --smallfiles --fork
How can I shut them down?
Run the following commands from the Unix shell:
mongo --port 27017 --eval 'db.adminCommand("shutdown")'
mongo --port 27018 --eval 'db.adminCommand("shutdown")'
mongo --port 27019 --eval 'db.adminCommand("shutdown")'
I don't think the accepted answer is correct. After some googling, I found this in mongodb-user group to be the best answer.
To shutdown:
- Run db.runCommand({ replSetFreeze: numOfSeconds }) on secondaries to
prevent from promoting to primary
- Run rs.stepDown(seconds) on the primary; it will check to make sure
at least one of the secondaries is sufficiently caught up to oplog
before stepping down. Choose a reasonably long wait time.
- Once everything is a secondary, db.shutdownServer() on everything
To start up:
- Run rs.freeze(seconds) on both secondaries with a lengthy timeout (say,
1-2 minutes)
- Bring up primary
- Use stepDown to fix in case a secondary somehow becomes primary
Simply shutting down is not enough, you have to prevent secondary nodes from promoting to primary.
1) Login to mongo shell on Secondary servers
2) Stop the secondary servers by using below command:
use admin
db.shutdownServer()
2] Go to Linux shell- on secondary servers and type below command:
sudo service mongod stop
For more information, Please check the below link:
http://www.tutespace.com/2016/03/stopping-and-starting-mongodb.html
Use ps -ef | grep mongod, get pids for mongod with port numbers 27017-27019, and kill processes with thats pids.
mongod --port 27017 --shutdown
mongod --port 27018 --shutdown
mongod --port 27019 --shutdown
By the way, if there is any possibility you will use sharding in the future, it would be best to avoid ports 27018 and 27019. Those are default ports for some components of a shard cluster.
In MongoDB 3.2 on Windows, I am using the following sequence:
mongo mongodb://localhost:27019/admin --eval "db.shutdownServer({timeoutSecs: 10})"
mongo mongodb://localhost:27018/admin --eval "db.shutdownServer({timeoutSecs: 10})"
mongo mongodb://localhost:27017/admin --eval "db.shutdownServer({timeoutSecs: 10})"
shutdown secondaries first to prevent rollbacks.
mongo admin --port <port> --eval "<db.auth if needed>;db.shutdownServer()"
source: mongodb university course
This is an old question but I still think that I should answer this.
Run the following command in the unix shell
ps -ef | grep mongo
This would give you a list of pid's (process ids) corresponding to the port 27017, 27018 and 27019. You can use the pid's to kill all the members of replicaset

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