一、安装前准备工作
1、关闭SELINUX、防火墙
[root@localhost ~]# setenforce 0
[root@localhost ~]# sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
[root@localhost ~]# systemctl stop firewalld && systemctl disable firewalld
2、设置时间同步
[root@localhost ~]# yum -y install ntp
[root@localhost ~]# \cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
[root@localhost ~]# ntpdate cn.pool.ntp.org
[root@localhost ~]# systemctl start ntpdate && systemctl enable ntpdate
[root@localhost ~]# date
3、安装依赖包
[root@localhost ~]# yum -y install libcurl openssl numactl
二、 使用二进制包的安装方式
1、解压安装包并创建相关目录
[root@localhost ~]# tar -zxvf mongodb-linux-x86_64-rhel70-4.2.18.tgz -C /usr/local
[root@localhost ~]# mv mongodb-linux-x86_64-rhel70-4.2.18/ mongodb
[root@localhost ~]# mkdir -p /usr/local/mongodb/{data,config,logs}
2、编辑配置文件
[root@localhost ~]# cd /usr/local/mongodb/config # 此种方式安装没有配置文件,手动编写一个
[root@localhost ~]# vim mongodb.conf
port=27017
bind_ip=0.0.0.0
fork=true # 以守护进程的方式运行
journal=true # 每次写入记录一条操作日志
maxConns=500 # 最大的同时连接数
logappend=true # 以追加的方式写日志
dbpath=/usr/local/mongodb/data
logpath=/usr/local/mongodb/logs/mongo.log
pidfilepath=/usr/local/mongodb/mongo.pid
3、启动服务、验证安装
[root@localhost ~]# /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/config/mongodb.conf
# 比较建议使用这样的启动方式: numactl --interleave=all /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/config/mongodb.conf
[root@test220 ~]# netstat -lntp | grep 27017
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 7973/mongod
4、配置环境变量
[root@localhost ~]# cp /etc/profile /etc/profile.bak
[root@localhost ~]# vim /etc/profile # 文本末尾追加下面两行
export MONGODB_HOME=/usr/local/mongodb
export PATH=.:$PATH:$MONGODB_HOME/bin
[root@localhost ~]# source /etc/profile
[root@localhost ~]# mongod --version
db version v4.2.18
[root@localhost ~]# mongo
MongoDB shell version v4.2.18
connecting to: mongodb://10.6.2.220:27017/test?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("8a14d796-a65c-424d-9265-716446d5d4c0") }
MongoDB server version: 4.2.18
Server has startup warnings:
... ...
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
> exit
5、配置成 Systemd 的启动方式启
[root@localhost ~]# vim /usr/lib/systemd/system/mongodb.service
[Unit]
Description=mongodb
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/config/mongodb.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/usr/local/mongodb/bin/mongod --shutdown --config /usr/local/mongodb/config/mongodb.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@localhost ~]# netstat -lntp | grep 27017
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 7973/mongod
[root@localhost ~]# kill -2 7973
[root@localhost ~]# systemctl start mongodb
[root@localhost ~]# systemctl status mongodb
● mongodb.service - mongodb
Loaded: loaded (/usr/lib/systemd/system/mongodb.service; disabled; vendor preset: disabled)
Active: active (running) since Mon 2022-01-10 16:59:20 CST; 8s ago
Process: 15873 ExecStop=/usr/local/mongodb/bin/mongod --shutdown --config /usr/local/mongodb/config/mongodb.conf (code=exited, status=0/SUCCESS)
Process: 15878 ExecStart=/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/config/mongodb.conf (code=exited, status=0/SUCCESS)
Main PID: 15880 (mongod)
CGroup: /system.slice/mongodb.service
└─15880 /usr/local/mongodb/bin/mongod --config /usr/local/mongodb/config/mongodb.conf
三、YUM 安装的方式
1、编辑 repo 仓库文件
[root@localhost ~]# vim /etc/yum.repos.d/mongo.repo
[mongodb-org-4.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.2/x86_64/
gpgcheck=0
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc
[root@localhost ~]# yum repolist | grep mongodb
mongodb-org-4.2 MongoDB Repository 124
2、进行安装
[root@localhost ~]# yum -y install mongodb-org
# 如果想安装特定版本的MongoDB,要分别指定每个组件软件包的版本,如以下
# yum install -y mongodb-org-4.2.18 mongodb-org-server-4.2.18 mongodb-org-shell-4.2.18 mongodb-org-mongos-4.2.18 mongodb-org-tools-4.2.18
[root@localhost ~]# whereis mongod
mongod: /usr/bin/mongod /etc/mongod.conf /usr/share/man/man1/mongod.1.gz
[root@localhost ~]# sed -i "s/127.0.0.1/0.0.0.0/" /etc/mongod.conf
[root@localhost ~]# systemctl start mongod
[root@localhost ~]# systemctl status mongod
● mongod.service - MongoDB Database Server
Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2022-01-05 00:59:05 CST; 37min ago
Docs: https://docs.mongodb.org/manual
Process: 2826 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=0/SUCCESS)
Process: 2823 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
Process: 2820 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
Process: 2819 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
Main PID: 2829 (mongod)
CGroup: /system.slice/mongod.service
└─2829 /usr/bin/mongod -f /etc/mongod.conf