Linux下MongoDB 4.X安装配置(图文详细)
一、资源准备
- 资源下载地址: https://www.mongodb.com/try/download

二、安装配置
mkdir -p /opt/module /opt/soft

#解压命令
tar -vxf mongodb-linux-x86_64-rhel70-4.2.14.tgz -C /opt/module/
#切换目录
cd /opt/module/mongodb-linux-x86_64-rhel70-4.2.14/ && ll

#创建文件夹
mkdir -p repo/db repo/logs repo/conf
#创建日志文件
touch repo/logs/mongod.log
cd /opt/module/mongodb-linux-x86_64-rhel70-4.2.14/repo/conf
vim mongod.conf
dbpath = /opt/module/mongodb-linux-x86_64-rhel70-4.2.14/repo/db
logpath = /opt/module/mongodb-linux-x86_64-rhel70-4.2.14/repo/logs/mongod.log
bind_ip = 0.0.0.0
port = 27017
logappend=true
journal=true
fork = true
auth= false
replSet=rs0
maxConns=8800

cd /etc/init.d/
vim mongod
export MONGO_HOME=/opt/module/mongodb-linux-x86_64-rhel70-4.2.14
export MONGO_CONF=/opt/module/mongodb-linux-x86_64-rhel70-4.2.14/repo/conf/mongod.conf
SYSTEM_MAXFD=655360
case $1 in
start)
ulimit -HSn $SYSTEM_MAXFD
$MONGO_HOME/bin/mongod --config $MONGO_CONF
;;
stop)
$MONGO_HOME/bin/mongod --shutdown --config $MONGO_CONF
;;
status)
ps -ef | grep mongod
;;
restart)
$MONGO_HOME/bin/mongod --shutdown --config $MONGO_CONF
$MONGO_HOME/bin/mongod --config $MONGO_CONF
;;
*)
echo "require start|stop|status|restart"
;;
esac
chmod +x mongod
chkconfig --add mongod
chkconfig mongod on
chkconfig --list

# 启停命令
systemctl start|stop|restart|status mongod
#查看状态
systemctl status mongod
ps -ef|grep mongod
#查看某个进程当前打开的文件数
lsof -n|awk '{print $2}'|sort|uniq -c|sort -nr|more |grep PID
#查看进程可以打开的最大文件数,数值过小使用过程中极大概率会出现 Too many open files 异常错误。
cat /proc/PID/limits

vim /etc/profile
export MONGODB_HOME=/opt/module/mongodb-linux-x86_64-rhel70-4.2.14
export PATH=$PATH:$MONGODB_HOME/bin
source /etc/profile

mongo
rs.initiate({ _id : "rs0",members: [{ _id: 0, host: "10.15.22.21:27017" }]})
use roo_db
db.createUser({user: "roo",pwd: "roo",roles: [ { role: "readWrite", db: "roo_db" } ]})
db.auth('roo','roo')

cd /opt/module/mongodb-linux-x86_64-rhel70-4.2.14/repo/conf/
vim mongod.conf
auth = true
systemctl restart mongod
mongo --host 10.15.22.21 --port 27017 -u roo -p roo --authenticationDatabase roo_db
mongodb://用户:密码@IP:PORT/数据库名称?authSource=数据库名称&authMechanism=SCRAM-SHA-1
