4台服务器,host1,host2,host3,host4.
2、部署方式
4个shard,每个shard由两个存储节点和一个仲裁节点组成replset。本部署方式采用最简部署架构,4台服务器组成环状集群。每个shard使用独立的端口号,以免混乱。各个shard的端口分别为:sd1:27001,sd2:27002, sd3:27003,sd4:27004。配置3个configserver端口为30000(网上有人说configserver为奇数个,但是我在官方文档上没有找到),mongos端口为40000。
3、安装及设置
3.1下载mongodb,我用的是2.0.5版本,然后解压,tarmongodb-2.0.5.tar.gz mongodb-2.0.5
3.3在data目录下新建sd1,sd2,sd3,sd4,configsvr目录
![Mongo集群安装与设置(shard+replset仲裁)[原创] Mongo集群安装与设置(shard+replset仲裁)[原创]](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)
3.4在conf目录下新建6个配置文件,configsvr.conf,mongos.conf,sd1.conf,sd2.conf,sd3.conf,sd4.conf
3.5修改配置文件内容
sd1~sd2注意红色为不一样的地方,sd2,把红色部分的1改为2即可
sd1.conf:
# where to store the data.
# Note: if you run mongodb as a non-root user (recommended) youmay
# need to create and set permissions for this directorymanually,
# e.g., if the parent directory isn't mutable by the mongodbuser.
dbpath=/home/zhangtieying/mongodb-2.0.5/data/sd1/
#where to log
logpath=/home/zhangtieying/mongodb-2.0.5/log/ sd1.log
logappend=true
fork = true
port = 27001
replSet = sd1
shardsvr = true
#configsvr = true
oplogSize = 200
rest = true
configsvr.conf:
# where to store the data.
# Note: if you run mongodb as a non-root user (recommended) youmay
# need to create and set permissions for this directorymanually,
# e.g., if the parent directory isn't mutable by the mongodbuser.
dbpath=/home/zhangtieying/mongodb-2.0.5/data/ configsvr/
#where to log
logpath=/home/zhangtieying/mongodb-2.0.5/log/ configsvr.log
logappend=true
fork = true
port = 30000
#replSet =sd1
#shardsvr =true
configsvr =true
#oplogSize =200
#rest = true
mongos.conf:
# mongos file
configdb =172.22.0.31:30000,172.22.0.32:30000,172.22.0.33:30000
port =40000
chunkSize = 64
logpath =/home/zhangtieying/mongodb-2.0.5/log/ mongos.log
logappend = true
fork = true
3.5把上述host1上的mongodb-2.0.5整个scp到host2、3、4上
3.6启动mongod服务:进入mongodb-2.0.5目录
在host1、2、3上启动shard1:./bin/mongod -f conf/sd1.conf
在host2、3、4上启动shard2:./bin/mongod -f conf/sd2.conf
在host3、4、1上启动shard3:./bin/mongod -f conf/sd3.conf
在host4、1、2上启动shard4:./bin/mongod -f conf/sd4.conf
3.7副本集配置
在hsot1、2、3任意一台机器均可:./bin/mongo --port 27001
配置完之后可以用rs.config()查看副本集状态。
配置replset2:
在hsot2、3、4任意一台机器均可:./bin/mongo --port 27002
配置replset3:
在hsot3、4、1任意一台机器均可:./bin/mongo --port 27003
配置replset4:
在hsot4、1、2任意一台机器均可:./bin/mongo --port 27004
3.8配置configserver
启动3个confiserver
host1:./bin/mongod -f conf/configsvr.conf
host2: ./bin/mongod -f conf/configsvr.conf
host3: ./bin/mongod -f conf/configsvr.conf
3.9 配置mongos
可以配置多个mongos,在本集群中暂时配置一个mongos
host1:./bin/mongos -f conf/mongos.conf
3.10 配置分片
登陆mongos(位于host1上):./bin/mongo --port 40000
![Mongo集群安装与设置(shard+replset仲裁)[原创]](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)
或:
到此集群配置完成,接下来就是新建用户,建库,建表,分库,分表,然后就可以进行查询插入操作了。
4、建库、建表
mongos->use ztytest
mongos->db.createCollection("docinfo")
注意上面的useztytest后如果不建表而离开的话,ztytest库并没有建立成功,mongodb会自动删掉ztytest这个库。
5、分库
mongos>db.runCommand({enablesharding:"ztytest"})
通过执行以上命令,可以让数据库跨shard,如果不执行这步,数据库只会存放在一个shard,一旦激活数据库分片,数据库中不同的collection将被存放在不同的shard上,但一个collection仍旧存放在同一个shard上,要使单个collection也分片,还需单独对collection作些操作。
可以用下面命令查看状态:
mongos> db.printShardingStatus()
6、分表
mongos>db.runCommand({shardcollection:"ztytest.docinfo",key:{id:1}})
注:
One note: a sharded collection can have only one unique index,which must exist on the shard key. No other unique indexes canexist on the collection.
用下面命令查看状态:
mongos> db.docinfo.stats()