mongodb分區部署

來源:互聯網
上載者:User

標籤:

Mongodb 分區部署

配置mongodb叢集,比如 在3個server上配置 3 shard的Mongodb叢集:

架構:

1.每片資料需要3個mongod server,2個為主從資料節點;1個為仲裁節點(arbiter),不存資料。 一共三片,可以做成:  

   20.220-mongo1:   20001(sh1主),20002(sh2從),20003(sh3仲裁)    20.221-mongo2:   20002(sh2主),20003(sh3從),20001(sh1仲裁)    20.222-mongo3:   20003(sh3主),20001(sh1從),20002(sh2仲裁) 

2. 需要3個mongod config server,登入3台機器執行如下

 20.220-mongo1: 19999    echo ‘export PATH=$PATH:/usr/local/mongodb/bin‘ >> /etc/profiletar zxvf mongodb-linux-x86_64-2.6.0.tgzmv mongodb-linux-x86_64-2.6.0 /usr/local/mongodbmkdir -p /usr/local/mongodb/{etc,date,log}cd /usr/local/mongodb/datemkdir sh1mkdir sh2mkdir sh3mkdir cf1mkdir cf2mkdir cf3
20.221-mongo2: 19998    echo ‘export PATH=$PATH:/usr/local/mongodb/bin‘ >> /etc/profiletar zxvf mongodb-linux-x86_64-2.6.0.tgzmv mongodb-linux-x86_64-2.6.0 /usr/local/mongodbmkdir -p /usr/local/mongodb/{etc,date,log}cd /usr/local/mongodb/datemkdir sh1mkdir sh2mkdir sh3mkdir cf1mkdir cf2mkdir cf3
20.222-mongo3: 19997    echo ‘export PATH=$PATH:/usr/local/mongodb/bin‘ >> /etc/profiletar zxvf mongodb-linux-x86_64-2.6.0.tgzmv mongodb-linux-x86_64-2.6.0 /usr/local/mongodbmkdir -p /usr/local/mongodb/{etc,date,log}cd /usr/local/mongodb/datemkdir sh1mkdir sh2mkdir sh3mkdir cf1mkdir cf2mkdir cf3

3. mongos server 作為入口

192.168.20.220: 20000

1.啟動mongod datanode service

/usr/local/mongodb/bin/mongod --fork --rest --replSet sh1 --shardsvr --oplogSize 40000 --dbpath /usr/local/mongodb/date/sh1 --logpath /usr/local/mongodb/log/sh1.log --port 20001/usr/local/mongodb/bin/mongod --fork --rest --replSet sh2 --shardsvr --oplogSize 40000 --dbpath /usr/local/mongodb/date/sh2 --logpath /usr/local/mongodb/log/sh2.log --port 20002/usr/local/mongodb/bin/mongod --fork --rest --replSet sh3 --shardsvr --oplogSize 40000 --dbpath /usr/local/mongodb/date/sh3 --logpath /usr/local/mongodb/log/sh3.log --port 20003

2.配置每片的 replica set,即每3台配成一個replica set互備 

20.220-mongo1:主機mongo 192.168.20.220:20001/adminuse admin;
config = {_id: ‘sh1‘, members:[{_id:0, host: ‘192.168.20.220:20001‘},{_id:1, host: ‘192.168.20.221:20001‘},{_id:2, host: ‘192.168.20.222:20001‘, arbiterOnly: true}]}
rs.initiate(config) //初始化repl set

rs.status()   //看結果是否成功建成repl set

20.221-mongo2:主機mongo 192.168.20.221:20002/adminuse admin;
config = {_id: ‘sh2‘, members:[{_id:0, host: ‘192.168.20.221:20002‘},{_id:1, host: ‘192.168.20.222:20002‘},{_id:2, host: ‘192.168.20.220:20002‘, arbiterOnly: true}]}
rs.initiate(config) //初始化repl set

rs.status()   //看結果是否成功建成repl set

20.222-mongo3:主機mongo 192.168.20.222:20003/adminuse admin; 
config = {_id: ‘sh3‘, members:[{_id:0, host: ‘192.168.20.222:20003‘},{_id:1, host: ‘192.168.20.220:20003‘},{_id:2, host: ‘192.168.20.221:20003‘, arbiterOnly: true}]}
rs.initiate(config) //初始化repl set

rs.status()   //看結果是否成功建成repl set

3.執行啟動 config server:

/usr/local/mongodb/bin/mongod --fork --rest --configsvr --port 19999 --dbpath /usr/local/mongodb/date/cf1/ --logpath /usr/local/mongodb/log/cf1.log20.221-mongo2/usr/local/mongodb/bin/mongod --fork --rest --configsvr --port 19998 --dbpath /usr/local/mongodb/date/cf2/ --logpath /usr/local/mongodb/log/cf2.log20.222-mongo3/usr/local/mongodb/bin/mongod --fork --rest --configsvr --port 19997 --dbpath /usr/local/mongodb/date/cf3/ --logpath /usr/local/mongodb/log/cf3.log 

4.啟動  mongos server

/usr/local/mongodb/bin/mongos --configdb 192.168.20.220:19999,192.168.20.221:19998,192.168.20.222:19997 --logpath /usr/local/mongodb/log/mongos.log --logappend --fork --port 20000

5. 配置mongo 分區叢集

mongo 192.168.20.220:20000/admin
mongos> db.runCommand({addshard:"sh1/192.168.20.220:20001,192.168.20.221:20001",name:"sh1"});

mongos> db.runCommand({addshard:"sh2/192.168.20.221:20002,192.168.20.222:20002",name:"sh2"});

mongos> db.runCommand({addshard:"sh3/192.168.20.222:20003,192.168.20.220:20003",name:"sh3"});

mongos> db.runCommand({listshards:1})

6. 啟用資料庫分區 

登入mongos:

>use admin;
>db.runCommand( { enablesharding : "dbname" } );

>db.runCommand( { shardcollection : "dbname.collectionname", key :{ "keyfield" :1 }});

注意分區使用的keyfield需要是表索引。

sh.enableSharding("dbname")

 

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.