1.基本命令
mongodb启动
help;
db.help();
show tables;
mkdir -p /home/db
mongod –port 11111 –dbpath E:\data\mongodb
> use foobar
> db
> post = {"title":"my blog post","content":"here is my bolg post","date":"new Date"}
> db.blog.insert(post);
> db.blog.find();
> db.blog.findOne();//查找一条数据
> post.comments = []
> db.blog.update({title:"my blog post"},post) //自动显示最多20个匹配的文档
> db.blog.remove({"title":"my blog post"});
2.mapreduce学习
QueryBuilder query = QueryBuilder.start("pin").notEquals(pin)
.and("sku").in(skus)
.and("updateDate").greaterThan(DateUtil.format(beginTime,DateUtil.YYYYMMDD_HHMMSS))
.and("updateDate").lessThanEquals(DateUtil.format(endTime,DateUtil.YYYYMMDD_HHMMSS));
String map = "function(){emit(this.pin,{sku:this.sku,count:1})}";
String reduce = "function(key,vals) { "+
" var array = []; "+
"for(var i in vals ){ "+
" var flag = false; "+
" for(var j in array){ "+
" if(vals[i].sku==array[j]){ "+
" flag=true; "+
" } "+
" } "+
" if(!flag){ "+
" array.push(vals[i].sku); "+
" } "+
" } "+
" return array.length; "+
"} ";
MapReduceOutput mp1 = userOrderClient.getCollection().mapReduce(map, reduce,null, MapReduceCommand.OutputType.INLINE, query.get());
//转为map,排序,截取,保存
Map<String,Integer> result = new HashMap<String,Integer>();
Iterator<DBObject> it= mp1.results().iterator();
while(it.hasNext()){
DBObject obj= it.next();
String str = obj.get("value").toString();
int value =1;
try{
value = Integer.parseInt(str.substring(0, str.length()-2));
}catch(NumberFormatException e){}
result.put(obj.get("_id").toString(), value);
}
3.新建集群库/表
先切到admin库下
库:
db.runCommand({"enablesharding":"UserProfile"})
表:
db.runCommand({shardcollection:'UserProfile.User_Temporary',key:{_id:1},unique:true})
删除库
user xxx
db.runCommand({"dropDatabase": 1});
4.master/slave配置,且设置认证
slave/10.10.242.195
rm -rf /root/mongodb_slave_data
mkdir -p /root/mongodb_slave_data
./mongod --slave --source 10.10.242.196:6688 --port 6689 --dbpath /root/mongodb_slave_data --maxConns 80000 #不加认证
use local
db.addUser('repl','repl');
./mongod --slave --source 10.10.242.196:6688 --port 6689 --dbpath /root/mongodb_slave_data --maxConns 80000 --auth #添加认证
master/10.10.242.196
master/10.10.242.196
rm -rf /root/mongodb_data
mkdir -p /root/mongodb_data
./mongod --master --dbpath /root/mongodb_data --port 6688 --maxConns 80000
use local
db.addUser('repl','repl');
./mongod --master --dbpath /root/mongodb_data --port 6688 --maxConns 80000 -auth
use admin
db.addUser('admin','admin');
为test添加密码
use admin
db.auth('admin','admin');
db.addUser('test','test');
5.MonogoDB Sharding + Replica Set配置集群
195
mkdir -p /usr/MonogoData/shard1_10001
mkdir -p /usr/MonogoData/shard1_10011
./mongod --shardsvr --replSet shard1 --port 10001 --dbpath /usr/MonogoData/shard1_10001/ --logpath /usr/MonogoData/shard1_10001/log.log --rest --directoryperdb --maxConns 2000 --fork &
./mongod --shardsvr --replSet shard1 --port 10011 --dbpath /usr/MonogoData/shard1_10011/ --logpath /usr/MonogoData/shard1_10011/log.log --rest --directoryperdb --maxConns 2000 --fork &
./mongo --port 10001
use admin
config={_id:'shard1',members:[{_id:0,host:'10.10.249.195:10001'},{_id:1,host:'10.10.249.195:10011'}]}
rs.initiate(config)
rs.status()
196
mkdir -p /usr/MonogoData/shard1_10001
mkdir -p /usr/MonogoData/shard1_10011
./mongod --shardsvr --replSet shard2 --port 10001 --dbpath /usr/MonogoData/shard1_10001/ --logpath /usr/MonogoData/shard1_10001/log.log --rest --directoryperdb --maxConns 2000 --fork &
./mongod --shardsvr --replSet shard2 --port 10011 --dbpath /usr/MonogoData/shard1_10011/ --logpath /usr/MonogoData/shard1_10011/log.log --rest --directoryperdb --maxConns 2000 --fork &
./mongo --port 10001
use admin
config={_id:'shard2',members:[{_id:0,host:'10.10.249.196:10001'},{_id:1,host:'10.10.249.196:10011'}]}
rs.initiate(config)
rs.status()
197
mkdir -p /usr/MonogoData/shard1_10001
mkdir -p /usr/MonogoData/shard1_10011
./mongod --shardsvr --replSet shard3 --port 10001 --dbpath /usr/MonogoData/shard1_10001/ --logpath /usr/MonogoData/shard1_10001/log.log --rest --directoryperdb --maxConns 2000 --fork &
./mongod --shardsvr --replSet shard3 --port 10011 --dbpath /usr/MonogoData/shard1_10011/ --logpath /usr/MonogoData/shard1_10011/log.log --rest --directoryperdb --maxConns 2000 --fork &
./mongo --port 10001
use admin
config={_id:'shard3',members:[{_id:0,host:'10.10.249.196:10001'},{_id:1,host:'10.10.249.196:10011'}]}
rs.initiate(config)
rs.status()
这样,shard就配置好了。
现在开始配置1台Mongodb Config
mkdir -p /usr/MonogoData/Config_20001
./mongod --configsvr --port 20001 --dbpath /usr/MonogoData/Config_20001/ --logpath /usr/MonogoData/Config_20001/log.log --rest --fork &
$ ps aux | grep mongodb | grep -v grep
查看是否mongodb的进程都起来了
下面接着配置Mongs
mkdir -p /usr/MonogoData/master
./mongos --port 40000 --logpath /usr/MonogoData/master/log.log --chunkSize 1 --configdb 10.10.249.195:20001 --fork &
./mongo --port 40000
use admin
db.runCommand({ addshard:'shard1/10.10.249.195:10001,10.10.249.195:10011' })
db.runCommand({ addshard:'shard2/10.10.249.196:10001,10.10.249.196:10011' })
db.runCommand({ addshard:'shard3/10.10.249.197:10001,10.10.249.197:10011' })
db.runCommand({listshards:1})
接下来进行切片
db.runCommand({ enablesharding:'liaoyongdb' }) #指定切片数据库
db.runCommand({shardcollection:'liaoyongdb.test',key:{_id:1}}) #指定切片数据的表
printShardingStatus()
删除索引
db.runCommand({'dropIndexes':"BrowseFinalBuy",'index':'wid_1_rwid_1'});
6.集群修改ip后,无法正常启动
sharding模式直接修改admin库里某个表ip即可
Replica Sets则修改local库中某个表ip即可
7.聚合函数学习
db.liguojun.aggregate({$match:{"key":"苹果"}},{$group:{"_id":"$sku","total":{$sum:"$count"}}});
where 为 key = 苹果,按sku分组,求count的和,注意:_id 不可更改,只能为_id