生产环境MongoDB 分片与集群 方案
生产环境MongoDB 分片与集群 方案
一、 mongodb分片与集群拓扑图
二、分片与集群的部署
1.Mongodb的安装
分别在以上3台服务器安装好mongodb
安装方法见安装脚本。
2.Mongod 创建单个分片的副本集
10.68.4.209
①建立数据文件夹和日志文件夹
mdkir /data/{master,slave,arbiter}
mkdir /data/log/mongodb/{master,slave,arbiter} -p
②建立配置文件
#master.conf
dbpath=/data/master
logpath=/data/log/mongodb/master/mongodb.log
pidfilepath=/var/run/mongo_master.pid
#directoryperdb=true
logappend=true
replSet=policydb
port=10002
oplogSize=10000
fork=true
noprealloc=true
profile=1
slowms=200
#slave.conf
dbpath=/data/slave
logpath=/data/log/mongodb/slave/mongodb.log
pidfilepath=/var/run/mongo_slave.pid
#directoryperdb=true
logappend=true
replSet=policydb
port=10001
oplogSize=10000
fork=true
noprealloc=true
profile=1
slowms=200
#arbiter.conf
dbpath=/data/arbiter
logpath=/data/log/mongodb/arbiter/mongodb.log
pidfilepath=/var/run/mongo_arbiter.pid
#directoryperdb=true
logappend=true
replSet=policydb
port=10000
oplogSize=10000
fork=true
noprealloc=true
profile=1
slowms=200
③启动mongodb
/etc/init.d/mongodb_master start
/etc/init.d/mongodb_slave start
/etc/init.d/mongodb_arbiter start
④配置主、备、仲裁节点
主节点:
# /usr/local/mongodb/bin/mongo 10.68.4.209:10002
MongoDB shell version: 2.4.9
connecting to: 10.68.4.209:10002/test
> use admin
switched to db admin
>config={ _id:"policydb", members:[ {_id:0,host:'10.68.4.209:10002',priority:2}, {_id:1,host:'10.68.4.209:10001',priority:1},
... {_id:2,host:'10.68.4.209:10000',arbiterOnly:true}] };
{
"_id" : "policydb",
"members" : [
{
"_id" : 0,
"host" : "10.68.4.209:10002",
"priority" : 2
},
{
"_id" : 1,
"host" : "10.68.4.209:10001",
"priority" : 1
},
{
"_id" : 2,
"host" : "10.68.4.209:10000",
"arbiterOnly" : true
}
]
}
rs.initiate(config) #初始化
rs.status() #查看集群状态
10.68.4.29
①建立数据文件夹和日志文件夹
mdkir /data/{master,slave,arbiter}
mkdir /data/log/mongodb/{master,slave,arbiter} -p
②建立配置文件
#master.conf
dbpath=/data/master
logpath=/data/log/mongodb/master/mongodb.log
pidfilepath=/var/run/mongo_master.pid
#directoryperdb=true
logappend=true
replSet=policydb2
port=10002
oplogSize=10000
fork=true
noprealloc=true
profile=1
slowms=200
#slave.conf
dbpath=/data/slave
logpath=/data/log/mongodb/slave/mongodb.log
pidfilepath=/var/run/mongo_slave.pid
#directoryperdb=true
logappend=true
replSet=policydb2
port=10001
oplogSize=10000
fork=true
noprealloc=true
profile=1
slowms=200
#arbiter.conf
dbpath=/data/arbiter
logpath=/data/log/mongodb/arbiter/mongodb.log
pidfilepath=/var/run/mongo_arbiter.pid
#directoryperdb=true
logappend=true
replSet=policydb2
port=10000
oplogSize=10000
fork=true
noprealloc=true
profile=1
slowms=200
③启动mongodb
/etc/init.d/mongodb_master start
/etc/init.d/mongodb_slave start
/etc/init.d/mongodb_arbiter start
④配置主、备、仲裁节点
主节点:
# /usr/local/mongodb/bin/mongo 10.68.4.209:10002
MongoDB shell version: 2.4.9
connecting to: 10.68.4.209:10002/test
> use admin
switched to db admin
> config={ _id:"policydb2", members:[ {_id:0,host:'10.68.4.29:10002',priority:2}, {_id:1,host:'10.68.4.29:10001',priority:1},
... {_id:2,host:'10.68.4.209:10000',arbiterOnly:true}] };
{
"_id" : "policydb",
"members" : [
{
"_id" : 0,
"host" : "10.68.4.29:10002",
"priority" : 2
},
{
"_id" : 1,
"host" : "10.68.4.29:10001",
"priority" : 1
},
{
"_id" : 2,
"host" : "10.68.4.29:10000",
"arbiterOnly" : true
}
]
}
rs.initiate(config) #初始化
rs.status() #查看集群状态
10.68.4.30
①建立数据文件夹和日志文件夹
mdkir /data/{master,slave,arbiter}
mkdir /data/log/mongodb/{master,slave,arbiter} -p
②建立配置文件
#master.conf
dbpath=/data/master
logpath=/data/log/mongodb/master/mongodb.log
pidfilepath=/var/run/mongo_master.pid
#directoryperdb=true
logappend=true
replSet=policydb3
port=10002
oplogSize=10000
fork=true
noprealloc=true
profile=1
slowms=200
#slave.conf
dbpath=/data/slave
logpath=/data/log/mongodb/slave/mongodb.log
pidfilepath=/var/run/mongo_slave.pid
#directoryperdb=true
logappend=true
replSet=policydb3
port=10001
oplogSize=10000
fork=true
noprealloc=true
profile=1
slowms=200
#arbiter.conf
dbpath=/data/arbiter
logpath=/data/log/mongodb/arbiter/mongodb.log
pidfilepath=/var/run/mongo_arbiter.pid
#directoryperdb=true
logappend=true
replSet=policydb3
port=10000
oplogSize=10000
fork=true
noprealloc=true
profile=1
slowms=200
③启动mongodb
/etc/init.d/mongodb_master start
/etc/init.d/mongodb_slave start
/etc/init.d/mongodb_arbiter start
④配置主、备、仲裁节点
主节点:
# /usr/local/mongodb/bin/mongo 10.68.4.209:10002
MongoDB shell version: 2.4.9
connecting to: 10.68.4.209:10002/test
> use admin
switched to db admin
> config={ _id:"policydb3", members:[ {_id:0,host:'10.68.4.30:10002',priority:2}, {_id:1,host:'10.68.4.30:10001',priority:1},
... {_id:2,host:'10.68.4.30:10000',arbiterOnly:true}] };
{
"_id" : "policydb",
"members" : [
{
"_id" : 0,
"host" : "10.68.4.30:10002",
"priority" : 2
},
{
"_id" : 1,
"host" : "10.68.4.30:10001",
"priority" : 1
},
{
"_id" : 2,
"host" : "10.68.4.30:10000",
"arbiterOnly" : true
}
]
}
rs.initiate(config) #初始化
rs.status() #查看集群状态
2.Mongod 创建单个分片的配置服务器
① 创建配置目录
10.68.4.209 mkdir /data/config
10.68.4.29 mkdir /data/config
10.68.4.30 mkdir /data/config
②准备配置服务器的配置文件
3个服务器的配置服务器的配置文件一致
#config.conf
dbpath=/data/config
logpath=/data/log/mongodb/config/mongodb.log
pidfilepath=/var/run/mongo_config.pid
directoryperdb=true
logappend=true
port=10003
fork=true
configsvr=true
③启动配置服务器
/etc/init.d/mongodb_config start
3.Mongod 创建并配置mongos和开启分片模式
① 创建日志目录
Mkdir -p /data/log/mongodb/mongos/
② 准备mongos的配置文件
#mongos.conf
logpath=/data/log/mongodb/mongos/mongodb.log
pidfilepath=/var/run/mongo_mongos.pid
logappend=true
port=10004
fork=true
configdb=10.68.4.209:10003,10.68.4.29:10003,10.68.4.30:1000
③ 启动mongos
/etc/init.d/mongodb_mongos start
④ 配置分片
sh.addShard("policydb/10.68.4.209:10002")
sh.addShard("policydb2/10.68.4.29:10002")
sh.addShard("policydb3/10.68.4.30:10002")
sh.enableSharding("policydb")
db.runCommand({"shardcollection":"policydb.fullPolicyTextInfo_history", "key":{"key":1}})
db.printShardingStatus() #查看分片状态
sh.status({verbose:true})
sh.status()
3.快速创建副本集和配置服务脚本
上图
1.图1
2.图2 config.conf配置文件
3.图3
MongoDB 的详细介绍:请点这里
MongoDB 的下载地址:请点这里