educoder MongoDB 复制集 & 分片

第2关:MongoDB 复制集搭建

mkdir -p /data/test/db1 /data/test/db2 /data/test/db3
mkdir -p /logs/test
touch { /logs/test/mongod1.log /logs/test/mongod2.log /logs/test/mongod3.log }
mkdir -p /etc/test
touch { /etc/test/mongod1.conf /etc/test/mongod2.conf /etc/test/mongod3.conf }

vim /etc/test/mongod1.conf

port=20001
dbpath=/data/test/db1 
logpath=/logs/test/mongod1.log 
logappend=true 
fork=true 
replSet=YOURMONGO


vim /etc/test/mongod2.conf

port=20002
dbpath=/data/test/db2
logpath=/logs/test/mongod2.log 
logappend=true 
fork=true 
replSet=YOURMONGO

vim /etc/test/mongod3.conf

port=20003
dbpath=/data/test/db3
logpath=/logs/test/mongod3.log 
logappend=true 
fork=true 
replSet=YOURMONGO


mongod -f /etc/test/mongod1.conf
mongod -f /etc/test/mongod2.conf
mongod -f /etc/test/mongod3.conf



mongo --port 20001


config = {  
  _id:"YOURMONGO",  
  members:[  
      {_id:0,host:'127.0.0.1:20001'},  
      {_id:1,host:'127.0.0.1:20002',arbiterOnly:true},  
      {_id:2,host:'127.0.0.1:20003'},  
  ]  
}


rs.initiate(config)










第3关:MongoDB 分片集搭建

mkdir -p /data/test2/shard1/db
mkdir -p /logs/test2/shard1/log
mkdir -p /data/test2/shard2/db
mkdir -p /logs/test2/shard2/log
mkdir -p /data/test2/shard3/db
mkdir -p /logs/test2/shard3/log
mkdir -p /data/test2/config/db
mkdir -p /logs/test2/config/log
mkdir -p /logs/test2/mongs/log
mkdir -p /etc/test2

vim /etc/test2/mongod1.conf

dbpath=/data/test2/shard1/db
logpath=/logs/test2/shard1/log/mongodb.log
port=21001
shardsvr=true
fork=true

vim /etc/test2/mongod2.conf

dbpath=/data/test2/shard2/db
logpath=/logs/test2/shard2/log/mongodb.log
port=21002
shardsvr=true
fork=true

vim /etc/test2/mongod3.conf

dbpath=/data/test2/shard3/db
logpath=/logs/test2/shard3/log/mongodb.log
port=21003
shardsvr=true
fork=true

mongod -f /etc/test2/mongod1.conf
mongod -f /etc/test2/mongod2.conf
mongod -f /etc/test2/mongod3.conf

mongod --dbpath /data/test2/config/db --logpath /logs/test2/config/log/mongodb.log --port 21004 --configsvr --replSet cs --fork

mongo localhost:21004

use admin;
cfg = {
  _id:'cs',
  configsvr:true,
  members:[
      {_id:0,host:'localhost:21004'}
   ]
};
rs.initiate(cfg);

exit

mongos --configdb cs/localhost:21004 --logpath /logs/test2/mongs/log/mongodb.log --port 21005 --fork


mongo localhost:21005

sh.addShard('localhost:21001');
sh.addShard('localhost:21002');
sh.addShard('localhost:21003');


sh.status();


db.collection.dropIndexes()












评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值