Not running with replSet option MongoDB - 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?

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"

Configure a Replica Set in local and on a cloud-server

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

Sharding on single machine

I want to run shard on my local server.
please help me step by step.
I want to create multi instance of mongod on my cpu(16 core).
By the way, I'm Collection that consist of 7 million documents, whether they went missing when I run shard?
I use this script for creating 3 shard on one replicaSet:
# clean everything up
echo "killing mongod and mongos"
killall mongod
killall mongos
echo "removing data files"
rm -rf /media/mongo/data/config
rm -rf /media/mongo/data/shard*
rm -rf /data/config
rm -rf /data/shard*
# For mac make sure rlimits are high enough to open all necessary connections
ulimit -n 2048
# start a replica set and tell it that it will be shard0
mkdir -p /media/mongo/data/shard0/rs0
mongod --replSet s0 --logpath "/var/log/mongodb/s0-r0.log" --dbpath /media/mongo/data/shard0/rs0 --port 37017 --fork --shardsvr --smallfiles
sleep 5
# connect to one server and initiate the set
mongo --port 37017 << 'EOF'
config = { _id: "s0", members:[
{ _id : 0, host : "localhost:37017" },
]};
rs.initiate(config)
EOF
# start a replicate set and tell it that it will be a shard1
mkdir -p /media/mongo/data/shard1/rs0
mongod --replSet s1 --logpath "/var/log/mongodb/s1-r0.log" --dbpath /media/mongo/data/shard1/rs0 --port 47017 --fork --shardsvr --smallfiles
sleep 5
mongo --port 47017 << 'EOF'
config = { _id: "s1", members:[
{ _id : 0, host : "localhost:47017" },
]};
rs.initiate(config)
EOF
# start a replicate set and tell it that it will be a shard2
mkdir -p /media/mongo/data/shard2/rs0
mongod --replSet s2 --logpath "/var/log/mongodb/s2-r0.log" --dbpath /media/mongo/data/shard2/rs0 --port 57017 --fork --shardsvr --smallfiles
sleep 5
mongo --port 57017 << 'EOF'
config = { _id: "s2", members:[
{ _id : 0, host : "localhost:57017" },
]};
rs.initiate(config)
EOF
# now start 3 config servers
rm cfg-a.log cfg-b.log cfg-c.log
mkdir -p /media/mongo/data/config/config-a /media/mongo/data/config/config-b /media/mongo/data/config/config-c
mongod --logpath "/var/log/mongodb/cfg-a.log" --dbpath /media/mongo/data/config/config-a --port 57040 --fork --configsvr --smallfiles
mongod --logpath "/var/log/mongodb/cfg-b.log" --dbpath /media/mongo/data/config/config-b --port 57041 --fork --configsvr --smallfiles
mongod --logpath "/var/log/mongodb/cfg-c.log" --dbpath /media/mongo/data/config/config-c --port 57042 --fork --configsvr --smallfiles
# now start the mongos on port 27018
rm mongos-1.log
sleep 5
mongos --port 27018 --logpath "/var/log/mongodb/mongos-1.log" --configdb localhost:57040,localhost:57041,localhost:57042 --fork
echo "Waiting 60 seconds for the replica sets to fully come online"
sleep 60
echo "Connnecting to mongos and enabling sharding"
# add shards and enable sharding on the test db
mongo --port 27018 << 'EOF'
db.adminCommand( { addshard : "s0/"+"localhost:37017" } );
db.adminCommand( { addshard : "s1/"+"localhost:47017" } );
db.adminCommand( { addshard : "s2/"+"localhost:57017" } );
db.adminCommand({enableSharding: "IBSng"});
EOF
sleep 5
echo "Done setting up sharded environment on localhost"
But I don't know that how to add shard key on my collection.
I speed write/read(I/O) is very important.
Sharding will not help improve query performance if it is done on a single machine which has huge number of documents(7 million is not small).
Reason : MongoDB uses memory mapped files which means copy of your data and indexes is stored in RAM and whenever there is a query it fetches it from the RAM itself. In the current scenario your queries are slower because your data + indexes size is so large that it will not fit in RAM , hence there will be lot of I/O activity to get data from disk which is the bottleneck.
What else can be done to improve query performance (incase Sharding is not an option):
Increase RAM on your machine
Use indexes
Redesign schema
Note - Even if you implement the above points, there is a limit to how much query performance can be improved, with sharding linear scaling of read/write throughput can be expected.

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 .