MongoDB: sync dbs from master to replica set - mongodb

Step1- I have following collections in my main db [running on port 27017]
$mongo
$show dbs
show dbs
DB 0.078GB
admin (empty)
auditing
local
university
test
Step2- Now I am creating replica using below command
$mongod --dbpath ~/mongodb_data_dir/mongo/data0 --replSet auditlogreplySet --port 27018
$mongod --dbpath ~/mongodb_data_dir/mongo/data1 --replSet auditlogreplySet --port 27019
$mongod --dbpath ~/mongodb_data_dir/mongo/data2 --replSet auditlogreplySet --port 27020
Step3- running mongo for port 27018
$ mongo --port 27018
$show dbs
admin (empty)
local 0.328GB
Query: is it possible to sync all dbs from port 27017(main DB) to replica 27018? Please help. so that I can see all the dbs existing in main db in replica set.

As per the above post, you have just created the replica set members.
you need to add these replica members to the PRIMARY NODE first.
In main db [running on port 27017] , run the below commands :
rs.initiate(); // this command will make this NODE as PRIMARY.
// To add the secondary replica members, use the below command, run these in the same main DB shell :
rs.add (machineName:port);
// once you are done, on each of the secondary replica shell, exceute the below command
rs.slaveOk();
// once you are done, it will sync all the dbs from the primary node.

Related

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.

Error adding new shard from mongos shell

I'm learning setting up sharding in mongoDB, I plan to have 1 configsvr, 1 shardsvr and 1 mongos
I started the mongods using
mongod --configsvr --replSet foo --dbpath e:\mongodb\configsvr --port 27019
mongod --shardsvr --replSet foo --dbpath e:\mongodb\shardsvr --port 27018
I also connected to each mongods using mongo --port 27019 and mongo --port 27018 and performed rs.initiate() on each
I so started the mongos using
mongos --configdb foo/MY-PC:27019
I finally connected to the mongos using mongo I got an error after using sh.addShard('foo/MY-PC:27018')
Error
{
"ok" : 0,
"errmsg" : "Cannot add foo/MY-PC:27018 as a shard since it is part of a config server replica set",
"code" : 96
}
Please help to solve this error
Thanks all
You set foo/MY-PC as a replica to another mongo server! it's data gonna be copy of that server!
Replication is copying a server's data in an other server so if something happen to it, you wouldn't loose your data! while Sharding is breaking your data into portions so you can speed up your data access speed by accessing different servers at the same time.
So if you shard your data in two portion Data-A and Data-B and your Primary replica server keeps Data-A, then foo/MY-PC would hold the same portion(Data-A) only!

How start Mongo Instance in sharded cluster

I want to deploy a sharded cluster for this i using MongoDB manual.
first i created 3 config servers.
mongod --configsvr --dbpath /data/configdb1 --port 27019 ,
mongod --configsvr --dbpath /data/configdb2 --port 27019 ,
mongod --configsvr --dbpath /data/configdb3 --port 27019
But in second step to Start the mongos Instances i find a command like
mongos --configdb cfg0.example.net:27019,cfg1.example.net:27019,cfg2.example.net:27019
but i can't understand the meaning of
cfg0.example.net
cfg1.example.net
cfg2.example.net
So Please explain meaning of above command and how can i use this according my hostname?
Please check "Deploy a Sharded Cluster", cfg[0-3].example.net means hostnames of 3 config servers, each of mongos server should be started with all config servers configured.
This should solve your issue
[ne#server1~]$mongos configdb"repl/localhost:57040,localhost:57041,localhost:57042" --fork --logpath log.mongos0 --port 27200
about to fork child process, waiting until server is ready for connections.
forked process: 2467
Hope it helps!!!
These are are the config servers connected to the the mongos server

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