mongodb4 集群分片安装
1.准备工作
1.1 参考地址
https://www.cnblogs.com/ityouknow/p/7344005.html
1.2 初始化目录
mkdir -p /data/mongodb/mongos/log
mkdir -p /data/mongodb/config/data
mkdir -p /data/mongodb/config/log
mkdir -p /data/mongodb/shard1/data
mkdir -p /data/mongodb/shard1/log
mkdir -p /data/mongodb/shard2/data
mkdir -p /data/mongodb/shard2/log
mkdir -p /data/mongodb/shard3/data
mkdir -p /data/mongodb/shard3/log
mkdir -p /home/work/mongodb/conf
1.3 软件准备
mkdir -p /home/work/software/
cd /home/work/software/
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.8.tgz
tar -zxvf mongodb-linux-x86_64-rhel70-4.2.8.tgz
mv mongodb-linux-x86_64-rhel70-4.2.8/* /home/work/mongodb/
sudo vim /etc/profile
export MONGODB_HOME=/home/work/mongodb
export PATH=$MONGODB_HOME/bin:$PATH
source /etc/profile
2. configsrc配置中心安装
2.1 config配置
cat << EOF > /home/work/mongodb/conf/config.conf
pidfilepath = /data/mongodb/config/log/configsrv.pid
dbpath = /data/mongodb/config/data
logpath = /data/mongodb/config/log/configsrv.log
logappend = true
bind_ip = 0.0.0.0
port = 21000
fork = true
#declare this is a config db of a cluster;
configsvr = true#副本集名称
replSet=configs
#设置最大连接数
maxConns=20000
EOF
2.2 systemctl 安装
cat << EOF > /usr/lib/systemd/system/mongodb_configsvr.service
[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
User=work
ExecStart= /home/work/mongodb/bin/mongod -f /home/work/mongodb/conf/config.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/home/work/mongodb/bin/mongod --shutdown --config /home/work/mongodb/conf/config.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
chmod 754 /usr/lib/systemd/system/mongodb_configsvr.service
systemctl start mongodb_configsvr.service
systemctl enable mongodb_configsvr.service
systemctl stop mongodb_configsvr.service
2.3 初始化
#登录任意一台初始化配置
/home/work/mongodb/bin/mongo --port 21000
config = {_id : "configs",members : [{_id : 0, host : "192.168.3.20:21000" }, {_id : 1, host : "192.168.3.21:21000" }, {_id : 2, host : "192.168.3.22:21000" }]}
rs.initiate(config)
3. 分片安装
3.1 配置分片
#配置分片
cat << EOF > /home/work/mongodb/conf/shard1.conf
pidfilepath = /data/mongodb/shard1/log/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
#打开web监控
#httpinterface=true
#rest=true
#副本集名称
replSet=shard1
#declare this is a shard db of a cluster;
shardsvr = true
#设置最大连接数
maxConns=20000
EOF
cat << EOF > /home/work/mongodb/conf/shard2.conf
pidfilepath = /data/mongodb/shard2/log/shard2.pid
dbpath = /data/mongodb/shard2/data
logpath = /data/mongodb/shard2/log/shard1.log
logappend = true
bind_ip = 0.0.0.0
port = 27002
fork = true
#打开web监控
#httpinterface=true
#rest=true
#副本集名称
replSet=shard2
#declare this is a shard db of a cluster;
shardsvr = true
#设置最大连接数
maxConns=20000
EOF
cat << EOF > /home/work/mongodb/conf/shard3.conf
pidfilepath = /data/mongodb/shard3/log/shard3.pid
dbpath = /data/mongodb/shard3/data
logpath = /data/mongodb/shard3/log/shard3.log
logappend = true
bind_ip = 0.0.0.0
port = 27003
fork = true
#打开web监控
#httpinterface=true
#rest=true
#副本集名称
replSet=shard3
#declare this is a shard db of a cluster;
shardsvr = true
#设置最大连接数
maxConns=20000
EOF
3.2 systemctl 安装
#修改里面的启动配置文件
#这里有修改里面配置文件地址
cp /usr/lib/systemd/system/mongodb_configsvr.service /usr/lib/systemd/system/mongodb_shard1.service
cp /usr/lib/systemd/system/mongodb_configsvr.service /usr/lib/systemd/system/mongodb_shard2.service
cp /usr/lib/systemd/system/mongodb_configsvr.service /usr/lib/systemd/system/mongodb_shard3.service
chmod 754 /usr/lib/systemd/system/mongodb_shard1.service
systemctl start mongodb_shard1.service
systemctl enable mongodb_shard1.service
systemctl start mongodb_shard2.service
systemctl enable mongodb_shard2.service
systemctl start mongodb_shard3.service
systemctl enable mongodb_shard3.service
3.3 初始化分片
#登录任意一台初始化分片
#注意 设置了arbiterOnly属性的,不能在本台机器运行,否则报错
/home/work/mongodb/bin/mongo --port 27001
config = {_id : "shard1",members : [{_id : 0, host : "192.168.3.20:27001" }, {_id : 1, host : "192.168.3.21:27001" }, {_id : 2, host : "192.168.3.22:27001", arbiterOnly: true }]}
rs.initiate(config)
/home/work/mongodb/bin/mongo --port 27002
config = {_id : "shard2",members : [{_id : 0, host : "192.168.3.20:27002" , arbiterOnly: true}, {_id : 1, host : "192.168.3.21:27002" }, {_id : 2, host : "192.168.3.22:27002"}]}
rs.initiate(config)
/home/work/mongodb/bin/mongo --port 27003
config = {_id : "shard3",members : [{_id : 0, host : "192.168.3.20:27003" }, {_id : 1, host : "192.168.3.21:27003" , arbiterOnly: true}, {_id : 2, host : "192.168.3.22:27003"}]}
rs.initiate(config)
4. mongos安装
4.1 mongos配置
#配置mongos
cat << EOF > /home/work/mongodb/conf/mongos.conf
pidfilepath = /data/mongodb/mongos/log/mongos.pid
logpath = /data/mongodb/mongos/log/mongos.log
logappend = true
bind_ip = 0.0.0.0
port = 20000
fork = true
#监听的配置服务器,只能有1个或者3个 configs为配置服务器的副本集名字
configdb = configs/192.168.3.20:21000,192.168.3.21:21000,192.168.3.22:21000
#设置最大连接数
maxConns=20000
EOF
4.2 systemctl 安装
cat << EOF > /usr/lib/systemd/system/mongos.service
[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
User=work
ExecStart= /home/work/mongodb/bin/mongos -f /home/work/mongodb/conf/mongos.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
chmod 754 /usr/lib/systemd/system/mongos.service
systemctl start mongos.service
systemctl enable mongos.service
4.3 串联分片
#串联分片
/home/work/mongodb/bin/mongo --port 20000
use admin
sh.addShard("shard1/192.168.3.20:27001,192.168.3.21:27001,192.168.3.22:27001")
sh.addShard("shard2/192.168.3.20:27002,192.168.3.21:27002,192.168.3.22:27002")
sh.addShard("shard3/192.168.3.20:27003,192.168.3.21:27003,192.168.3.22:27003")
5.mongos 常用操作
5.1 集群操作
#mongodb常用操作
查看集群状态
sh.status()
查看balance状态
mongos> sh.getBalancerState()
#设置balance窗口 这里必须设置一下,不然极度耗cpu内存,这里把迁移放在用户较少时候
db.settings.update({ _id : "balancer" }, { $set : { activeWindow : { start : "23:00", stop : "6:00" } } }, true )
#查看
mongos> sh.getBalancerWindow()
5.2 分片操作
#分片命令
db.runCommand( { enablesharding :"testdb"});
db.runCommand( { shardcollection : "testdb.table1",key : {id: 1} } )
#查看分片效果
db.table1.stats()
#组合分片是比较好的一种分片的选择,好的组合分片可以同时解决热点和随机读IO问题。例如:
sh.shardCollection("test.bbbb",{"username":1,"_id":1});
具体可以参考百度里面分片的一些知识