Configure a Replica Set - mongodb

I'm trying to configure a Replica Set with 2 members in local e 1 member on a cloud-server.
I started the two instance from local in this way:
mongod --port 27117 --dbpath mongodb/rs0-0 --logpath mongodb/rs0-0/mongo.log --replSet rs0 --fork
mongod --port 27118 --dbpath mongodb/rs0-1 --logpath mongodb/rs0-1/mongo.log --replSet rs0 --fork
and then I started the instance on my cloud-server (after opened the port):
mongod --port 27119 --dbpath mongoRS/rs0-2 --logpath mongoRS/rs0-2/mongo.log --replSet rs0 --fork
So, I started the server to configure the Replica Set:
mongo --port 27117
rsconf = {
_id: "rs0",
members: [{
_id: 0,
host: "myLocalIP:27117"
}]
}
rs.initiate( rsconf )
rs.add("myLocalIP:27118")
rs.add("myServerIP:27119")
So I tried to do Test Connections in both directions:
from my local's shell:
mongo --host myCloudServer --port 27119
I enter in the server but it's not the Replica Set instance
from cloud-server's shell, it fails:
mongo --host myLocalIP --port 27017
where is the error? Thanks

I take it back about test connections.
Both connections from myLocal to remote-myCloud and vice-versa was refused

Related

Mongodb start a local sharded cluster

Trying to set up a local sharded cluster for testing (mongo 3.6). There are a number of scripts online (e.g here, here) all of which seem to fail for me.
running (cribbed mostly from the 2nd example above):
start the config server:
mongod --configsvr --replSet csrs --dbpath /data/db/configdb --port 27018 --fork --logpath log/mongods.log
initiate the replica set on the config:
mongo localhost:27018 << !
rs.initiate({_id: "csrs", configsvr: true, members: [{_id: 0, host: "${local_ip}:27018"}]});
!
start the mongod processes:
mongod --port 27030 --dbpath /data/db/db1-0 --shardsvr --replSet rs --fork --logpath log/mongod1-0.log
mongod --port 27040 --dbpath /data/db/db2-0 --shardsvr --replSet rs --fork --logpath log/mongod2-0.log
mongod --port 27020 --dbpath /data/db/db0-0 --shardsvr --replSet rs --fork --logpath log/mongod0-0.log
initiate the replica set(s)
mongo localhost:27020 << !
rs.initiate({_id: "rs", members: [{_id: 0, host: "${local_ip}:27020"}, {_id: 1, host: "${local_ip}:27030"}, {_id: 1, host: "${local_ip}:27040"}]});
!
(note, initial script had a separate rs for each shard, which caused the same error)
start a mongos and point it to the config server:
mongos --configdb csrs/${local_ip}:27018 --port 27017 --fork --logpath log/mongos.log
log into the mongos and add shards:
e.g. sh.addShard("rs/${local_ip}:27020").
This last step is where this goes wrong, getting the error
"errmsg" : "Could not find host matching read preference { mode: \"primary\" } for set rs",
"code" : 133,
Can anyone provide any pointers on what I'm missing here?

MongoDB 3.4 Sharding Error. "Cannot accept sharding commands if not started with --shardsvr"

