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<<EOF

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<<EOF

* 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里,搭建成功

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值