This is what I want to do:
- Create a Config Server
- Create 2 Shard
- Create a Mongos Router
I don't want to replicate my data using replica set, I want to simulate that each shard has only a part of data like it says is shard theory.
I'm following this guide, but I understood that the guide create many replicas for config server and shared server and I don't want that. So this is what I do:
Config file of Config Server:
sharding:
clusterRole: configsvr
replication:
replSetName: localhost
net:
bindIp: localhost
storage:
dbPath: /home/christian/mongo/confsvr/metadata
Execute Config Server:
mongod --config /mongo/confsvr/mongod.conf --port 27019
Now doc uses rs.initiate()
for adding a replica of config server (if I understood right), so I dropped this part.
After that, I created two config file for Shard Server.
sharding:
clusterRole: shardsvr
replication:
replSetName: localhost
net:
bindIp: localhost
storage:
dbPath: /home/christian/mongo/shard[1|2]
I started two shard:
mongod --config mongo/shard[1|2]/shard.conf --port 2703[1|2]
Created conf file for Mongos Router:
sharding:
configDB:localhost/localhost:27019
net:
bindIp: localhost
So i tried to start mongos:
mongos --config /home/christian/mongo/router/mongos.conf
But terminal print in loop this:
Unable to reach primary for set localhost
Cannot Reach any nodes for set localhost
Last steps were connecting to mongos, and add the Shards to DB
How could I obtain what I want?
Best Answer
Reference:
There are few things wrong in your approach. I suggest you first understand the concept and test every step. For example once you create your config server make sure you can connect to your
Mongod
and then only move to next step. Same is true for each of your shard.My answer is pointing to those and some more reading material so you can do this by yourself.
First of all starting from MongoDB 3.6 shards must be deployed as Replica set and not stand alone:
What you mentioned below is not possible. You will need Replica Set to use Sharding but for development/test you can have a one member Replica set.
You are using same name for Config Server, Shard1 and Shard2
Replicaset
. You need to use different name and make suremongod
is running on separate port for all instances.If all your
Mongod
is running in one host you need to mention the port in your configuration file. See example here.See the documentation for using
rs.initiate()
here.I do not see you adding your shards to the cluster. See this.