一、mongodb分片介绍

  

 二、分片搭建

 

三台机器都创建一下目录和配置文件,注意修改IP

mkdir -p /data/mongodb/mongos/log
 mkdir -p /data/mongodb/config/{data,log}
 mkdir -p /data/mongodb/shard1/{data,log}
 mkdir -p /data/mongodb/shard2/{data,log}
 mkdir -p /data/mongodb/shard3/{data,log}

mkdir /etc/mongod/
 vim /etc/mongod/config.conf

pidfilepath=/var/run/mongodb/configsrv.pid
dbpath=/data/mongodb/config/data
logpath=/data/mongodb/config/log/congigsrv.log
logappend=true
bind_ip=192.168.134.130
port=21000
fork=true
configsvr=true
replSet=configs
maxConns=20000

三台机器都启动config,server

mongod -f /etc/mongod/config.conf

登录任意一台,执行以下命令

config={_id:"configs",members:[{_id:0,host:"192.168.134.130:21000"},{_id:1,host:"192.1
68.134.134:21000"},{_id:2,host:"192.168.134.150:21000"}]}

rs.initiate(config)

  

三台都创建shard1.conf,shard2.conf,shard3.conf,注意修改shard后的数字和端口

pidfilepath=/var/run/mongodb/shard1.pid
dbpath=/data/mongodb/shard1/data
logpath=/data/mongodb/shard1/log/shard1.log
logappend=true
bind_ip=0.0.0.0
port=27001
fork=true
httpinterface=true
rest=true
replSet=shard1
shardsvr=true
maxConns=20000

三台机器都启动shard1.conf,shard2.conf,shard3.conf服务

mongod  -f  /etc/mongod/shard1.conf

mongod  -f  /etc/mongod/shard2.conf

mongod  -f  /etc/mongod/shard3.conf

在130或134上执行以下命令,150不行,因为150被的端口被作为了仲裁。

mongo --port 27001

config={_id:"shard1",members:[{_id:0,host:"192.168.134.130:27001"},{_id:1,host:"192.16
8.134:27001"},{_id:2,host:"192.168.134.150:27001",arbiterOnly:true}]}

rs.initiate(config)

在150或134上执行以下命令,130不行,因为130被的端口被作为了仲裁。

mongo --port 27002

config={_id:"shard2",members:[{_id:0,host:"192.168.134.130:27002",arbiterOnly:true},{
_id:1,host:"192.168.134.134:27002"},{_id:2,host:"192.168.134.150:27002"}]}

rs.initiate(config)

在130或150上执行以下命令,134不行,因为134被的端口被作为了仲裁。

mongo --port 27003

config={_id:"shard3",members:[{_id:0,host:"192.168.134.130:27003"},{_id:1,host:"192.1
68.134.134:27003",arbiterOnly:true},{_id:2,host:"192.168.134.150:27003"}]}

rs.initiate(config)

 

  

  

三台机器创建monggos.conf

pidfilepath=/var/run/mongodb/mongos.pid
logpath=/data/mongodb/mongos/log/mongos.log
logappend=true
bind_ip=0.0.0.0
port=20000
fork=true
configdb=configs/192.168.134.130:21000,192.168.134.134:21000,192.168.134.150:21000
maxConns=20000

三台都启动该服务

monggos -f /etc/mongod/mongos.conf

登录任意一台

sh.addShard("shard1/192.168.134.130:27001,192.168.134.134:27001,192.168.134.150:27001")

sh.addShard("shard2/192.168.134.130:27002,192.168.134.134:27002,192.168.134.150:27002")

sh.addShard("shard3/192.168.134.130:27003,192.168.134.134:27003,192.168.134.150:27003")

 sh.status()

测试

show dbs

sh.status()

备份与恢复

 

posted on 2018-06-04 22:08  天梭  阅读(189)  评论(0编辑  收藏  举报