0.环境
系统:14.04
节点:四个,三个为数据节点,一个为配置节点和路由节点
mongoDB:3.0.5
时间:2015.8.15
1.下载安装包并解压分发:
官网:https://www.mongodb.org/downloads
wget <code class="build-snippet" id="development-linux-build-snippet">https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1404-3.0.5.tgz</code>
tar -zxvf <code class="build-snippet" id="development-linux-build-snippet">mongodb-linux-x86_64-ubuntu1404-3.0.5.tgz</code>
mv <code class="build-snippet" id="development-linux-build-snippet">mongodb-linux-x86_64-ubuntu1404-3.0.5.tgz</code> mongodb
cp mongodb ~/cloud/
新建文件夹放db和log:
cd ~/cloud/mongodb
mkdir -p data/{db,log}
如果权限要修改用chmod
分发安装包到其他节点:
scp mongodb username@ip:~/cloud
用户名为节点用户名,ip分别是其他节点的ip,需要进行三次。嫌麻烦可以写脚本
可用ssh来设置无密钥登陆,就不需要输入密码了。详细请查找openssh等相关资料。
2.在每个数据节点(三个:node1/2/3)分别执行:
./bin/mongod -shardsvr -port 27017 -dbpath=data/db/ -logpath=data/db/shard11.log --fork
会生成子进程,可查看:ps -aux|grep mongo 或者ps -ef|grep mongo 或者top
如果第一次没配置成功可以用kill -9 pid(进程号)杀死进程
3.配置节点设置(master):
./bin/mongod -configsvr -dbpath=data/db/ -port 20000 -logpath=data/log/config.log --fork
4.配置路由节点(master):
./bin/mongos -configdb ip_master:20000 -port 30000 -chunkSize 100 -logpath=data/log/mongos.log --fork
ip_master为路由节点的ip,需要自己输入,如192.168.1.11
5.配置分片:
启动:
./bin/mongo ip_master:30000/admin
ip_master为路由节点的ip,需要自己输入,如192.168.1.11
ip_node1/2/3对应的需要输入对应的ip
添加分片服务器:
db.runCommand({"addshard":"ip_node1:27017"})
db.runCommand({"addshard":"ip_node2:27017"})
db.runCommand({"addshard":"ip_node3:27017"})
设置分片数据库:
db.runCommand({"enablesharding": "xubo"})
使用数据库:
use xubo
设置表分片基于字段hash:
sh.shardCollection("xubo.resume_meta_data",{_id:"hashed"})
设置表分片基于字段(注意这种形式与以上作用相同):
db.runCommand({"shardcollection":"xubo.things", "key":{"_id":1}})
打印数据库分片信息:
db.printShardingStatus()
打印服务器信息:
db.serverStatus()
创建表索引:
db.resume_meta_data.ensureIndex({"org_name":1},{"background":true})
查看表索引:
db.resume_meta_data.getIndexes()
6.速度测试:
速度测试报错插入insert和删除remove测试
1) 插入数据速度测试:
var inserttimes=function(n){
var start=(new Date()).getTime();
for(var i=1;i<=n;i++){
var id=ObjectId();
db.tester.insert({"_id":id,"x":Math.random(),"name":"xubo","time":"20150813","ops":"testinserttimes"});
}
var timeDiff=(new Date()).getTime()-start;
print("n:"+ n +"insert times:"+timeDiff+"ms");
}
多个n值:
for(var i=1;i<=1000000;i=i*10){
inserttimes(i);
}
运行结果:
n:1insert times:1ms
n:10insert times:11ms
n:100insert times:119ms
n:1000insert times:1020ms
n:10000insert times:10046ms
n:100000insert times:100879ms
n:1000000insert times:1011941ms
查看个节点数据情况:db.stats()
mongos> db.stats()
{
"raw" : {
"ip_node1:27017" : {
"db" : "xubo",
"collections" : 3,
"objects" : 8,
"avgObjSize" : 88,
"dataSize" : 704,
"storageSize" : 20480,
"numExtents" : 3,
"indexes" : 3,
"indexSize" : 24528,
"fileSize" : 67108864,
"nsSizeMB" : 16,
"extentFreeList" : {
"num" : 0,
"totalSize" : 0
},
"dataFileVersion" : {
"major" : 4,
"minor" : 22
},
"ok" : 1
},
"ip_node2:27017" : {
"db" : "xubo",
"collections" : 3,
"objects" : 8,
"avgObjSize" : 88,
"dataSize" : 704,
"storageSize" : 20480,
"numExtents" : 3,
"indexes" : 3,
"indexSize" : 24528,
"fileSize" : 67108864,
"nsSizeMB" : 16,
"extentFreeList" : {
"num" : 0,
"totalSize" : 0
},
"dataFileVersion" : {
"major" : 4,
"minor" : 22
},
"ok" : 1
},
"ip_node3:27017" : {
"db" : "xubo",
"collections" : 4,
"objects" : 1283504,
"avgObjSize" : 111.99975068250664,
"dataSize" : 143752128,
"storageSize" : 174755840,
"numExtents" : 15,
"indexes" : 4,
"indexSize" : 41673072,
"fileSize" : 469762048,
"nsSizeMB" : 16,
"extentFreeList" : {
"num" : 0,
"totalSize" : 0
},
"dataFileVersion" : {
"major" : 4,
"minor" : 22
},
"ok" : 1
}
},
"objects" : 1283520,
"avgObjSize" : 110.99971328845675,
"dataSize" : 143753536,
"storageSize" : 174796800,
"numExtents" : 21,
"indexes" : 10,
"indexSize" : 41722128,
"fileSize" : 603979776,
"extentFreeList" : {
"num" : 0,
"totalSize" : 0
},
"ok" : 1
}
分布不均匀。。。
查找数据:db.tester.find()
mongos> db.tester.find()
{ "_id" : ObjectId("55cd8ba97978fdf77ed4fbf7"), "name" : "xubo", "time" : "20150813", "ops" : "testinserttimes" }
{ "_id" : ObjectId("55cd8ba97978fdf77ed4fbf8"), "name" : "xubo", "time" : "20150813", "ops" : "testinserttimes" }
{ "_id" : ObjectId("55cd8ba97978fdf77ed4fbf9"), "name" : "xubo", "time" : "20150813", "ops" : "testinserttimes" }
{ "_id" : ObjectId("55cd8ba97978fdf77ed4fbfa"), "name" : "xubo", "time" : "20150813", "ops" : "testinserttimes" }
{ "_id" : ObjectId("55cd8ba97978fdf77ed4fbfb"), "name" : "xubo", "time" : "20150813", "ops" : "testinserttimes" }
{ "_id" : ObjectId("55cd8ba97978fdf77ed4fbfc"), "name" : "xubo", "time" : "20150813", "ops" : "testinserttimes" }
{ "_id" : ObjectId("55cd8ba97978fdf77ed4fbfd"), "name" : "xubo", "time" : "20150813", "ops" : "testinserttimes" }
{ "_id" : ObjectId("55cd8ba97978fdf77ed4fbfe"), "name" : "xubo", "time" : "20150813", "ops" : "testinserttimes" }
{ "_id" : ObjectId("55cd8ba97978fdf77ed4fbff"), "name" : "xubo", "time" : "20150813", "ops" : "testinserttimes" }
{ "_id" : ObjectId("55cd8ba97978fdf77ed4fc00"), "name" : "xubo", "time" : "20150813", "ops" : "testinserttimes" }
{ "_id" : ObjectId("55cd8ba97978fdf77ed4fc01"), "name" : "xubo", "time" : "20150813", "ops" : "testinserttimes" }
{ "_id" : ObjectId("55cd8ba97978fdf77ed4fc02"), "name" : "xubo", "time" : "20150813", "ops" : "testinserttimes" }
{ "_id" : ObjectId("55cd8ba97978fdf77ed4fc03"), "name" : "xubo", "time" : "20150813", "ops" : "testinserttimes" }
{ "_id" : ObjectId("55cd8ba97978fdf77ed4fc04"), "name" : "xubo", "time" : "20150813", "ops" : "testinserttimes" }
{ "_id" : ObjectId("55cd8ba97978fdf77ed4fc05"), "name" : "xubo", "time" : "20150813", "ops" : "testinserttimes" }
{ "_id" : ObjectId("55cd8ba97978fdf77ed4fc06"), "name" : "xubo", "time" : "20150813", "ops" : "testinserttimes" }
{ "_id" : ObjectId("55cd8ba97978fdf77ed4fc07"), "name" : "xubo", "time" : "20150813", "ops" : "testinserttimes" }
{ "_id" : ObjectId("55cd8ba97978fdf77ed4fc08"), "name" : "xubo", "time" : "20150813", "ops" : "testinserttimes" }
{ "_id" : ObjectId("55cd8ba97978fdf77ed4fc09"), "name" : "xubo", "time" : "20150813", "ops" : "testinserttimes" }
{ "_id" : ObjectId("55cd8ba97978fdf77ed4fc0a"), "name" : "xubo", "time" : "20150813", "ops" : "testinserttimes" }
2)删除速度测试:
var removetimes=function(){
var start=(new Date()).getTime();
db.tester.remove({});
var timeDiff=(new Date()).getTime()-start;
print("remove times:"+timeDiff+"ms");
}
文档数:1283520
结果:
mongos> removetimes()
remove times:12890ms
参考资料:
[1] http://my.oschina.net/zhzhenqin/blog/97268
[2] http://www.lanceyan.com/tech/arch/mongodb_shard1.html
[3] http://blog.csdn.net/e421083458/article/details/46791433
[4] http://blog.csdn.net/luonanqin/article/details/8497860 ->测试没成功