Last active
November 22, 2015 06:40
-
-
Save xigua/e8adc26a3b943570a18d to your computer and use it in GitHub Desktop.
Mongodb 3.0 Replica Set Setup with Docker Notes
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
Reference: https://medium.com/@gargar454/deploy-a-mongodb-cluster-in-steps-9-using-docker-49205e231319#.ff9lbtdl9 | |
this post talks about 3 Nodes,Mongodb 2.6.5. In order to use mongo 3.0, a few changes has been made. Assume using root | |
1. First we need 2 servers with docker installed | |
1.1 recommend use Ubuntu 14.04 on aliyun, then install daocloud for docker installation and monitoring | |
curl -sSL https://get.daocloud.io/docker | sh | |
1.2 install daocloud monitoring program | |
follow instruction on daocloud console | |
2. Find the two servers internal ip addresses if under the same PaaS provider and modify /etc/hosts on both servers | |
vi /etc/hosts | |
10.0.0.1 mongo-main | |
10.0.0.2 mongo-slave | |
3. Create a key file and put it on the same location of all servers | |
on main server: | |
cd /opt | |
openssl rand -base64 741 > mongodb-keyfile | |
chmod 600 mongodb-keyfile | |
chown 999 mongodb-keyfile | |
scp mongodb-keyfile [email protected]:/opt | |
then ssh to the other box and do: | |
chown 999 mongodb-keyfile | |
4. On main server, start mongodb container. | |
docker run --name mongo -v /data/mongo:/data/db --hostname="master.mongo.xxx.cn" -p 27017:27017 -d daocloud.io/mongo --storageEngine wiredTiger | |
5. Connect to the master mongo db to create admin user etc.. | |
docker exec -it mongo /bin/bash | |
mongo | |
use admin | |
db.system.version.insert({ "_id" : "authSchema", "currentVersion" : 3 }) | |
db.createUser( { | |
user: "siteUserAdmin", | |
pwd: "adminpassword", | |
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] | |
}); | |
db.createUser( { | |
user: "siteRootAdmin", | |
pwd: "adminpassword", | |
roles: [ { role: "root", db: "admin" } ] | |
}); | |
exit | |
exit | |
6. stop master mongo | |
docker stop mongo | |
7. Start master again with the keyfile | |
docker run --name mongo -v /data/mongo:/data/db -v /opt/mongodb-keyfile:/opt/mongodb-keyfile --hostname="master.mongo.xxx.cn" --add-host master.mongo.xxx.cn:10.0.0.1 --add-host slave.mongo.xxx.cn:10.0.0.2 -p 27017:27017 -d daocloud.io/mongo --keyFile /opt/mongodb-keyfile --replSet "meteor-rs" --storageEngine wiredTiger | |
8. Connect to the replica set and configure it. Still on master node. | |
Keep one ssh window tailing on the master docker log | |
docker logs -f mongo | |
Now start another ssh window to connect to the replica set | |
docker exec -it mongo /bin/bash | |
mongo | |
use admin | |
db.auth("siteRootAdmin", "adminpassword"); | |
rs.initiate() | |
9. Verify the initial replica set configuration | |
rs.conf() | |
10. Start Mongo on slave | |
mkdir -p /data/mongo | |
docker run --name mongo -v /data/mongo:/data/db -v /opt/mongodb-keyfile:/opt/mongodb-keyfile --hostname="slave.mongo.xxx.cn" --add-host master.mongo.xxx.cn:10.0.0.1 --add-host slave.mongo.xxx.cn:10.0.0.2 -p 27017:27017 -d daocloud.io/mongo --keyFile /opt/mongodb-keyfile --replSet "meteor-rs" --storageEngine wiredTiger | |
11. Add slave to the replica set | |
back to master node | |
docker exec -it mongo /bin/bash | |
mongo | |
use admin | |
db.auth("siteRootAdmin", "adminpassword"); | |
rs.add("slave.mongo.xxx.cn"); | |
rs.status() | |
12. To allow querying on slave | |
on slave mongo shell | |
rs.slaveOk() | |
13. Create new user to access other db | |
use dbname | |
db.createUser( { | |
user: "admin", | |
pwd: "password", | |
roles: [{"role": "readWrite", "db": "dbname"}] | |
}); | |
use dbname | |
db.createUser( { | |
user: "admin", | |
pwd: "password", | |
roles: [{"role": "readWrite", "db": "dbname"}] | |
}); | |
14. restore mongodb | |
mongorestore -u admin -p password -d dbname mongodb-2015-11-03-1400/db/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment