mongodb 3.2.7版本 的分片集群安装demo

来源:互联网 发布:微控制器是单片机 编辑:程序博客网 时间:2024/06/06 17:33
下面的作法,是在一台机器上,搭建一个mongodb分片集群。所有的mongod,mongos实例均运行在一台机器上,原因是目前手头没有多余的机器,所以只能在一台服务器上,共运行12个mongod实例(9个用于副本集,3个用于配置),1个mongos实例,为了防止端口、路径、文件冲突所以进行了细心的设计。另外也只是学习mongodb的分片集群的安装,所以也就没有搞太多的机器。

操作步骤
cd /home/deploy/test/mongodb
这个目录为mongodb的安装目录(关于mongodb的安装,就不详述了),在该目录下建如下目录
mkdir data
cd data
#shard1目录
mkdir db1-1
mkdir db1-2
mkdir db1-3
#shard2目录
mkdir db2-1
mkdir db2-2
mkdir db2-3
#shard3目录
mkdir db3-1
mkdir db3-2
mkdir db3-3
#log目录
mkdir log
#配置文件目录
mkdir configdb1
mkdir configdb2
mkdir configdb3

#副本集db1 启动命令

/home/deploy/test/mongodb/bin/mongod --dbpath /home/deploy/test/mongodb/data/db1-1 --port 11001 --replSet db1 --logpath=/home/deploy/test/mongodb/data/log/db1-1.log --pidfilepath=/home/deploy/test/mongodb/data/db1-1.pid & 


/home/deploy/test/mongodb/bin/mongod --dbpath /home/deploy/test/mongodb/data/db1-2 --port 11002 --replSet db1 --logpath=/home/deploy/test/mongodb/data/log/db1-2.log --pidfilepath=/home/deploy/test/mongodb/data/db1-2.pid & 


/home/deploy/test/mongodb/bin/mongod --dbpath /home/deploy/test/mongodb/data/db1-3 --port 11003 --replSet db1 --logpath=/home/deploy/test/mongodb/data/log/db1-3.log --pidfilepath=/home/deploy/test/mongodb/data/db1-3.pid & 
#副本集db2 启动命令

/home/deploy/test/mongodb/bin/mongod --dbpath /home/deploy/test/mongodb/data/db2-1 --port 21001 --replSet db2 --logpath=/home/deploy/test/mongodb/data/log/db2-1.log --pidfilepath=/home/deploy/test/mongodb/data/db2-1.pid & 


/home/deploy/test/mongodb/bin/mongod --dbpath /home/deploy/test/mongodb/data/db2-2 --port 21002 --replSet db2 --logpath=/home/deploy/test/mongodb/data/log/db2-2.log --pidfilepath=/home/deploy/test/mongodb/data/db2-2.pid & 


/home/deploy/test/mongodb/bin/mongod --dbpath /home/deploy/test/mongodb/data/db2-3 --port 21003 --replSet db2 --logpath=/home/deploy/test/mongodb/data/log/db2-3.log --pidfilepath=/home/deploy/test/mongodb/data/db2-3.pid & 
#副本集db3 启动命令

/home/deploy/test/mongodb/bin/mongod --dbpath /home/deploy/test/mongodb/data/db3-1 --port 31001 --replSet db3 --logpath=/home/deploy/test/mongodb/data/log/db3-1.log --pidfilepath=/home/deploy/test/mongodb/data/db3-1.pid & 


/home/deploy/test/mongodb/bin/mongod --dbpath /home/deploy/test/mongodb/data/db3-2 --port 31002 --replSet db3 --logpath=/home/deploy/test/mongodb/data/log/db3-2.log --pidfilepath=/home/deploy/test/mongodb/data/db3-2.pid & 


