1 .安装Mongodb

编辑Mongodb安装源

vi /etc/yum.repos.d/mongodb-org-4.4.repo

编辑内容如下:

[mongodb-org-4.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc

安装

yum install -y mongodb-org

2 .在启动前修改一些配置

2.1.(推荐)修改ulimit

vi /etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
vi /etc/profile
ulimit -SHn 65535

2.2.(推荐)关闭transparent_hugepage

vi /etc/rc.local

if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
  echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
  echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi

3 . 修改mongodb配置文件

vi /etc/mongod.conf

systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

storage:
  dbPath: /var/lib/mongo
  journal:
    enabled: true

processManagement:
  fork: true
  pidFilePath: /var/run/mongodb/mongod.pid

net:
  port: 27017
  bindIp: 0.0.0.0

security:
  authorization: enabled
  javascriptEnabled: false

4 . 管理

启动 mongod -f /etc/mongod.conf

停止 mongod -f /etc/mongod.conf --shutdown

#新建管理员用户

mongo --port 21111
> use admin
> db.createUser({user:"admin",pwd:"123456",roles:[{role:"root",db:"admin"}]})
> db.auth("userAdmin","123456")
#新建普通用户

> use test
> db.createUser({user:"test",pwd:"123456",roles:[{role:"readWrite",db:"test"}]})

#删除安装的包

yum erase $(rpm -qa | grep mongodb-org)

删除数据及日志

rm -r /var/log/mongodb

rm -r /var/lib/mongo

PS: 默认是使用 mongodb用户启动, 所以注意使用目录的权限