安装部署 mongo
前题准备
官方下载地址
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
创建源文件
初始化mongo 数据目录
# 初始化文件目录
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
启动mongo
sudo -u mongo numactl --interleave=all /usr/local/mongodb/bin/mongod -f ${mongo_path}/mongodb.cnf &>/dev/null
配置副本集
启动master 节点
# 启动节点
sudo -u mongo numactl --interleave=all /usr/local/mongodb/bin/mongod -f ${mongo_path}/mongodb.cnf &>/dev/null
# 配置副本集参数
/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}}}')'
# 配置初始化系统用户
/usr/local/mongodb/bin/mongo 127.0.0.1:${mongo_port}/admin --eval 'db.createUser({user: "dba",pwd: "dba",roles:["__system"]})'
# 添加管理超级用户
/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"]})'
# 将写操作刷新到磁盘并锁定后解锁
/usr/local/mongodb/bin/mongo 127.0.0.1:${mongo_port}/admin -u ${mongo_user} -p ${mongo_pwd} --eval 'db.fsyncLock();db.fsyncUnlock();'
# 删除初始化用户
/usr/local/mongodb/bin/mongo 127.0.0.1:${mongo_port}/admin -u dba -p dba --eval 'db.dropUser("dba")'
# kill 掉之前进程 重新启动节点
ps aux | grep mongo
kill xxxxx
sudo -u mongo numactl --interleave=all /usr/local/mongodb/bin/mongod -f ${mongo_path}/mongodb.cnf &>/dev/null
#认证降级,3.6及其以上版本不需要做认证降级
/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 })'
启动从节点
# 启动节点
sudo -u mongo numactl --interleave=all /usr/local/mongodb/bin/mongod -f ${mongo_path}/mongodb.cnf &>/dev/null
# 去主节点添加从节点信息
/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 分片集群
路由节点配置
#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 节点配置
#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
分片节点配置
#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
启动步骤
1.先部署两个分片节点 (步骤参考上文配置副本集节点)
2.再部署config server 节点 (步骤参考上文配置副本集节点)
3.最后再部署mongos 路由节点
# 启动节点
sudo -u mongo numactl --interleave=all /usr/local/mongodb/bin/mongod -f ${mongo_path}/mongodb.cnf &>/dev/null
# 添加2个分片节点
/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}")'