安裝部署 mongo
前題準(zhǔn)備
官方下載地址
https://www.mongodb.com/try/download/community
安裝mongodb
ls mongodb-linux-x86_64-rhel70-4.2.12.tgz
tar -zvxf mongodb-linux-x86_64-rhel70-4.2.12.tgz
mv mongodb-linux-x86_64-rhel70-4.2.12.tgz /usr/local/mongodb
cd /usr/local/mongodb
ls
bin LICENSE-Community.txt MPL-2 README THIRD-PARTY-NOTICES THIRD-PARTY-NOTICES.gotools
創(chuàng)建源文件
初始化mongo 數(shù)據(jù)目錄
# 初始化文件目錄
mkdir ${mongo_path}
mkdir ${mongo_path}/log
cd ${mongo_path}
# 初始化 keyFile 和 mongodb.cnf(配置文件)
echo "XXXXXXXX" > keyFile
chmod 4000 keyFile
vim mongodb.cnf
chown mongo.mongo -R ${mongo_path}
ll
total 8
-r-------- 1 mongo mongo 17 Feb 18 21:22 keyFile
drwxr-xr-x 2 mongo mongo 6 Feb 18 21:21 log
-rw-r--r-- 1 mongo mongo 718 Feb 18 21:21 mongodb.cnf
mongodb 配置文件參考
#SERVER
fork = true
port = ${mongo_port}
quiet = true
dbpath = ${mongo_path}
logpath = ${mongo_path}/log/mongod.log
logappend = true
journal = true
directoryperdb = true
maxConns=20000
#>=3.6
#bind_ip_all = true
#noIndexBuildRetry=true
#serviceExecutor=adaptive
#SLOW_LOG
#profile = 1
slowms = 300
#RS
#replSet = ${mongo_replSet}
oplogSize = 20480
# add for 3.4
#shardsvr=true
#SCO
#configsvr = true
#configdb = MSCHOST
#setParameter=enableLocalhostAuthBypass=1
#USER
keyFile=${mongo_path}/keyFile
setParameter=enableLocalhostAuthBypass=1
setParameter=failIndexKeyTooLong=1
storageEngine=wiredTiger
wiredTigerCacheSizeGB=15
wiredTigerCollectionBlockCompressor=snappy
#VER
#mongo_version=mongodb40
vim修改配置文件
# 替換\${mongo_path} 為 /data1/mongodb7666
:%s/${mongo_path}/\/data1\/mongodb7666/g
# 替換 ${mongo_replSet} 和 ${mongo_port} 為 7666
:%s/${mongo_replSet}/7666/g
:%s/${mongo_port}/7666/g
啟動(dòng)mongo
sudo -u mongo numactl --interleave=all /usr/local/mongodb/bin/mongod -f ${mongo_path}/mongodb.cnf &>/dev/null
配置副本集
啟動(dòng)master 節(jié)點(diǎn)
# 啟動(dòng)節(jié)點(diǎn)
sudo -u mongo numactl --interleave=all /usr/local/mongodb/bin/mongod -f ${mongo_path}/mongodb.cnf &>/dev/null
# 配置副本集參數(shù)
/usr/local/mongodb/bin/mongo 127.0.0.1:${mongo_port}/admin --eval 'rs.initiate('{_id:"${mongo_port}",version:1,writeConcernMajorityJournalDefault:false,members:[{_id:0,host:"${mongo_node_ip}:${mongo_port}"}],settings:{getLastErrorDefaults:{w:"majority",wtimeout:3000}}}')'
# 配置初始化系統(tǒng)用戶(hù)
/usr/local/mongodb/bin/mongo 127.0.0.1:${mongo_port}/admin --eval 'db.createUser({user: "dba",pwd: "dba",roles:["__system"]})'
# 添加管理超級(jí)用戶(hù)
/usr/local/mongodb/bin/mongo 127.0.0.1:${mongo_port}/admin -u dba -p dba --eval 'db.createUser({user: "'${mongo_user}'",pwd: "'${mongo_pwd}'",roles:["root"]})'
# 將寫(xiě)操作刷新到磁盤(pán)并鎖定后解鎖
/usr/local/mongodb/bin/mongo 127.0.0.1:${mongo_port}/admin -u ${mongo_user} -p ${mongo_pwd} --eval 'db.fsyncLock();db.fsyncUnlock();'
# 刪除初始化用戶(hù)
/usr/local/mongodb/bin/mongo 127.0.0.1:${mongo_port}/admin -u dba -p dba --eval 'db.dropUser("dba")'
# kill 掉之前進(jìn)程 重新啟動(dòng)節(jié)點(diǎn)
ps aux | grep mongo
kill xxxxx
sudo -u mongo numactl --interleave=all /usr/local/mongodb/bin/mongod -f ${mongo_path}/mongodb.cnf &>/dev/null
#認(rèn)證降級(jí),3.6及其以上版本不需要做認(rèn)證降級(jí)
/usr/local/mongodb/bin/mongo 127.0.0.1:${mongo_port}/admin -u dba -p dba --eval 'db.system.version.save({ "_id" : "authSchema", "currentVersion" : 3 })'
啟動(dòng)從節(jié)點(diǎn)
# 啟動(dòng)節(jié)點(diǎn)
sudo -u mongo numactl --interleave=all /usr/local/mongodb/bin/mongod -f ${mongo_path}/mongodb.cnf &>/dev/null
# 去主節(jié)點(diǎn)添加從節(jié)點(diǎn)信息
/usr/local/mongodb/bin/mongo ${master_ip}:${mongo_port}/admin -u ${mongo_user} -p ${mongo_pwd} --eval 'rs.add("${mongo_node_ip}:${mongo_port}")'
配置mongos 分片集群
路由節(jié)點(diǎn)配置
#SERVER
fork = true
port = ${mongo_port}
quiet = true
logpath = ${mongo_path}/log/mongod.log
logappend = true
maxConns = 20000
bind_ip_all = true
#serviceExecutor = adaptive
#SLOW_LOG
configdb=${mongo_config_replSet}/${mongo_config_node1}:${mongo_config_port},${mongo_config_node2}:${mongo_config_port},${mongo_config_node3}:${mongo_config_port}
#USER
keyFile=${mongo_path}/keyFile
setParameter=enableLocalhostAuthBypass=1
setParameter=taskExecutorPoolSize=8
setParameter=ShardingTaskExecutorPoolMinSize=15
#VER
#mongo_version=mongodb40
config 節(jié)點(diǎn)配置
#SERVER
fork = true
port = ${mongo_port}
quiet = true
dbpath = ${mongo_path}
logpath = ${mongo_path}/log/mongod.log
logappend = true
maxConns = 20000
directoryperdb = true
#>=3.6
bind_ip_all = true
#noIndexBuildRetry=true
#SLOW_LOG
#profile = 1
slowms = 300
#SCO
configsvr = true
#configdb = MSCHOST
#for 3.4
replSet=${mongo_replSet}
storageEngine=wiredTiger
wiredTigerCacheSizeGB=1
oplogSize = 8192
#USER
keyFile=${mongo_path}/keyFile
setParameter=enableLocalhostAuthBypass=1
#VER
#mongo_version=mongodb40
分片節(jié)點(diǎn)配置
#SERVER
fork = true
port = ${mongo_port}
quiet = true
dbpath = ${mongo_path}
logpath = ${mongo_path}/log/mongod.log
logappend = true
journal = true
directoryperdb = true
maxConns=20000
#>=3.6
bind_ip_all = true
#noIndexBuildRetry=true
#serviceExecutor=adaptive
#SLOW_LOG
#profile = 1
slowms = 300
#RS
replSet = ${mongo_replSet}
oplogSize=10240
# add for 3.4
shardsvr=true
#SCO
#configsvr = true
#configdb = MSCHOST
#setParameter=enableLocalhostAuthBypass=1
#USER
keyFile=${mongo_path}/keyFile
setParameter=enableLocalhostAuthBypass=1
setParameter=failIndexKeyTooLong=1
storageEngine=wiredTiger
wiredTigerCacheSizeGB=2
wiredTigerCollectionBlockCompressor=snappy
#VER
#mongo_version=mongodb40
啟動(dòng)步驟
1.先部署兩個(gè)分片節(jié)點(diǎn) (步驟參考上文配置副本集節(jié)點(diǎn))
2.再部署config server 節(jié)點(diǎn) (步驟參考上文配置副本集節(jié)點(diǎn))
3.最后再部署mongos 路由節(jié)點(diǎn)
# 啟動(dòng)節(jié)點(diǎn)
sudo -u mongo numactl --interleave=all /usr/local/mongodb/bin/mongod -f ${mongo_path}/mongodb.cnf &>/dev/null
# 添加2個(gè)分片節(jié)點(diǎn)
/usr/local/mongodb/bin/mongo 127.0.0.1:${mongo_port}/admin -u dba -p dba --eval 'sh.addShard("${mongo_replSet}/${mongo_shards_node1}:${mongo_shards_port},${mongo_shards_node2}:${mongo_shards_port},${mongo_shards_node3}:${mongo_shards_port}")'