Created
August 10, 2011 10:30
-
-
Save paulmillr/1136527 to your computer and use it in GitHub Desktop.
MongoDB sharding setup (quick & dirty)
This file contains 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
#!/usr/bin/env zsh | |
# Change "Mongal" in text to name of your collection. | |
DATADIR="$HOME/Documents/data" | |
mkdir -p $DATADIR/db/{config,logs,0,1,2,3} | |
mkdir $DATADIR/backup | |
cd $DATADIR/db/logs | |
touch 0.log 1.log 2.log 3.log config.log main.log | |
chmod 666 *.log | |
mongod --port 32010 --dbpath=../0 --shardsvr > 0.log & | |
mongod --port 32011 --dbpath=../1 --shardsvr > 1.log & | |
mongod --port 32012 --dbpath=../2 --shardsvr > 2.log & | |
mongod --port 32013 --dbpath=../3 --shardsvr > 3.log & | |
mongod --port 32000 --configsvr --dbpath ../config > config.log & | |
sleep 3 | |
mongos --port 32001 --configdb 127.0.0.1:32000 > main.log & | |
# mongos commands | |
mongo 127.0.0.1:32001/admin | |
db.runCommand({addshard: '127.0.0.1:32010'}); | |
db.runCommand({addshard: '127.0.0.1:32011'}); | |
db.runCommand({addshard: '127.0.0.1:32012'}); | |
db.runCommand({addshard: '127.0.0.1:32013'}); | |
db.runCommand({enablesharding: 'mongal'}); | |
exit; | |
# Download, unpack & restore latest backup from the server to our machine. | |
cd $DATADIR/backup | |
date=`date +%Y-%m-%d` | |
name=DB-$date | |
file=$name.tar.gz | |
scp -P 3022 your.server.com:/back_up_dir/$date/$file . | |
tar -zxvf $file | |
cd $name/ | |
mongorestore --port 32001 -d mongal mongal | |
# check if we have user_ids. | |
mongo 127.0.0.1:32001/mongal | |
if (db.user.count() > 60000) { | |
print('Everything OK'); | |
} else { | |
print('Warning: DB is empty!'); | |
} | |
# production | |
#db.dropDatabase(); | |
#exit() | |
#mongorestore --port 32001 -d mongal mongal/ | |
# Rebuild indexes, shard mongal. | |
db.user.dropIndexes(); | |
db.user.ensureIndex({level: 1}); | |
use admin; | |
db.runCommand({shardcollection: 'mongal.user', key: {level: 1}}); | |
db.runCommand({split: 'mongal.user', middle: {}}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment