§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
Sharding Strategy
I have two shards eith rplicaSet ShrdA and ShardB running on port 27995 27993. as follows
| 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 |
| 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 |
| 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?
Return to Top