MongoDB複製集環境搭建

來源:互聯網
上載者:User

標籤:mongodb   複製集   



環境介紹:

192.168.1.250 主  port=27408

192.168.1.250 仲裁 port=27409

192.168.1.251 備  port=27408



[[email protected] ~]# tar xvf mongodb-linux-x86_64-2.6.10.tgz

[[email protected] ~]# mkdir -p /export/mongodb

[[email protected] ~]# mkdir -p /export/mongodb/bin

[[email protected] ~]# mkdir -p /export/mongodb/conf

[[email protected] ~]# mkdir -p /export/mongodb/log

[[email protected] ~]# mkdir -p /export/mongodb/data

[[email protected] bin]# cd /root/mongodb-linux-x86_64-2.6.10/bin

[[email protected] bin]# cp /root/mongodb-linux-x86_64-2.6.10/bin/* /export/mongodb/bin/

[[email protected] bin]# vi /export/mongodb/conf/mongod.conf

port=27408dbpath=/export/mongodb/datalogpath=/export/mongodb/log/mongod.logfork=truelogappend=truekeyFile=/export/mongodb/key/mongodnohttpinterface=truereplSet=shard1[[email protected] bin]# vi /export/mongodb/conf/arbiter.confport=27409dbpath=/export/mongodb/arbiterlogpath=/export/mongodb/log/arbiter.logfork=truelogappend=truekeyFile=/export/mongodb/key/arbiternohttpinterface=truereplSet=shard1


keyfile檔案包括:

mongod,arbiter


建立一個產生keyfile的指令碼

vi create_key.sh

cat /dev/urandom | LC_ALL=C tr -dc "[:alnum:]" | fold -w 10 |head -1 >/tmp/key.txtkeystring=`cat /tmp/key.txt`echo $keystring >/export/mongodb/key/mongodecho $keystring >/export/mongodb/key/arbiterchmod 600 /export/mongodb/key/*


啟動伺服器在主

[[email protected] ~]#/export/mongodb/bin/mongod -f /export/mongodb/conf/mongod.conf

[[email protected] ~]# /export/mongodb/bin/mongod -f /export/mongodb/conf/arbiter.conf

在從

[[email protected] ~]#/export/mongodb/bin/mongod -f /export/mongodb/conf/mongod.conf



>config={_id:‘shard1‘,members:[{_id:0,host:‘192.168.1.248:27408‘},{_id:1,host:‘192.168.1.249:27408‘},{_id:2,host:‘192.168.1.248:27409‘,arbiterOnly:true}]}




>rs.initiate(config)

初始化rs.initiate(config),config是之前定義的名


主備庫配置好後,備庫查詢

shard1:SECONDARY> use testswitched to db testshard1:SECONDARY> db.t1.find()error: { "$err" : "not master and slaveOk=false", "code" : 13435 }shard1:SECONDARY> rs.slaveOk()shard1:SECONDARY> db.t1.find(){ "_id" : ObjectId("5704c11d3e0651733bfdea23"), "x" : 1 }


rs.stauts()可以看狀態,health:1代表健康,stateStr誰是我們的仲裁

想讓主庫降級成從庫,rs.stepDown()


本文出自 “歲伏” 部落格,請務必保留此出處http://suifu.blog.51cto.com/9167728/1853478

MongoDB複製集環境搭建

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.