mongodb分片

教程发布:风哥 教程分类:ITPUX技术网 更新日期:2022-02-12 浏览学习:185

[size=22.0pt]Mongodb [size=22.0pt]分片部署
[size=7.5pt]配置mongodb集群,比如 在3个server上配置 3 shard的Mongodb集群:[size=7.5pt]架构:[size=7.5pt]1.[size=7.5pt]每片数据需要3个mongod server,2个为主从数据节点;1个为仲裁节点(arbiter),不存数据。[size=7.5pt] [size=7.5pt]一共三片,可以做成: 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仲裁) [size=7.5pt] [size=7.5pt]2. [size=7.5pt]需要3个mongod config server,登录3台机器执行如下 20.220-mongo1:19999 echo'export PATH=$PATH:/usr/local/mongodb/bin' >> /etc/profiletar zxvfmongodb-linux-x86_64-2.6.0.tgzmvmongodb-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[size=7.5pt] 20.221-mongo2:19998 echo 'exportPATH=$PATH:/usr/local/mongodb/bin' >> /etc/profiletar zxvfmongodb-linux-x86_64-2.6.0.tgzmvmongodb-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[size=7.5pt] 20.222-mongo3:19997 echo'export PATH=$PATH:/usr/local/mongodb/bin' >> /etc/profiletar zxvfmongodb-linux-x86_64-2.6.0.tgzmvmongodb-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[size=7.5pt]3. mongos server [size=7.5pt]作为入口 192.168.20.220:20000[size=7.5pt] [size=7.5pt]实施步骤:[size=7.5pt]1.[size=7.5pt]启动mongod datanode service[size=7.5pt]分别3台服务器上执行如下: /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[size=7.5pt] [size=7.5pt]2.[size=7.5pt]配置每片的 replica set,即每3台配成一个replica set互备 [size=7.5pt]登录mongo 192.168.20.220:20001/admin [size=7.5pt] 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}]}[size=7.5pt] mongo 192.168.20.221:20002/admin[size=7.5pt] config = {_id: 'sh2', members:[{_id:0, host: '192.168.20.220:20002'},{_id:1, host: '192.168.20.221:20002'},{_id: 2, host: '192.168.20.222:20002',arbiterOnly: true}]}[size=7.5pt] mongo 192.168.20.222:20003/admin[size=7.5pt] config = {_id: 'sh3', members:[{_id:0, host: '192.168.20.220:20003'},{_id:1, host: '192.168.20.221:20003'},{_id: 2, host: '192.168.20.222:20003',arbiterOnly: true}]}[size=7.5pt] [size=7.5pt]分别在3台机器上执行如下rs.initiate(config) //初始化replset[size=7.5pt] [size=7.5pt] rs.status() //看结果是否成功建成repl set[size=7.5pt] [size=7.5pt]3.[size=7.5pt]启动 config server:/usr/local/mongodb/bin/mongod --fork--configsvr --port 19999 --dbpath /usr/local/mongodb/date/cf1/ --logpath/usr/local/mongodb/log/cf1.log --rest /usr/local/mongodb/bin/mongod --fork--configsvr --port 19998 --dbpath /usr/local/mongodb/date/cf2/ --logpath/usr/local/mongodb/log/cf2.log --rest /usr/local/mongodb/bin/mongod --fork--configsvr --port 19997 --dbpath /usr/local/mongodb/date/cf3/ --logpath/usr/local/mongodb/log/cf3.log --rest [size=7.5pt]4. [size=7.5pt]启动 mongos server/usr/local/mongodb19999/bin/mongos--configdb 192.168.20.220:19999,192.168.20.221:19998,192.168.20.222:19997--logpath /usr/local/mongodb19999/log/mongos.log --logappend --fork --port20000[size=7.5pt]5. [size=7.5pt]配置mongo 分片集群[size=7.5pt]登录mongos: [size=7.5pt]mongo 192.168.20.220:20000/admin[size=7.5pt]运行:[size=7.5pt]>db.runCommand({addshard:"sh1/l-mo1.prod.cn2:20001,l-mo2.prod.cn2:20001",name:"sh1"});[size=7.5pt]>db.runCommand({addshard:"sh2/l-mo2.prod.cn2:20002,l-mo3.prod.cn2:20002",name:"sh2"});[size=7.5pt]>db.runCommand({addshard:"sh3/l-mo3.prod.cn2:20003,l-mo1.prod.cn2:20003",name:"sh3"});[size=7.5pt]> db.runCommand({listshards:1})[size=7.5pt] [size=7.5pt]6.[size=7.5pt]配置 库表分片 [size=7.5pt]登录mongos:[size=7.5pt]>use admin;[size=7.5pt]>db.runCommand( { enablesharding :"dbname" } );[size=7.5pt]>db.runCommand( { shardcollection :"dbname.collectionname", key :{ "keyfield" :1 }});[size=7.5pt]注意分片使用的keyfield需要是表索引。

本文标签:
本文标题:mongodb分片
网站声明:本文由风哥整理发布,转载请保留此段声明,本站所有内容将不对其使用后果做任何承诺,请读者谨慎使用!
【上一篇】
【下一篇】