Error adding new shard from mongos shell - mongodb

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!

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

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

MongoDB: sync dbs from master to replica set

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.

why can't I use IP address of locally running mongod server to add it to mongodb replication set

I am having 3 mongod servers running locally on different ports as with below command
mongod.exe --port 27017 --replSet rs0 --dbpath C:\data\db1 --smallfiles --oplogSize 128
mongod.exe --port 27018 --replSet rs0 --dbpath C:\data\db2 --smallfiles --oplogSize 128
mongod.exe --port 27019 --replSet rs0 --dbpath C:\data\db3 --smallfiles --oplogSize 128
this will start three mongoDB server locally.
Now to to configure the replication set I used the following commands
rsconf = { _id : rs0,
members: [
{_id:1, host:"localhost:27017"}
]
}
then to start
rs.initiate(rsconfig)
now to add other members
rs.add({_id:2, host: "localhost:27018"})
works fine however the following
rs.add({_id:2, host: "10.212.71.116"})
throws an exception -
{
"errmsg" : "exception: can't use localhost in repl set member names exce
pt when using it for all members",
"code" : 13393,
"ok" : 0
}
I am newbie to mongoDB hence it seems more proper for me to use IP address(for localhost) than localhost. Why is this so in mongoDB?
while going through documentation for mongodb sharding I found following statement
Because all components of a sharded cluster must communicate with each other over the network, there are special restrictions regarding the use of localhost addresses:
If you use either “localhost” or “127.0.0.1” as the host identifier, then you must use “localhost” or “127.0.0.1” for all host settings for any MongoDB instances in the cluster. This applies to both the host argument to addShard and the value to the mongos --configdb run time option. If you mix localhost addresses with remote host address, MongoDB will produce errors.
on page "http://docs.mongodb.org/manual/core/sharded-cluster-architectures/#sharding-requirements-infrastructure"
This implies that when you are in test environment and do not want to add hosts other than localhost then you can go with the approach of using localhost, otherwise prefer using IP address
I had the same problem. After rs.initiate() look at the me field in printed result of this command. It's the name of your computer. For me it was Apollo-17:27001 . Having this information add other 2 servers: >rs.add("Apollo-17:27002") etc. Ensure that everything is ok: in console you will see { "ok" : 1 } . That's all.