Configure a Replica Set in local and on a cloud-server - 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: "localhost:27117"
}]
}
rs.initiate( rsconf )
rs.add("localhost:27118")
Until now all was correct. But when I tryed to add the server-instance a error occurred:
rs.add("myServer.it:27119")
"errmsg" : "Either all host names in a replica set configuration must be localhost references, or none must be; found 2 out of 3",
So I'm thinking that I cannot set this configuration or what?

While trying to add different member in a replSet, you should avoid using localhost(specially when your servers are located at different environments).
Please try below options:
Remove existing replSet member: rs.remove("localhost:27118")
Try to add the server back with host ip(Local Server IP): rs.add("your_server_IP:27118")
Add the cloud server with Public IP: rs.add("Cloud_server_public_IP:27119")
Also ensure 27117,27118 and 27119 ports are opened from both Localhost and Cloud server.
Thanks,
Deep

Related

mongos error "mirrored config server connections are not supported"

Is something wrong with this?
$ sudo mongos --logpath "mongos-1.log" --configdb localhost:57040,localhost:57041,localhost:57042 --fork
FailedToParse: mirrored config server connections are not supported; for config server replica sets be sure to use the replica set connection string
try 'mongos --help' for more information
I had the same problem as you just have to update your --configdb to "repSetConfigName/localhost:57040,localhost:57041,localhost:57042"
here is an example :
this is my configuration of the config server:
mongod --configsvr --dbpath cfg0 --replSet conf --port 27050 --fork --logpath log.cfg0 --logappend
mongod --configsvr --dbpath cfg1 --replSet conf --port 27051 --fork --logpath log.cfg1 --logappend
mongod --configsvr --dbpath cfg2 --replSet conf --port 27052 --fork --logpath log.cfg2 --logappend
then I used
# connect to any of the mongod process on port 27050, 27051, 27052
mongo --port 27050
rs.initiate(
{
_id: "conf",
configsvr: true,
members: [
{ _id : 0, host : "localhost:27050" },
{ _id : 1, host : "localhost:27051" },
{ _id : 2, host : "localhost:27052" }
]
}
)
fianlly
mongos --configdb "conf/localhost:27050,localhost:27051,localhost:27052" --fork --logpath log.mongos0 --port 27200
Just appendix to #sylia-baraka answer... If you are using MongoDB 3.4 or newer you need to use CSRS instead of SCCC, read more here.
In version 3.4, MongoDB removes support for SCCC config servers. Before you can upgrade your sharded clusters to 3.4, you must convert your config servers from SCCC to CSRS. To convert to CSRS, follow the procedure to change your 3.2.x SCCC to 3.2.x CSRS.
configdb should be like this :
"your_replSetName/host:port,host:port,host:port"

Not running with replSet option MongoDB

I am trying to create a small replica set on my computer in Windows.
Here is what I have done so far:
I have made three directories in data by
mkdir \data\rs1 \data\rs2 \data\rs3
Now, I am starting three mongo instances:
start mongod --replSet m101 --logpath 1.log --dbpath \data\rs1 --port 27017 --smallfiles --oplogSize 64
start mongod --replSet m101 --logpath 2.log --dbpath \data\rs2 --port 27018 --smallfiles --oplogSize 64
start mongod --replSet m101 --logpath 3.log --dbpath \data\rs3 --port 27019 --smallfiles --oplogSize 64
Now I am starting mongod.exe by command line and connect to a mongo shell by typing
mongo --port 27017
Here is my replica set:
config = { _id: "m101", members:[
{ _id : 0, host : "localhost:27017"},
{ _id : 1, host : "localhost:27018"},
{ _id : 2, host : "localhost:27019"} ]
};
Now when I type rs.initiate(config); , It shows this node was not started with replSet option.
I also tried to run command line options on admin
use admin
db.runCommand("getCmdLineOpts")
and I don't see any --replSet options in there.
I get
{
"argv" : [ "mongod" ],
"parsed" : {},
"ok" : 1
}
I know many questions related to this problem are available and I tried many of them but none of them worked. I am new to this and learning this from one of the courses of MongoDB University. Please can anyone tell me what am I doing wrong?

MongoDB: can't shard on localhost because of shard socket exception

