§2023-08-07
by ChatGPT
Q: In Mongodb I have two shards set up runnig on port 27995 and 27993. replSetName is ShardA and ShardB. How to set up shards.
It looks like you're already connected to the MongoDB instance for ShardB using the mongosh shell. To initialize sharding for a specific database, follow these steps:
Select the Database:
In the mongosh shell, switch to the database you want to shard. For example, if you want to shard the mydb database, use the following command:
javascript
use mydb
Enable Sharding for the Database: After selecting the database, enable sharding for that database using the sh.enableSharding() command. For example:
javascript
sh.enableSharding("mydb")
Choose a Sharding Key: You will need to specify a sharding key for the collection you want to shard. The sharding key determines how data is distributed across the shards. Choose a field that will evenly distribute data and align with your application's access patterns.
Shard the Collection: Once you've chosen a sharding key, shard the desired collection using the sh.shardCollection() command. Replace "mycollection" with the actual name of your collection, and "sharding_key" with the field you've chosen as the sharding key. For example:
javascript
sh.shardCollection("mydb.mycollection", { "sharding_key": "hashed" })
Note: Replace "hashed" with the appropriate sharding strategy based on your chosen sharding key.
That's it! You've initialized sharding for a specific database and collection in your MongoDB sharded cluster. Make sure to adjust the database name, collection name, and sharding key according to your actual setup and requirements.
Enable Sharding and Add Shards:
Connect to the mongos router using the mongosh shell, and run the following commands to enable sharding and add the shards:
// Inside the mongosh shell
sh.enableSharding("your_database_name");
sh.addShard("ShardA/localhost:27995");
sh.addShard("ShardB/localhost:27993");
With these steps, you've configured sharding with two shards (ShardA and ShardB) using the mongosh shell. Remember to replace "your_database_name" with the actual name of the database you want to shard.
Data Distribution
-
Shard Key
- MongoDB shards at the collection level. You choose which collection(s) you want to shard. MongoDB uses the shard key to distribute a collection’s documents across shards. MongoDB splits the data into “chunks”, by dividing the span of shard key values into non-overlapping ranges. MongoDB then attempts to distribute those chunks evenly among the shards in the cluster.
-
Sharding Strategy
- MongoDB supports two sharding strategies for distributing data across sharded clusters:
- Ranged Sharding
- Hashed Sharding
- MongoDB supports two sharding strategies for distributing data across sharded clusters:
I have two shards eith rplicaSet ShrdA and ShardB running on port 27995 27993. as follows
- ShardA
Machine | IP | port | OS | rsplicationSet |
---|---|---|---|---|
n2Mnjaro-01.yushei.net | 192.168.48.237 | 27995 | ManJaro | ShardA |
hc4bookworm.yushei.net | 192.168.48.239 | 27995 | Debian | ShardA |
hc4Llunar.yushei.net | 192.168.48.243 | 27995 | Ubuntu | ShardA |
- ShardB
Machine | IP | port | OS | rsplicationSet |
---|---|---|---|---|
n2Mnjaro-01.yushei.net | 192.168.48.237 | 27993 | ManJaro | ShardB |
hc4bookworm.yushei.net | 192.168.48.239 | 27993 | Debian | ShardB |
hc4Llunar.yushei.net | 192.168.48.243 | 27993 | Ubuntu | ShardB |
- And config server replicaSet configServer running on port 27997
Machine | IP | OS | mongod/mongos Version | Replication Set | momgosh Avialbale | configsvr/replSetName |
---|---|---|---|---|---|---|
n2Mnjaro.yushei.net | 192.168.48.245 | odroid-n2+, ManJaro | 7.0.0-rc8 | N/A | 1.10.1 no ssl | 27997, "configServer" |
hc4Lunar.yushei.net | 192.168.48.243 | Odroid-hc4 Ubuntu Jammy | 7.0.0-rc8 | N/A | 1.10.1 no ssl | 27997. "configServer" |
orpi5jammy.yushei.net | 192.168.48.247 | orgPi5 Ubuntu Jammy | 7.0.0-rc8 | N/A | 1.10.1 no ssl | 27997, "configServer" |
How to use mongos to coonect to it and and setup my Learn database on the ShardA, ShardB?