Sharding (configuration server, mongos, mongosh)

Hello there,

I am trying out sharding for the first time, so please bear with me.

I have two replica sets set up. Each replica set has one primary node and two secondary nodes. There names and ports are as follows:

Replica Set 1
Name: company1repl
Primary: port 27017
Secondary1: port 27020
Secondary2: port 27030

Replica Set 2
Name: configReplSet
Primary: port 27018
Secondary1: port 27028
Secondary2: port 27038

I want to use “Replica Set 1” for sharding, and “Replica Set 2” as the configuration server.

I tried to make “Replica Set 2” the configuration server by typing in the following in the command line, as per MongoDB documentation:
mongod --configsvr --replSet configReplSet

I’m not sure if that worked, but I moved on to the next step, which is to start a mongos instance. To do that, I typed the following in a new command line window:
mongos --configdb configReplSet/localhost:27018,localhost:27028,localhost:27038

After hitting enter, nothing happens. The cursor just keeps blinking next to the “…:27038”. So I’m thinking that didn’t work either. But since I’ve never done this before, maybe that’s what’s supposed to happen.

So I move on. I try to connect mongosh to the mongos:

mongosh localhost:27017/admin

which works. So I try to add shards there from my other Replica Set:

sh.addShard( “company1repl/localhost:27017,localhost:27020,localhost:27030” )

And I get the following error message:
Warning: MongoshWarning: [SHAPI-10003] You are not connected to a mongos. This command may not work as expected.

So I have a lot of questions here, and would be greatly if anyone can suggest how to solve them. Namely:

  1. how does one start a mongos instance? There seems to be no documentation on how to do this step by step. Am I to create a directory anywhere on the computer with a mongos.conf file? After that, what exactly needs to be in that file?
  2. How do I make sure that I’ve designated one of my replica sets as the configuration server?
  3. How do I properly connect the mongosh to the mongos?

Thanks for anything you could suggest. I should mention that I am trying to do everything via the command line for the sake of my assignment.

Hello @Sam_Vickerbond, good to know you are trying sharding. It can be little daunting if you are trying sharding for the first time. Here is the documentation / tutorial for Deploy Sharded Cluster from MongoDB Manual.

The documentation has the following steps (titled as Procedure) which will guide you to deploy a sharded cluster and shard a collection. Please follow those (the steps are detailed and are from the above link):

  1. Create the Config Server Replica Set
  2. Create the Shard Replica Sets
  3. Start a mongos for the Sharded Cluster
  4. Connect to the Sharded Cluster
  5. Add Shards to the Cluster
  6. Enable Sharding for a Database
  7. Shard a Collection
  8. Shard Key Considerations (useful to understand the importance of a shard key)
1 Like

Name: company1repl
Primary: port 27017
Secondary1: port 27020
Secondary2: port 27030

It looks like port 27017 is assigned to the primary node of your replica set.

So I move on. I try to connect mongosh to the mongos:
mongosh localhost:27017/admin

Based on the error message it looks like a mongod process is running on this node.

The default port is 27017 (if one is not specified), I would try specifying a port to run the mongos on that is different than the ones you used above (maybe 27040 or something), and then trying to connect to that port to avoid any confusion between the first replica set and your mongos for testing purposes.

1 Like

Hi @Sam_Vickerbond

Everything is correct till mongod --configsvr --replSet configReplSet

next step is to start the mongos --bind_ip option was missing in command

mongos --configdb configReplSetName/localhost:27018,localhost:27028,localhost:27038 --bind_ip <ipAddressOftheServer>

answers for your questions:

Ques 1. how does one start a mongos instance?
mongos --configdb configReplSetName/localhost:27018,localhost:27028,localhost:27038 --bind_ip

how to connect to a Mongos instance
mongosh --host

Eg:- mongosh --host 192.168.1.137

Ques 2. To check whether your replicaSet is as configuration server. just execute the above command and enter another command

sh.status(); // no warning message will be shown if you are connected to mongos and execute this command

[direct : mongos] will be shown.

//add the shard server
sh.addShard(“/localhost:27017,localhost:27020,localhost:27030”);

return success
{
shardAdded: ‘shardReplSet’,
ok: 1,
‘$clusterTime’: {
clusterTime: Timestamp({ t: 1700572413, i: 1 }),
signature: {
hash: Binary(Buffer.from(“0000000000000000000000000000000000000000”, “hex”), 0),
keyId: Long(“0”)
}
},
operationTime: Timestamp({ t: 1700572413, i: 1 })
}

Question 3 already answered with question two.