I have installed mongoDB 3.4.5 in centOS 7.2 machine.
I am configuring sharding in standalone mode.
Steps I followed:
For Replica Sets:
mongod --replSet rs0 --dbpath /data/srd/rs0 --logpath /data/srd/rs0/log.rs0 --port 27000 --fork --logappend --smallfiles --oplogSize 50 &
mongod --replSet rs0 --dbpath /data/srd/rs1 --logpath /data/srd/rs1/log.rs1 --port 27001 --fork --logappend --smallfiles --oplogSize 50 &
mongod --replSet rs0 --dbpath /data/srd/rs2 --logpath /data/srd/rs2/log.rs2 --port 27002 --fork --logappend --smallfiles --oplogSize 50 &
For Shards:
mongod --replSet rs0 --shardsvr --dbpath /data/srd/srd0 --logpath /data/srd/srd0/log.srd0 --port 27020 --fork --logappend --smallfiles --oplogSize 50 &
mongod --replSet rs0 --shardsvr --dbpath /data/srd/srd1 --logpath /data/srd/srd1/log.srd1 --port 27021 --fork --logappend --smallfiles --oplogSize 50 &
mongod --replSet rs0 --shardsvr --dbpath /data/srd/srd2 --logpath /data/srd/srd2/log.srd2 --port 27022 --fork --logappend --smallfiles --oplogSize 50 &
For Config server:
mongod --configsvr --replSet configReplSet --dbpath /data/srd/cfgrep0 --logpath /data/srd/cfgrep0/log.cfgrep0 --port 27006 --fork &
mongod --configsvr --replSet configReplSet --dbpath /data/srd/cfgrep1 --logpath /data/srd/cfgrep1/log.cfgrep1 --port 27007 --fork &
mongod --configsvr --replSet configReplSet --dbpath /data/srd/cfgrep2 --logpath /data/srd/cfgrep2/log.cfgrep2 --port 27008 --fork &
To start Mongos:
mongos --configdb configReplSet/sit:27006,sit:27007,sit:27008 --port 26060
I am able to login the mongos shell and also able to add the shards.
But while enabling the shard I am getting exceptions as below.
mongos> sh.enableSharding("test");
{
"code" : 193,
"ok" : 0,
"errmsg" : "Cannot accept sharding commands if not started with --shardsvr"
}
Any suggestion ?
These are steps I followed to setup sharding:
1. Start Config Server:
mongod --configsvr --dbpath fresh/config1 --port 30001 --replSet r1
Start Mongos:
mongos --configdb "r1/localhost:30001" --port 40001
Connect to mongo shell and execute these commands:
Connect to Mongo Shell :
mongo --host localhost --port 30001
Now execute these commands:
rs.initiate();
rs.status();
Start Mongo Shard Servers:
mongod --shardsvr --port 50001 --dbpath fresh/m1
mongod --shardsvr --port 50002 --dbpath fresh/m2
Connect to mongos and execute these commands:
Connect to mongos:
mongo --host localhost --port 40001
Now execute below commands:
sh.addShard('localhost:50001');
sh.addShard('localhost:50002');
sh.enableSharding('testdb');
sh.shardCollection('testdb.demo',{'name':1});
MongoDB User Manual: https://docs.mongodb.com/manual/reference/method/#sharding

mongodb replication in sharded machine

