注意:本文全程使用 root 账号,如果不是 root 账号,需要在命令前加 sudo

第一部分:配置 Yum 源

vim /etc/yum.repos.d/mongodb-org-6.0.repo

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

第二部分:关闭防火墙以及禁用 SELinux

# 关闭防火墙
systemctl stop firewalld # 停止防火墙
systemctl disable firewalld # 禁用防火墙

# 禁用SELinux
sestatus # 查看 SELinux 状态

# 输出示例(已禁用)
    SELinux status:                 enabled
    SELinuxfs mount:                /sys/fs/selinux
    SELinux root directory:         /etc/selinux
    Loaded policy name:             targeted
    Current mode:                   enforcing
    Mode from config file:          enforcing
    Policy MLS status:              enabled
    Policy deny_unknown status:     allowed
    Max kernel policy version:      31

setenforce 0 # 临时将 SELinux 模式从目标更改为允许
vim /etc/selinux/config # 编辑 SELinux 配置文件

# 将 SELINUX=enforcing 改为 SELINUX=disabled (如下文所示)
    # This file controls the state of SELinux on the system.
    # SELINUX= can take one of these three values:
    #       enforcing - SELinux security policy is enforced.
    #       permissive - SELinux prints warnings instead of enforcing.
    #       disabled - No SELinux policy is loaded.
    SELINUX=disabled
    # SELINUXTYPE= can take one of these two values:
    #       targeted - Targeted processes are protected,
    #       mls - Multi Level Security protection.
    SELINUXTYPE=targeted

reboot # 保存并重启系统

# 使用 sestatus 命令验证更改
sestatus
# 示例输出
    SELinux status:                 disabled

第三部分:安装并启动 MongoDB

# 安装 MongoDB
yum install mongodb-org -y

# 卸载 MongoDB
yum remove mongodb*

# 启动 MongoDB
systemctl start mongod

# 设置开机自启
systemctl enable mongod

# 查看运行状态( active (running) 表示正常运行)
systemctl status mongod

第四部分:验证是否安装成功

mongo --quiet "mongodb://127.0.0.1:27017" # 连接到数据库

# 出现以下子项表示安装成功
    $ mongo --quiet "mongodb://127.0.0.1:27017"
    Welcome to the MongoDB shell.
    >