1.环境准备
centos6.6 x64 3台
192.168.0.23
192.168.0.24
192.168.0.201
2.集群规划
每个机器同时启动mongos、config server 、shard0、shard2、shard3,
config server得ip端口分别为:192.168.0.23:1111,192.168.0.24:1111,192.168.0.201:1111
mongos得ip端口分别为:192.168.0.23:2222,192.168.0.24:2222,192.168.0.201:2222
shard0得ip端口分别为: 192.168.0.23:3333,192.168.0.24:3333,192.168.0.201:3333
shard1得ip端口分别为: 192.168.0.23:4444,192.168.0.24:4444,192.168.0.201:4444
shard2得ip端口分别为: 192.168.0.23:5555,192.168.0.24:5555,192.168.0.201:5555
3.软件版本:
https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-amazon-3.2.6.tgz
4.每台分别安装mongodb并创建好响应的目录
wget -c https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-amazon-3.2.6.tgz
tar xf mongodb-linux-x86_64-amazon-3.2.6.tgz
mkdir -pv /export/{configsvr,shard0,shard1,shard2}/data
5.在每台机器上启动config server
创建配置文件:
vim configsvr.conf
dbpath = /export/mongodb/configsvr/data/
port = 1111
configsvr = true
fork = true
logpath=/export/log/mongconfigsvr.log
logappend=true
maxConns=1000
pidfilepath=/var/run/mongconfigsvr.pid
httpinterface=true
启动configsvr
mkdir -pv /export/log/
cd /export/log/
touch mongconfigsvr.log
/opt/mongodb3.2.6/bin/mongod -f /opt/mongodb3.2.6/configsvr.conf
netstat -tlnup 1111端口启动
6.在每台机器上配置路由
创建mongos的配置文件
vim mongos.conf
port = 2222
maxConns=1000
logpath=/var/log/mongos.log
logappend=true
logRotate=rename
pidfilepath=/var/run/mongos.pid
fork=true
httpinterface=true
configdb = 192.168.0.23:1111,192.168.0.24:1111,192.168.0.201:1111
启动路由:
/opt/mongodb3.2.6/bin/mongos -f /opt/mongodb3.2.6/mongos.conf
注意这里有的机器时间不一样会报错,报错日志有一个error checking clock skew of cluster
请同步时间
7.在每台机器上配置副本集分片shard0
创建shard的配置文件
vim shard0.conf
port=3333
maxConns=1000
logpath=/var/log/shard0.log
logappend=true
logRotate=rename
pidfilepath=/var/run/shard0.pid
fork=true
cpu=true
dbpath=/export/mongodb/shard0/data
oplogSize=1000
replSet = testrs0
rest = true
httpinterface=true
启动分片
/opt/mongodb3.2.6/bin/mongos -f /opt/mongodb3.2.6/mongos.conf
#注意下面的操作在一起机器上操作即可,要不会产生一个没有主节点的问题,如果差生了请删除在单台机器上操作即可。
/opt/mongodb3.2.6/bin/mongo 127.0.0.1:3333
use admin
#将其他的分片添加进来
config={_id:"testrs0", members: [{"_id":0,"host":"192.168.0.23:3333"},{"_id":1,"host":"192.168.0.24:3333"},{"_id":2,"host":"192.168.0.201:3333"}]}
#使用config配置初始化
rs.initiate(config)
#查看主节点
db.isMaster()
#查看副本集信息
rs.status()
配置副本集分片2
port=4444
maxConns=1000
logpath=/var/log/sahrd1.log
logappend=true
logRotate=rename
pidfilepath=/var/run/shard1.pid
fork=true
cpu=true
dbpath=/export/mongodb/shard1/data
oplogSize=1000
replSet = testrs1
rest = true
httpinterface=true
启动分片
/opt/mongodb3.2.6/bin/mongo 127.0.0.1:4444
use admin
config={_id:"testrs1", members: [{"_id":0,"host":"192.168.0.23:4444"},{"_id":1,"host":"192.168.0.24:4444"},{"_id":2,"host":"192.168.0.201:4444"}]}
配置副本集分片3
port=5555
maxConns=1000
logpath=/var/log/sahrd2.log
logappend=true
logRotate=rename
pidfilepath=/var/run/shard2.pid
fork=true
cpu=true
dbpath=/export/mongodb/shard2/data
oplogSize=1000
replSet = testrs2
rest = true
httpinterface=true
启动分片
/opt/mongodb3.2.6/bin/mongo 127.0.0.1:4444
use admin
config={_id:"testrs2", members: [{"_id":0,"host":"192.168.0.23:5555"},{"_id":1,"host":"192.168.0.24:5555"},{"_id":2,"host":"192.168.0.201:555"}]}
8.添加副本集
/opt/mongodb3.2.6/bin/mongo 127.0.0.1:2222
mongos> use admin
switched to db admin
mongos> db.runCommand( { addshard : "testrs0/192.168.0.23:3333,192.168.0.24:3333,192.168.0.201:3333"})
{ "shardAdded" : "testrs0", "ok" : 1 }
mongos> db.runCommand( { addshard : "testrs1/192.168.0.23:4444,192.168.0.24:4444,192.168.0.201:4444"})
{ "shardAdded" : "testrs1", "ok" : 1 }
mongos> db.runCommand( { addshard : "testrs2/192.168.0.23:5555,192.168.0.24:5555,192.168.0.201:5555"})
{ "shardAdded" : "testrs2", "ok" : 1 }
查看分片服务器的配置
db.runCommand( { listshards : 1 } );
指定testdb分片生效
mongos> use admin
switched to db admin
mongos> db.runCommand( { enablesharding :"testdb"});
{ "ok" : 1 }
mongos> use admin
switched to db admin
mongos> db.runCommand( { shardcollection : "testdb.tid",key : {id: 1} } )
我们设置testdb的 table1 表需要分片,根据 id 自动分片到 shard0 ,shard1,shard2 上面去。要这样设置是因为不是所有mongodb 的数据库和表都需要分片!