Last active
May 7, 2024 13:33
-
-
Save joewagner/c2881417f4a997e0a155 to your computer and use it in GitHub Desktop.
Bash shell script that sets up a sharded mongodb cluster on a single machine. Handy for testing or development when a sharded deployment is required. Notice that this will remove everything in the data/config and data/shard directories. If you are using those for something else, you may want to edit this...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# clean everything up | |
echo "killing mongod and mongos" | |
killall mongod | |
killall mongos | |
echo "removing data files" | |
rm -rf data/config | |
rm -rf data/shard* | |
# For mac make sure rlimits are high enough to open all necessary connections | |
ulimit -n 2048 | |
# start a replica set and tell it that it will be shard0 | |
mkdir -p /data/shard0/rs0 /data/shard0/rs1 /data/shard0/rs2 | |
mongod --replSet s0 --logpath "s0-r0.log" --dbpath data/shard0/rs0 --port 37017 --fork --shardsvr --smallfiles | |
mongod --replSet s0 --logpath "s0-r1.log" --dbpath data/shard0/rs1 --port 37018 --fork --shardsvr --smallfiles | |
mongod --replSet s0 --logpath "s0-r2.log" --dbpath data/shard0/rs2 --port 37019 --fork --shardsvr --smallfiles | |
sleep 5 | |
# connect to one server and initiate the set | |
mongo --port 37017 << 'EOF' | |
config = { _id: "s0", members:[ | |
{ _id : 0, host : "localhost:37017" }, | |
{ _id : 1, host : "localhost:37018" }, | |
{ _id : 2, host : "localhost:37019" }]}; | |
rs.initiate(config) | |
EOF | |
# start a replicate set and tell it that it will be a shard1 | |
mkdir -p /data/shard1/rs0 /data/shard1/rs1 /data/shard1/rs2 | |
mongod --replSet s1 --logpath "s1-r0.log" --dbpath data/shard1/rs0 --port 47017 --fork --shardsvr --smallfiles | |
mongod --replSet s1 --logpath "s1-r1.log" --dbpath data/shard1/rs1 --port 47018 --fork --shardsvr --smallfiles | |
mongod --replSet s1 --logpath "s1-r2.log" --dbpath data/shard1/rs2 --port 47019 --fork --shardsvr --smallfiles | |
sleep 5 | |
mongo --port 47017 << 'EOF' | |
config = { _id: "s1", members:[ | |
{ _id : 0, host : "localhost:47017" }, | |
{ _id : 1, host : "localhost:47018" }, | |
{ _id : 2, host : "localhost:47019" }]}; | |
rs.initiate(config) | |
EOF | |
# start a replicate set and tell it that it will be a shard2 | |
mkdir -p /data/shard2/rs0 /data/shard2/rs1 /data/shard2/rs2 | |
mongod --replSet s2 --logpath "s2-r0.log" --dbpath data/shard2/rs0 --port 57017 --fork --shardsvr --smallfiles | |
mongod --replSet s2 --logpath "s2-r1.log" --dbpath data/shard2/rs1 --port 57018 --fork --shardsvr --smallfiles | |
mongod --replSet s2 --logpath "s2-r2.log" --dbpath data/shard2/rs2 --port 57019 --fork --shardsvr --smallfiles | |
sleep 5 | |
mongo --port 57017 << 'EOF' | |
config = { _id: "s2", members:[ | |
{ _id : 0, host : "localhost:57017" }, | |
{ _id : 1, host : "localhost:57018" }, | |
{ _id : 2, host : "localhost:57019" }]}; | |
rs.initiate(config) | |
EOF | |
# now start 3 config servers | |
rm cfg-a.log cfg-b.log cfg-c.log | |
mkdir -p /data/config/config-a /data/config/config-b /data/config/config-c | |
mongod --logpath "cfg-a.log" --dbpath data/config/config-a --port 57040 --fork --configsvr --smallfiles | |
mongod --logpath "cfg-b.log" --dbpath data/config/config-b --port 57041 --fork --configsvr --smallfiles | |
mongod --logpath "cfg-c.log" --dbpath data/config/config-c --port 57042 --fork --configsvr --smallfiles | |
# now start the mongos on port 27018 | |
rm mongos-1.log | |
sleep 5 | |
mongos --port 27018 --logpath "mongos-1.log" --configdb localhost:57040,localhost:57041,localhost:57042 --fork | |
echo "Waiting 60 seconds for the replica sets to fully come online" | |
sleep 60 | |
echo "Connnecting to mongos and enabling sharding" | |
# add shards and enable sharding on the test db | |
mongo --port 27018 << 'EOF' | |
db.adminCommand( { addshard : "s0/"+"localhost:37017" } ); | |
db.adminCommand( { addshard : "s1/"+"localhost:47017" } ); | |
db.adminCommand( { addshard : "s2/"+"localhost:57017" } ); | |
db.adminCommand({enableSharding: "test"}); | |
EOF | |
sleep 5 | |
echo "Done setting up sharded environment on localhost" |
Hello,
Could this work on mongodb 5.0.3? Thanks.
I haven't tried it on any 5.x version. If you try it, I'd love to know how it goes
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello,
Could this work on mongodb 5.0.3? Thanks.