Mongo集群安装与设置(分片+副本集)

本文详细介绍了MongoDB的分片与副本集部署过程,包括集群的搭建、配置及状态检查,帮助读者掌握MongoDB的分布式部署技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、硬件环境
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、安装及设置
  先在host1上安装配置,然后再scp到其他机器上。
3.1下载mongodb,我用的是2.0.5版本,然后解压,tarmongodb-2.0.5.tar.gz mongodb-2.0.5



3.2在mongodb-2.0.5目录下新建data目录,log目录,和conf目录



3.3在data目录下新建sd1,sd2,sd3,sd4,configsvr目录
Mongo集群安装与设置(shard+replset仲裁)[原创]


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

   以下命令查看是否正常启动:

   # netstat-lnpt # 或 ps -ef | grep ./bin/mongod


3.7副本集配置

配置replset1:
在hsot1、2、3任意一台机器均可:./bin/mongo --port 27001
    >useadmin
     
      >config= {_id:"sd1", members: [

          {_id: 0, host:"172.22.0.31:27001"},

          {_id: 1, host:"172.22.0.32:27001"},

          {_id: 2, host:"172.22.0.33:27001","arbiterOnly":true}]

       };

   >rs.initiate(config)

配置完之后可以用rs.config()查看副本集状态。


配置replset2:

在hsot2、3、4任意一台机器均可:./bin/mongo --port 27002
   >useadmin
   
   >config= {_id:"sd2", members: [

          {_id: 0, host:"172.22.0.32:27002"},

          {_id: 1, host:"172.22.0.33:27002"},

          {_id: 2, host:"172.22.0.34:27002","arbiterOnly":true}]

       };

   >rs.initiate(config)

   >exit

配置replset3:

在hsot3、4、1任意一台机器均可:./bin/mongo --port 27003
   >useadmin
   
   >config= {_id:"sd3", members: [

          {_id: 0, host:"172.22.0.33:27003"},

          {_id: 1, host:"172.22.0.34:27003"},

          {_id: 2, host:"172.22.0.31:27003","arbiterOnly":true}]

       };

   >rs.initiate(config)

   >exit

配置replset4:

在hsot4、1、2任意一台机器均可:./bin/mongo --port 27004
   >useadmin
   
   >config= {_id:"sd4", members: [

          {_id: 0, host:"172.22.0.34:27004"},

          {_id: 1, host:"172.22.0.31:27004"},

          {_id: 2, host:"172.22.0.32:27004","arbiterOnly":true}]

       };

   >rs.initiate(config)

   >exit

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

  mongos> use admin
   #maxsize:20480 #单位 mb 分片限制大小 根据实际服务器来定
 

  mongos>db.runCommand({addshard:"sd1/172.22.0.31:27001,172.22.0.32:27001,172.22.0.33:27001",name:"sd1",maxsize:20480} )

   mongos>db.runCommand({addshard:"sd2/172.22.0.32:27002,172.22.0.33:27002,172.22.0.34:27002",name:"sd2",maxsize:20480} )

   mongos>db.runCommand({addshard:"sd3/172.22.0.33:27003,172.22.0.34:27003,172.22.0.31:27003",name:"sd3",maxsize:20480} )

   mongos>db.runCommand({addshard:"sd4/172.22.0.34:27004,172.22.0.31:27004,172.22.0.32:27004",name:"sd4",maxsize:20480} )

   命令检查分片添加情况,如出现以下结果则表示配置成功:

      mongos>db.runCommand( {listshards : 1 } )
   
    若上述sd1写错或报如下错误:
Mongo集群安装与设置(shard+replset仲裁)[原创]

或:



到此集群配置完成,接下来就是新建用户,建库,建表,分库,分表,然后就可以进行查询插入操作了。

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}})

注:
  a.  分片的collection系统会自动创建一个索引(也可用户提前创建好)
  b.分片的collection只能有一个在分片key上的唯一索引,其它唯一索引不被允许
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()


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值