Mongo集群安装与设置(分片+副本集)
2015-01-04 10:18
295 查看
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目录
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写错或报如下错误:
或:
到此集群配置完成,接下来就是新建用户,建库,建表,分库,分表,然后就可以进行查询插入操作了。
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()
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目录
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写错或报如下错误:
或:
到此集群配置完成,接下来就是新建用户,建库,建表,分库,分表,然后就可以进行查询插入操作了。
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()
相关文章推荐
- mongodb数据库安装 & 设置管理员密码(单独mongo,无副本集)
- Hadoop集群安装设置
- Mongo服务器集群配置学习二——副本集
- Mongobd Replica Set(副本集) 集群搭建
- VMware虚拟机内安装的Ubuntu 16.04设置静态IP地址 spark2.0集群固定IP配置
- mongodb 3.2.7版本 的分片集群安装demo
- hadoop集群搭建(ip设置和jdk安装)
- Virtualbox搭建局域网设置,安装ambari hadoop集群管理第一步
- MongoDB主从 副本集 分片集群安装
- mongodb分片集群安装教程
- Mongo服务器集群配置学习三——分片
- 构建一个Mongo分片集群
- 安装部署MONGODB分片集群
- 安装mongodb副本分片集群
- VMware下Hadoop 2.4.1完全分布式集群平台安装与设置
- Mongo服务器集群配置学习三——分片
- 源码安装mysql-cluster-gpl-7.2.15.tar.gz 及 ndb集群设置
- 【Mongodb】 Sharding Replica Sets 分片集群生产环境安装与介绍!
- mongo集群分片
- VMware虚拟机内安装的Ubuntu 16.04设置静态IP地址 spark2.0集群固定IP配置