how to keep read and write separate mongo replicat set - mongodb

bin>mongod --dbpath "xx" --port 27017 --replSet replset/127.0.0.1:27018
bin>mongod --dbpath "yy" --port 27018 --replSet replset/127.0.0.1:27017
bin>mongod --dbpath "zz" --port 27019 --replSet replset/127.0.0.1:27017
client driver how use to separate read/write?
mongodb://host1,host2,host3/

By default, MongoDB always reads from and writes to the primary of a replica set.
Each driver provides a slaveOkay option that allows you to read from something other than the primary.
You will need to look at the documentation for your specific language to see how slaveOkay is used.

Related

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.

Sharding Mongo Surprised to discover

I am trying to run a sharding in mongo. I ran this commands:
mongod --rest --shardsvr --port 10000 --dbpath data/localhost10000 --logpath data/localhost10000/log
mongod --rest --port 10002 --dbpath data/localhost10002 --logpath data/localhost10002/log
mongos --port 10003 --configdb localhost:10002 > run_routing_service_log
mongo localhost:10003
use admin
db.runCommand({addshard:"localhost:10000", name:"shard10000"});
db.runCommand({addshard:"localhost:10001", name:"shard10001"});
use test_sharding
sh.enableSharding("test_sharding")
db.people.ensureIndex({"zip": 1})
db.people.insert({"name": "a1", "password": "a1", .... )
sh.status()
I am obtain this error:
Surprised to discover that localhost:10002 does not believe it is a config server
Add --configsvr argument in the second mongod command as below.
mongod --rest --port 10002 --dbpath data/localhost10002 --logpath data/localhost10002/log
This should start the configserver on port #10002. However, you might face another issue related to quorum as 3 config-servers are recommended by mongodb official documentation that can be started as the above command on different ports.
Also, I don't see that you have initialized the mongodb shard. That might also create an issue in recognizing correct shard configurations. Following link might be helpful.
http://www.mongodbspain.com/en/2015/01/26/how-to-set-up-a-mongodb-sharded-cluster/

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

mongo sharding across multiple machines connection possible through identity file (.pem)

Some example illustrating mongo sharding across multiple machines .connection among them is possible through identity file (.pem).
use of --keyFile will accomplish connection across ssh-secured machines.
Generate .pub file from ssh-keygen
ssh-keygen
Now you can use that to make mongo interactions
for eg. connectToMongo.pub is my keyfile
mongod --rest --configsvr --dbpath path --keyFile connectToMongo.pub --port port_number
mongod --rest --shardsvr --replSet replica_name --dbpath path --keyFile connectToMongo.pub --port port_number
mongos --configdb ip:port_number --keyFile connectToMongo.pub --port port_number
You can enable --keyFile authentication for your mongod/mongos servers, using your .pem file as the --keyFile value.
Note that use of --keyFile also enables the general "secure mode" requiring authentication for all user and application connections.