/home/deploy/test/mongodb/bin/mongod --dbpath /home/deploy/test/mongodb/data/db3-3 --port 31003 --replSet db3 --logpath=/home/deploy/test/mongodb/data/log/db3-3.log --pidfilepath=/home/deploy/test/mongodb/data/db3-3.pid & 
#所有副本集的停止命令
kill -2 `cat /home/deploy/test/mongodb/data/db1-1.pid`
kill -2 `cat /home/deploy/test/mongodb/data/db1-2.pid`
kill -2 `cat /home/deploy/test/mongodb/data/db1-3.pid`

kill -2 `cat /home/deploy/test/mongodb/data/db2-1.pid`
kill -2 `cat /home/deploy/test/mongodb/data/db2-2.pid`
kill -2 `cat /home/deploy/test/mongodb/data/db2-3.pid`

kill -2 `cat /home/deploy/test/mongodb/data/db3-1.pid`
kill -2 `cat /home/deploy/test/mongodb/data/db3-2.pid`
kill -2 `cat /home/deploy/test/mongodb/data/db3-3.pid`

#副本集的配置及初始化
./mongo  --port 11001
>config={
"_id":"db1",
"members":[
{"_id":0,"host":"192.168.1.31:11001"},
{"_id":1,"host":"192.168.1.31:11002"},
{"_id":2,"host":"192.168.1.31:11003"}
]
}

rs.initiate(config)
exit 

./mongo --port 21001
config={
"_id":"db2",
"members":[
{"_id":3,"host":"192.168.1.31:21001"},
{"_id":4,"host":"192.168.1.31:21002"},
{"_id":5,"host":"192.168.1.31:21003"}
]
}

rs.initiate(config)
exit

./mongo --port 31001
config={
"_id":"db3",
"members":[
{"_id":0,"host":"192.168.1.31:31001"},
{"_id":1,"host":"192.168.1.31:31002"},
{"_id":2,"host":"192.168.1.31:31003"}
]
}

rs.initiate(config)
exit

#配置库的启动命令
/home/deploy/test/mongodb/bin/mongod --configsvr  --dbpath /home/deploy/test/mongodb/data/configdb1 --port 20000 --logpath=/home/deploy/test/mongodb/data/log/configdb1.log --pidfilepath=/home/deploy/test/mongodb/data/cdb-1.pid &  
/home/deploy/test/mongodb/bin/mongod --configsvr --dbpath /home/deploy/test/mongodb/data/configdb2 --port 20001 --logpath=/home/deploy/test/mongodb/data/log/configdb2.log --pidfilepath=/home/deploy/test/mongodb/data/cdb-2.pid &
/home/deploy/test/mongodb/bin/mongod --configsvr --dbpath /home/deploy/test/mongodb/data/configdb3 --port 20002 --logpath=/home/deploy/test/mongodb/data/log/configdb3.log --pidfilepath=/home/deploy/test/mongodb/data/cdb-3.pid &
#配置库的停止命令
kill -2 `cat /home/deploy/test/mongodb/data/cdb-1.pid`
kill -2 `cat /home/deploy/test/mongodb/data/cdb-2.pid`
kill -2 `cat /home/deploy/test/mongodb/data/cdb-3.pid`


#启动mongos 
/home/deploy/test/mongodb/bin/mongos --port 30000 --configdb 192.168.1.31:20000,192.168.1.31:20001,192.168.1.31:20002 --logpath=/home/deploy/test/mongodb/data/log/mongos.log &  

#测试
./mongo --port 30000

use admin
sh.addShard("db1/192.168.1.31:11001")
sh.addShard("db2/192.168.1.31:21001")
sh.addShard("db3/192.168.1.31:31001")

db.runCommand({listshards:1})  

db.runCommand({"enablesharding":"test"})  
 
sh.shardCollection("test.person",{_id:'hashed'})  

use test

插入测试数据:
for(var i=0;i<10;i++){db.person.insert({name:"bluejoe"+i});}  

db.person.find()

exit

./mongo --port 11001
db.person.find() -- 查看分片情况
0 0
原创粉丝点击