I'm trying the MongoDB's shard tutorial, for some simple testing (proof-of-concept project).
I want to try sharding on a single machine. Is this possible and/or does it make any sense?
When I follow the guide steps, it does not work.
First, I start the mongod configsrv database instances on my machine:
bin/mongod --configsvr --dbpath $BASEDIR/data/configdb --port 27019 &
bin/mongod --configsvr --dbpath $BASEDIR/data/configdb1 --port 27020 &
bin/mongod --configsvr --dbpath $BASEDIR/data/configdb2 --port 27021 &
Then, I start the mongos instances so that they "bind" to the config servers:
HOST=$(uname -n) # my machine's name
# starts on default poort 27017
bin/mongos --configdb $HOST:27019,$HOST:27020,$HOST:27021
Until here, everything looks good.
Now I want to add a Shard to the cluster:
bin/mongo --host $(uname -n)
It enters the MongoDB shell.
connecting to: my.machine.name:27017/test
But when I try to add a new shard, I have the following error:
mongos> sh.addShard( "rs1/my.machine.name:27017" )
{
"ok" : 0,
"errmsg" : "couldn't connect to new shard socket exception [CONNECT_ERROR] for rs1/my.machine.name:27017"
}
I have tried with ip, machine's alias, localhost ... nothing seems to work.
Anyone could help me on this? Maybe I'm missing a point.
Thanks in advance
I had the same problem, you have to run
mongod &
on your each of your shards
and then you can call
sh.addShard( "rs1/my.machine.name:27017" )
Its possible, follow below sequence.
lets say we want to create a cluster with 3 shards each with 1 replica on same machine.
Start first mongod server (shard 1 with replica 1)with a replica set named s1
Start second mongod server (shard 2 with replica 1)with a replica set named s2
Start third mongod server (shard 3 with replica 1)with a replica set named s3
mkdir -p /data/shard0/rs0 /data/shard0/rs1 /data/shard0/rs2
/bin/mongod --replSet Sv1 --logpath "Sv1-r0.log" --dbpath /data/shard0/rs0 --port 37017
/bin/mongod --replSet Sv2 --logpath "Sv2-r0.log" --dbpath /data/shard0/rs1 --port 47017
/bin/mongod --replSet Sv3 --logpath "Sv3-r0.log" --dbpath /data/shard0/rs2 --port 57017
This will start 3 mongod servers/shards with one replica for each shard
/bin/mongo --port 37017 << 'EOF'
config = { _id: "Sv1", members:[
{ _id : 0, host : "<ip>:37017" }]};
rs.initiate(config)
EOF
This will add replicas to replica set Sv1. This should be done for each shard i.e. on each port 47017 and 57017
Now we have cluster ready with shards and replicas but no one knows other shards and replicas, thus we need config server
/bin/mongod --logpath "cfg-a.log" --dbpath /data/config/config-a --port 57040 --fork --configsvr
Then start mongos for admin access,
/bin/mongos --logpath "mongos-1.log" --configdb 10.88.66.218:57040
Now add shards to our cluster,
/bin/mongo <<'EOF'
db.adminCommand( { addShard : "Sv1/"+"<ip>:37017" } );
db.adminCommand( { addShard : "Sv2/"+"<ip>:47017" } );
db.adminCommand( { addShard : "Sv3/"+"<ip>:57017" } );
use db
db.createCollection("col1")
db.adminCommand({enableSharding:"db"}) db.adminCommand({shardCollection: "db.col1", key: {_id:"hashed"}});
EOF
And that's it.... Cluster should be up with 3 shards. You can check with sh.status() command on mongos.

MongoDB server is not running with replset