I have done Replication in one single machine with three different port(Say 27018[master],27019,27020). Also I have done Sharding in one single machine with two different port(Say 27021,27022).
Now I have to implement replication for the sharded machine port. I need to implement replication for 27021 and 27022. How can I do this? Please help me to resolve this issue.
Steps followed :
Part 1: Set up 3 port for replication
mongod --replSet rs0
mongod --port 27018 --dbpath F:\Data1 --replSet rs0
mongod --port 27020 --dbpath F:\Data2 --replSet rs0
mongo localhost:27017
rs.initiate()
rs.add("ComputerName:27018")
rs.add("ComputerName:27020")
Part 2: Now set up for sharding
mongod --configsvr --replSet configReplSet
mongod --port 27021 --dbpath F:\Data4 --replSet configReplSet
mongod --port 27025 --dbpath F:\Data5 --replSet configReplSet
mongo 127.0.0.1:27019
rs.initiate({_id:"configReplSet",configsvr:true,members:[{_id: 0,host: "127.0.0.1:27021"},{_id:1,host: "127.0.0.1:27025"}]})
When I run this code I am getting an error:
{
"ok" : 0,
"errmsg" : "No host described in new configuration 1 for replica set configReplSet maps to this node",
"code" : 93
}
So remaining steps I am not able to execute. Any idea how can I overcome this issue?
Got one method to achieve my requirement.
Steps as follows
Config Server and its replication
mongod --configsvr --dbpath F:\Data1\configdb\ --replSet rs2 --port 27017
mongod --configsvr --dbpath F:\Data2\configdb\ --replSet rs2 --port 27018
mongod --configsvr --dbpath F:\Data3\configdb\ --replSet rs2 --port 27019
mongo machineip:27017
config= {_id:"rs2", members :[{ _id:0, host:'10.18.0.225:27017'}, {_id:1, host:'10.18.0.225:27018'}, {_id:2, host:'10.18.0.225:27019'}]}
rs.initiate (config)
rs.status()
Routing Server
mongos -configdb machineip:27017 --port 27020
Shard1 and its replication
mongod --dbpath F:\Data4\db\ --shardsvr --replSet rs0 --port 27021
mongod --dbpath F:\Data5\db\ --shardsvr --replSet rs0 --port 27022
mongod --dbpath F:\Data6\db\ --shardsvr --replSet rs0 --port 27023
Shard2 and its replication
mongod --dbpath F:\Data7\db\ --shardsvr --replSet rs1 --port 27024
mongod --dbpath F:\Data8\db\ --shardsvr --replSet rs1 --port 27025
mongod --dbpath F:\Data9\db\ --shardsvr --replSet rs1 --port 27026
Replication configuration for Shard1
mongo machineip:27021
config= {_id:"rs0", members :[{ _id:0, host:'machineip:27021'},{_id:1,host:'machineip:27022'},{_id:2,host:'machineip:27023'}]}
rs.initiate(config)
rs.status()
Replication configuration for Shard2
mongo machineip:27024
config= {_id:"rs0", members :[{ _id:0, host:'machineip:27024''},{_id:1,host:'machineip:27025''},{_id:2,host:'machineip:27026''}]}
rs.initiate(config)
rs.status()
Sharding Configuration in route server
sh.status()
sh.addShard ("rs0/machineip:27021")
sh.addShard ("rs0/machineip:27024")
sh.status()
sh.enableSharding ("test")
sh.shardCollection ("test.stud", {id: 1})
sh.status()
Hope this will help :-)

Mongodb Sharding - no such command: 'addShard'

I'm trying to configure sharding in one machine and I keep getting an error message when trying to add shards. I'm using the following website as a reference: http://www.javahotchocolate.com/notes/mongodb-sharding.html
I'm using a single machine and using different ports to simulate the different IPs.
I have a configuration as follows:
Replica set members:
mongod --dbpath \rs1_p\db --port 27017 --replSet rs1
mongod --dbpath \rs1_s1\db --port 27018 --replSet rs1
mongod --dbpath \rs1_s2\db --port 27019 --replSet rs1
mongod --dbpath \rs1_p\db --port 47017 --replSet rs2
mongod --dbpath \rs1_s1\db --port 47018 --replSet rs2
mongod --dbpath \rs1_s2\db --port 47019 --replSet rs2
Configuring replica sets in MongoDB shell:
config =
{
_id:"rs1",
members:
[
{ _id:0, host:"computerName:27017" },
{ _id:1, host:"computerName:27018" },
{ _id:2, host:"computerName:27019" }
]
}
rs.initiate( config )
config =
{
_id:"rs2",
members:
[
{ _id:0, host:"computerName:47017" },
{ _id:1, host:"computerName:47018" },
{ _id:2, host:"computerName:47019" }
]
}
rs.initiate( config )
Launching configuration servers:
mongod --dbpath \c1\db --configsvr --port 37017
mongod --dbpath \c2\db --configsvr --port 37018
mongod --dbpath \c3\db --configsvr --port 37019
Launching sharding routers:
mongos --port 27077 --configdb computerName:37017, computerName:37018, computerName:37019
mongos --port 27078 --configdb computerName:37017, computerName:37018, computerName:37019
All okay until here, but In getting an error message in the next step
sh.addShard('rs1/computerName:27017')
I'm getting the follow error:
{
"ok" : 0.0,
"errmsg" : "no such command: 'addShard', bad cmd: '{ addShard: \"rs1/computerName:27017\" }'",
"code" : 59
}
I can not seem to get around from this error message.
Any ideas on what am I missing.
Thanks
as per documentation
Run addShard when connected to a mongos instance. The command takes the following form when adding a single database instance as a shard
when you run this connected to mongo shell you will get that error, so you need to connect to let say mongo --port 27078 and issue that command.
mongo --host <hostname of machine running mongos> --port <port mongos listens on>
manaual
more here and here

"errmsg" : "server is not running with --replSet"

i am creating replica set on my local system in windows environment
by following step
mkdir -p \srv\mongodb\rs0-0 \srv\mongodb\rs0-1 \srv\mongodb\rs0-2
mongod --port 27017 --dbpath \srv\mongodb\rs0-0 --replSet rs0 --smallfiles --oplogSize 128
mongod --port 27018 --dbpath \srv\mongodb\rs0-1 --replSet rs0 --smallfiles --oplogSize 128
mongod --port 27019 --dbpath \srv\mongodb\rs0-2 --replSet rs0 --smallfiles --oplogSize 128
and then
mongo shell type mongo --port 27017
rs.initiate()
but i am getttin error
"{ "ok" : 0, "errmsg" : "server is not running with --replSet" }"
thanks in advance
I had the same problem. Thanks to this question and previous answers I solved doing:
I checked all the running mongod processes ps -ef | grep mongo. I had 1 mongo and 2 mongod processes
I killed mongod with sudo sudo killall mongod
and at the end I started with sudo sudo mongod --port 27017 --dbpath \srv\mongodb\rs0-0 --replSet rs0 --smallfiles --oplogSize and it worked