主從架構(Master-Slave)、副本集架構(Replica Set)、數據分片架構(Sharding)linux
這裏安裝集羣的方式爲副本集架構(Replica Set)git
Primary:主節點,惟一 Secondary:從節點,同步主節點的數據,能夠多個實例,可只讀 Hidden:備份節點,不處理任何客戶端驅動請求 Secondary-Only:只能爲從節點,防止一些性能不高的節點成爲主節點 Non-Voting:沒有選舉權的 secondary 節點,備份使用,雖然不清楚和隱藏節點的差異是什麼 Arbiter:仲裁節點,不存數據,只參與仲裁,可選github
cd /root/mongodb yum install -y *
定義變量 NETWORK_ETH='eth0' ip=`ip address show dev ${NETWORK_ETH} | grep 'inet ' | awk -F ' ' '{print $2}' | cut -d / -f 1` #ip=`ifconfig eth1 | grep 'inet ' | awk -F ' ' '{print $2}' | cut -d : -f 2` confPath='/etc/mongod.conf' mongoDataPath='/data/mongodb/data' mongopath='/data/mongodb'
mkdir -p ${mongopath} mkdir -p ${mongoDataPath} # [path to mongdb data]
cat > ${confPath} <<EOF dbpath=${mongoDataPath} logpath=${mongopath}/mongo.log pidfilepath=${mongopath}/mongo.pid directoryperdb=true logappend=true replSet=tnebula bind_ip=${ip} port=27017 #auth=true oplogSize=100 fork=true noprealloc=true #maxConns=4000 EOF
mongod -f ${confPath}
其餘兩臺副本節點(secondary)、仲裁節點(arbiter)也執行以上操做mongodb
mongo 172.20.10.5:27017 use admin #[根據現場修改IP] cfg={ _id:"tnebula",members:[{_id:0,host:'172.26.1.14:27017',priority:2},{_id:1,host:'172.26.1.15:27017',priority:1},{_id:2,host:'172.26.1.16:27017',arbiterOnly:true}] }; rs.initiate(cfg) #檢測集羣狀態 rs.status();
mongo mongodb://172.26.1.14:27017,172.26.1.15:27017,172.26.1.16:27017/?replicaSet=tnebula
# mongodb導出、整庫導出 去掉 -d mongodump -h 172.26.1.14:27017 -o -d test mongodb_bak20200319 # mongodb導入 mongorestore --host 172.26.1.14:27017 --port 27017 --username mongouser --password "password" --authenticationDatabase=admin --dir=./mongodb_bak20200319 --drop
一、關閉一直處於RECOVERING狀態的mongodb server /opt/mongodb/mongodb-linux-x86_64-2.4.8/bin/mongo 127.0.0.1:22001 use admin db.shutdownServer() 二、將原數據目錄更名,新建數據目錄,再啓動mongodb實例 mv /opt/mongodb/shard1/data /opt/mongodb/shard1/data_bak mkdir /opt/mongodb/shard1/data /opt/mongodb/mongodb-linux-x86_64-2.4.8/bin/mongod --shardsvr --replSet shard1 --port 22001 --dbpath /opt/mongodb/shard1/data --logpath /opt/mongodb/shard1/log/shard1.log --fork 三、查看恢復狀態,爲STARTUP2 /opt/mongodb/mongodb-linux-x86_64-2.4.8/bin/mongo 127.0.0.1:22001 use admin rs.status()
本人也編寫了ansible-playbook一鍵部署mongodb集羣,github:https://github.com/xiaowenxiao/deploy/tree/master/ansible-mongodb-cluster-deploy架構