I do a MongoDB homework and follow the steps, but I faced the problem " server is not running with replset"
I do the step is :
`"start mongod --replSet m101 --logpath 1.log --dbpath \data\rs1 --port 27017 --smallfiles --oplogSize 64
start mongod --replSet m101 --logpath 2.log --dbpath \data\rs2 --port 27018 --smallfiles --oplogSize 64
start mongod --replSet m101 --logpath 3.log --dbpath \data\rs3 --port 27019 --smallfiles --oplogSize 64
Now connect to a mongo shell and make sure it comes up.
mongo --port 27017
Now you will create the replica set. Type the following commands into the mongo shell:
config = { _id: "m101", members:[`
{ _id : 0, host : "localhost:27017"},
{ _id : 1, host : "localhost:27018"},
{ _id : 2, host : "localhost:27019"} ]
};
rs.initiate(config);"
I do not know where is the problem, someone said the problem maybe is another mongod is running. But I have not any other mongod running in the same time.
Thank you!
It happened the same to me as well. The problem was that I have runned mongod without any parameters before I started launching the nodes.
First kill all the mongo, mongod and mongos instances to guarantee the environment is clear.
ps -ef | grep -i 'mongo'
sudo service mongod stop
The problem cause is simple. The default port for mongod and mongos is 27017. So, what happened to me was that the first replica node was never created. The following command was connecting me to a default mongod instance:
mongo --port 27017
I hope this solves your problem. ;)
Check what command line options the mongod you are connected to in the shell is running with:
> use admin
switched to db admin
> db.runCommand("getCmdLineOpts")
{
"argv" : [
"mongod",
"--dbpath",
"/data/db",
"--replSet",
"rs0"
],
"parsed" : {
"replication" : {
"replSet" : "rs0"
},
"storage" : {
"dbPath" : "/data/db"
}
},
"ok" : 1
}
Do you see the replSet option?
You can also try a different way of initiating the replica set. Instead of typing in the configuration and passing it to rs.initiate(), just run rs.initiate(). It will set up default config with one member- the server you are connected to in the shell. Then you can add the other two servers. See the tutorial for more information.
For MacOS Homebrew setup & default configuration
Edit default configuration mongodb file nano /opt/homebrew/etc/mongod.conf
Insert the following piece of code
replication:
replSetName: "rs0"
Save and restart the mongodb service brew services restart mongodb-community#6.0
Exec the following command in mongosh shell rs.initiate()
$ mongosh
> use admin
admin> rs.initiate()
success response:
{
info2: 'no configuration specified. Using a default configuration for the set',
me: '127.0.0.1:27017',
ok: 1
}

mongoDB set name does not match

I have got 3 mongod demo replicates servers running on my machine. I have used following command to create replicated server:
F:\>mongod --replSet test2 --dbpath 2 --port 27112 --oplogSize 50 --logpath log.2 --logappend
all output going to: log.2
Like this I have test1 on 27111 port, test2 on 27112 port and test3 on 27113 port. Yet at configuration I am getting an error:
cfg = {
"_id" : "test1",
"members" : [
{
"_id" : 0,
"host" : "localhost:27111"
},
{
"_id" : 1,
"host" : "localhost:27112"
},
{
"_id" : 2,
"host" : "localhost:27113"
}
]
}
> rs.initiate( cfg )
{
"errmsg" : "couldn't initiate : set name does not match the set name host localhost:27112 expects",
"ok" : 0
}
Now what do I have to do to make it match ?
All the sets I would run should have the same name.
For an instance if I run
mongod --replSet test2 --dbpath 2 --port 27112 --oplogSize 50 --logpath log.2 --logappend
all output going to: log.2
Then the other mongo servers I would intend to be in that set, should have the same name test2
The name or variable used in "cfg" = { "_id":="test1" }
should confirm with the name used in the following for parameter replSet for all the members in that replication set.
Then things will work smoothly for you
"start mongod --replSet "test1" --logpath "1.log" --dbpath C:\Replica\rs01 --port 27017 --oplogSize 64
Good luck
Rao
The steps below worked for me:
mongod --port 27017 --dbpath "C:\MongoDB\data01" --replSet rs0 --bind_ip localhost
mongod --port 27018 --dbpath "C:\MongoDB\data02" --replSet rs0 --bind_ip localhost
These two lines start two different mongo instances in different ports.
On the second instance, I have added replSet=rs0 in mongod.conf.
Then add the secondary in rs0 node by this command:
rs.add("localhost:27018")
Try with the name of your machine or 127.0.0.1. It's a best practice to use the DNS name.
MongoDB replicaset doesn't accept localhost. Try using the actual machine name while adding the node to the replicaset as following:
rs.add("MY_MACHINE_NAME:27017");
When adding the replica sets, the --replset should be same for all three. For example if for primary it was "rs01", it should be same for other two also .