MongoDB複製集搭建

來源:互聯網
上載者:User

看了mongodb權威指南之後,對於讀寫分離的方式採用複製集比較合適,一般的系統足夠使用了。

很容易找到,暫不累述,只描述關鍵步驟。

2台伺服器分別為201和202 共3個節點,2個初始配置在202上,1個後來添加在201上

先部署2個節點
202:
./mongod --dbpath ./db1 --port 10002 --replSet blort/Server202:10001
./mongod --dbpath ./db --port 10001 --replSet blort/Server202:10002

./mongo Server202:10001/admin
db.runCommand({"replSetInitiate":{"_id":"blort","members":[{"_id":1,"host":"Server202:10001"},{"_id":2,"host":"Server202:10002"}]}})
db._adminCommand("replSetGetStatus");

增加一個節點
201:
./mongod --dbpath ./db --port 10001 --replSet blort/Server202:10002
202:
rs.add("Server201:10002");

預設從庫不可查詢,執行如下語句使得當前從庫可以查詢,分擔主庫查詢負載
db.getMongo().setSlaveOk()

隨機器自動啟動指令碼在/etc/rc.local
rm -rf /home/aircom/mongodb-linux-i686-2.0.6/bin/db/*.lock
rm -rf /home/aircom/mongodb-linux-i686-2.0.6/bin/db1/*.lock
/home/aircom/mongodb-linux-i686-2.0.6/bin/mongod --dbpath /home/aircom/mongodb-linux-i686-2.0.6/bin/db --port 10001 --replSet blort/Server202:10002 --fork --logpath /home/aircom/mongodb-linux-i686-2.0.6/bin/log/1001.log
/home/aircom/mongodb-linux-i686-2.0.6/bin/mongod --dbpath /home/aircom/mongodb-linux-i686-2.0.6/bin/db1 --port 10002 --replSet blort/Server202:10001 --fork --logpath /home/aircom/mongodb-linux-i686-2.0.6/bin/log/1002.log

修改配置,如修改優先順序等,可以利用優先順序來控制主幾點那個ji
conf=rs.conf()
conf.members[0].priority=3
conf.members[1].priority=2
conf.members[2].priority=1
conf.members[3].priority=0.5
rs.reconfig(conf)

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.