mongodb分片+副本集集群环境搭建

1.准备工作
准备机器

 端口               28000              27017            27018            27019       27020
192.168.0.1        config             route          shardmaster    shardreplset    shardreplset
192.168.0.2        config             route          shardreplset   shardmaster     shardreplset
192.168.0.3        config             route          shardreplset   shardreplset    shardmaster

2.修改系统参数(3台)
cat >> /etc/sysctl.conf<

vm.overcommit_memory=1
net.core.somaxconn=1024
fs.file-max=655350

EOF
echo never >> /sys/kernel/mm/transparent_hugepage/enabled
echo never>>/sys/kernel/mm/transparent_hugepage/defrag

cat >>/etc/security/limits.conf<

* soft nproc 655350
* hard nproc 655350
* soft nofile 655350
* hard nofile 655350

EOF

sysctl -p
cat /etc/sysctl.conf
cat /etc/security/limits.conf
3.安装安装包,创建文件夹(3台)
cd /usr/local/
tar zxvf /root/mongodb-linux-x86_64-rhel62-3.5.10.tgz.gz
ln -s mongodb-linux-x86_64-rhel62-3.5.10 mongo
vi /etc/profile

export PATH=/usr/local/mongo/bin/

source /etc/profile
mkdir -p /etc/mongodb
mkdir -p /data/mongodb/configserver/
mkdir -p /data/mongodb/mongos/
mkdir -p /data/mongodb/shard1/
mkdir -p /data/mongodb/shard2/
mkdir -p /data/mongodb/shard3/
4.搭建config server(3台)
vi /etc/mongodb/config.conf

directoryperdb=true
pidfilepath = /data/mongodb/configserver/mongod.pid
dbpath      = /data/mongodb/configserver/
logpath     = /data/mongodb/configserver/mongod.log
logappend   = true
bind_ip= 0.0.0.0
port = 28000
fork  = true
slowms = 500
oplogSize=50
quiet=true
configsvr   = true
replSet  = configs
maxConns = 20000

mongod -f /etc/mongodb/config.conf
192.168.0.1上:
mongo --port 28000
初始化副本集

config = {
    _id : "configs",
    members : [
    {_id : 0, host : "192.168.0.1:28000" },
    {_id : 1, host : "192.168.0.2:28000" },
    {_id : 2, host : "192.168.0.3:28000" }
    ]
}

rs.initiate(config)

5.搭建shard master(3台)
vi /etc/mongodb/shard1.conf

directoryperdb=true
pidfilepath = /data/mongodb/shard1/mongod.pid
dbpath      = /data/mongodb/shard1/
logpath     =/data/mongodb/shard1/mongod.log
logappend   = true
bind_ip= 0.0.0.0
port = 27018  #第一台27018,第二台27019,第三台27020
fork  = true
slowms = 500
oplogSize=50
quiet=true
shardsvr = true
replSet  = shard1
maxConns = 20000

mongod -f /etc/mongodb/shard1.conf
vi /etc/mongodb/shard2.conf

directoryperdb=true
pidfilepath = /data/mongodb/shard2/mongod.pid
dbpath      = /data/mongodb/shard2/
logpath     =/data/mongodb/shard2/mongod.log
keyFile = /data/mongodb/shard2/mongodb.key
logappend   = true
bind_ip= 0.0.0.0
port = 27019  #第一台27019,第二台27020,第三台27018
fork  = true
slowms = 500
oplogSize=50
quiet=true
shardsvr    = true
replSet  = shard2
maxConns = 20000

mongod -f /etc/mongodb/shard2.conf
192.168.0.3上:
vi /etc/mongodb/shard3.conf

directoryperdb=true
pidfilepath = /data/mongodb/shard3/mongod.pid
dbpath      = /data/mongodb/shard3/
logpath     =/data/mongodb/shard3/mongod.log
keyFile = /data/mongodb/shard3/mongodb.key
logappend   = true
bind_ip= 0.0.0.0
port = 27020   #第一台27020,第二台27018,第三台27019
fork  = true
slowms = 500
oplogSize=50
quiet=true
shardsvr    = true
replSet  = shard3
maxConns = 20000

mongod -f /etc/mongodb/shard3.conf
192.168.0.1上:
mongo --port 27018
初始化副本集

config = {
    _id : "shard1",
    members : [
    {_id : 0, host : "192.168.0.1:27018" },
    {_id : 1, host : "192.168.0.2:27019" },
    {_id : 2, host : "192.168.0.3:27020" }
    ]
}
rs.initiate(config)

192.168.0.2上:
mongo --port 27020
初始化副本集

config = {
    _id : "shard2",
    members : [
    {_id : 0, host : "192.168.0.2:27020" },
    {_id : 1, host : "192.168.0.1:27019" },
    {_id : 2, host : "192.168.0.3:27018" }
    ]
}
rs.initiate(config)

192.168.0.3上:
mongo --port 27019
初始化副本集

config = {
    _id : "shard3",
    members : [
    {_id : 0, host : "192.168.0.3:27019" },
    {_id : 1, host : "192.168.0.2:27018" },
    {_id : 2, host : "192.168.0.1:27020" }
    ]
}
rs.initiate(config)

5.搭建route(3台)
vi /etc/mongodb/mongos.conf

pidfilepath = /data/mongodb/mongos/mongod.pid
logpath     =/data/mongodb/mongos/mongod.log
logappend   = true
bind_ip= 0.0.0.0
port = 27017
fork  = true
quiet=true
maxConns = 20000
configdb = configs/192.168.0.1:28000,192.168.0.2:28000,192.168.0.3:28000

mongos -f /etc/mongodb/mongos.conf
在任意一台例如192.168.0.1上
mongo --port 27017
use admin
sh.addShard(“shard1/192.168.0.1:27018,192.168.0.2:27019,192.168.0.3:27020”);
sh.addShard(“shard2/192.168.0.2:27020,192.168.0.1:27019,192.168.0.3:27018”);
sh.addShard(“shard3/192.168.0.3:27019,192.168.0.1:27020,192.168.0.2:27018”);
sh.status()
6.测试
sh.enableSharding(“cctest”)
sh.shardCollection(“cctest.user”,{name:1})
use cctest
for(var i=1;i<=100000;i++){db.user.save({_id:i,name:“user-”+i,age:18})}
发现数据库分片到了不同的shard里,搭建成功

你可能感兴趣的:(mongodb,mongodb,分片)