-
-
Save ftroncosom/a682ef522f0b254f26751440ca7e3d40 to your computer and use it in GitHub Desktop.
MongoDB replica set in docker
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
# get it | |
docker pull mongo | |
# startup a 3 node replica set | |
docker run --name mongo-rs-1 -d mongo --nojournal --oplogSize 10 --replSet rs | |
docker run --name mongo-rs-2 -d mongo --nojournal --oplogSize 10 --replSet rs | |
docker run --name mongo-rs-3 -d mongo --nojournal --oplogSize 10 --replSet rs | |
# connect to first node | |
docker run -it --link mongo-rs-1:mongo1 --link mongo-rs-2:mongo2 --link mongo-rs-3:mongo3 --rm mongo /bin/bash | |
# find all IP's for later commands | |
MONGO1=`grep mongo1 /etc/hosts | awk '{print $1}'` | |
MONGO2=`grep mongo2 /etc/hosts | awk '{print $1}'` | |
MONGO3=`grep mongo3 /etc/hosts | awk '{print $1}'` | |
mongo mongo1:27017 --eval "rs.initiate(); myconf = rs.conf(); myconf.members[0].host = '$MONGO1:27017'; rs.reconfig(myconf,{force:true}); rs.add('$MONGO2:27017'); rs.add('$MONGO3:27017');" | |
# test it out! | |
# write new data on primary | |
mongo mongo1:27017/test --eval 'db.users.insert({"name":"Somebody", "city": "Raleigh"})' | |
# check data in secondary | |
mongo mongo2:27017/test --eval 'rs.slaveOk(); db.users.find().forEach(function(x){printjson(x);})' | |
# exit docker client container | |
exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment