1、rpm包安装

1)寻找yum源

首先找到国内一些开源镜像站(此处选择清华大学的开源镜像站)
在这里插入图片描述
从中找到MongoDB
在这里插入图片描述
点击进去之后选择yum
在这里插入图片描述
选择下方所需版本(此处选中4.4)
在这里插入图片描述
复制下方图片红框中的url链接(为后面编写yum源做准备)【https://mirrors.tuna.tsinghua.edu.cn/mongodb/yum/el7-4.4/】
请添加图片描述

2)配置MongoDB的yum源文件

[root@localhost ~]# vim /etc/yum.repos.d/mongodb.repo
//如果配置过红帽Linux的yum源,应该都明白此步骤的目的
[mongodb]
name=mongodb	//yum源的名字(建议与上方标题一样)
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mongodb/yum/el7-4.4/
//此处url选择上面刚刚复制的url
gpgcheck=0		//表示不检查签名信息

3)查看MongoDB的安装包(确认yum源是否可用)

[root@localhost ~]# yum list | grep mongo
mongodb-database-tools.x86_64             100.5.2-1                    @mongodb 
mongodb-org.x86_64                        4.4.14-1.el7                 @mongodb 
mongodb-org-database-tools-extra.x86_64   4.4.14-1.el7                 @mongodb 
mongodb-org-mongos.x86_64                 4.4.14-1.el7                 @mongodb 
mongodb-org-server.x86_64                 4.4.14-1.el7                 @mongodb 
mongodb-org-shell.x86_64                  4.4.14-1.el7                 @mongodb 
mongodb-org-tools.x86_64                  4.4.14-1.el7                 @mongodb 
google-noto-sans-mongolian-fonts.noarch   20141117-5.el7               base     
mongocli.x86_64                           1.25.0-1                     mongodb  
mongodb-atlas-cli.x86_64                  1.1.0-1                      mongodb  
mongodb-mongosh.x86_64                    1.4.2-1.el8                  mongodb  

4)安装

[root@localhost ~]# yum install mongo-org.x86_64 -y

5)开启MongoDB服务 & 开启MongoDB开机自启

[root@localhost ~]# systemctl start mongod
[root@localhost ~]# systemctl enable mongod

6)检查服务是否开启(多种方法)

方法一:
[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-06-01 20:20:49 CST; 21min ago
     Docs: https://docs.mongodb.org/manual
  Process: 945 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=0/SUCCESS)
  Process: 942 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 939 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 936 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
 Main PID: 983 (mongod)
   CGroup: /system.slice/mongod.service
           └─983 /usr/bin/mongod -f /etc/mongod.conf

Jun 01 20:20:47 localhost.localdomain systemd[1]: Starting MongoDB Database Server...
Jun 01 20:20:47 localhost.localdomain mongod[945]: about to fork child process, waiting....
Jun 01 20:20:47 localhost.localdomain mongod[945]: forked process: 983
Jun 01 20:20:49 localhost.localdomain mongod[945]: child process started successfully, ...g
Jun 01 20:20:49 localhost.localdomain systemd[1]: Started MongoDB Database Server.
Hint: Some lines were ellipsized, use -l to show in full.

方法二:
[root@localhost ~]# ps -ef | grep mongod
mongod      983      1  0 20:20 ?        00:00:06 /usr/bin/mongod -f /etc/mongod.conf
root       2333   1296  0 20:40 pts/0    00:00:00 grep --color=auto mongod

方法三:
[root@localhost ~]# netstat -lnupt | grep 27017
tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      983/mongod 

方法四:
[root@localhost ~]# lsof -i tcp:27017
COMMAND PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
mongod  983 mongod   11u  IPv4  18211      0t0  TCP localhost:27017 (LISTEN)

7)使用MongoDB

[root@localhost ~]# mongo
MongoDB shell version v4.4.14
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("ae06ee2e-558f-4885-bcf9-73263de9ac0c") }
MongoDB server version: 4.4.14
---
The server generated these startup warnings when booting: 
        2022-06-01T20:20:49.209+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
        2022-06-01T20:20:49.210+08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
        2022-06-01T20:20:49.210+08:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'
---
---
        Enable MongoDB's free cloud-based monitoring service, which will then receive and display
        metrics about your deployment (disk utilization, CPU, operation statistics, etc).

        The monitoring data will be available on a MongoDB website with a unique URL accessible to you
        and anyone you share the URL with. MongoDB may use this information to make product
        improvements and to suggest MongoDB products and deployment options to you.

        To enable free monitoring, run the following command: db.enableFreeMonitoring()
        To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
> 

2、源码安装

1)获取对应的MongoDB源码安装包(此处选择4.4.14版本)

[root@localhost ~]#wget -c https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.14.tgz

[root@localhost ~]# ll		//查看下载结果
total 70708
-rw-------. 1 root root     1392 Mar 26 17:20 anaconda-ks.cfg
-rw-r--r--  1 root root 72399766 May  4 23:03 mongodb-linux-x86_64-rhel70-4.4.14.tgz

2)解压源码安装包

[root@localhost ~]# tar xf mongodb-linux-x86_64-rhel70-4.4.14.tgz -C /usr/local/
//解压至“/usr/local”目录下

[root@localhost ~]# ll -d /usr/local/mongodb-linux-x86_64-rhel70-4.4.14/
drwxr-xr-x 5 root root 123 May 28 23:40 /usr/local/mongodb-linux-x86_64-rhel70-4.4.14/
//可以看到已经解压成功

3)创建软链接(起名mongodb)【方便】:

[root@localhost ~]# ln -sv mongodb-linux-x86_64-rhel70-3.4.7/ /usr/local/mongodb

4)创建环境变量:

[root@localhost ~]# echo "export PATH=$PATH:/usr/local/mongodb/bin" > /etc/profile.d/mongo.sh
[root@localhost ~]# source /etc/profile.d/mongo.sh
//注:一定要执行此步骤,否则最后无法通过“mongo”命令使用MongoDB

5)创建对应的系统组 & 系统用户

[root@localhost ~]# groupadd -g 996 -r mongod
[root@localhost ~]# useradd -u 997 -g 996 -r -c mongod -d /var/lib/mongo -s /bin/false mongod

//根据系统用户要求,创建对应目录:
[root@localhost ~]# mkdir /var/lib/mongo
[root@localhost ~]# chown -R mongod:mongod /var/lib/mongo/

//查看创建结果:
[root@localhost ~]# grep mongod /etc/passwd
mongod:x:997:996:mongod:/var/lib/mongo:/bin/false
[root@localhost ~]# id mongod
uid=997(mongod) gid=996(mongod) groups=996(mongod)

6)创建主配置文件mongod.conf(网上找模板 & 搬运rpm包安装的主配置文件)

[root@localhost ~]# vim /usr/local/mongodb/bin/mongod.conf
systemLog:   
         destination: file  #以文件形式输出日志文件,输入file之前,需要空格
         path: /usr/local/mongodb/log/mongodb.log #表示存储日志文件    
         logAppend: true    #日志是追加模式,false是覆盖模式 
storage: 
         dbPath: /usr/local/mongodb/data    #表示存储mongodb数据库文件目录
processManagement: 
         fork: true    #作为后台服务运行
#net:
#         bindIp: 127.0.0.1
#         port: 27017
#security:
#         authorization:enabled

7)创建主配置文件中指定的所需目录(数据目录 & 日志目录) & 指定的日志文件(不会自动创建)【不创建启动会报错】

[root@localhost ~]# mkdir -p /usr/local/mongodb/{data,log}

[root@localhost ~]# touch /usr/local/mongodb/log/mongodb.log

8)编写systemctl启动 / 关闭 / 重启脚本(上网找模板 & 搬运rpm包安装的脚本)

[root@localhost ~]# vim /usr/lib/systemd/system/mongod.service
[Unit]
Description=mongodb service daemon
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/bin/mongod.conf
#调用启动服务的命令,并且指定服务主配置文件的路径
ExecStop=/usr/local/mongodb/bin/mongod --shutdown -f /usr/local/mongodb/bin/mongod.conf
#调用关闭服务的命令,并且指定服务主配置文件的路径
PrivateTmp=true

[Install]
WantedBy=multi-user.target
开启MongoDB服务:
[root@localhost ~]# systemctl start mongod

9)检查服务是否开启(多种方法)

方法一:
[root@localhost ~]# systemctl status mongod
● mongod.service - mongodb service daemon
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2022-06-01 21:11:46 CST; 5s ago
  Process: 1352 ExecStart=/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/bin/mongod.conf (code=exited, status=0/SUCCESS)
 Main PID: 1354 (mongod)
   CGroup: /system.slice/mongod.service
           └─1354 /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/bin/mongod.conf

Jun 01 21:11:44 localhost.localdomain systemd[1]: Starting mongodb service daemon...
Jun 01 21:11:44 localhost.localdomain mongod[1352]: about to fork child process, waitin....
Jun 01 21:11:44 localhost.localdomain mongod[1352]: forked process: 1354
Jun 01 21:11:46 localhost.localdomain mongod[1352]: child process started successfully,...g
Jun 01 21:11:46 localhost.localdomain systemd[1]: Started mongodb service daemon.
Hint: Some lines were ellipsized, use -l to show in full.

方法二:
[root@localhost ~]# ps -ef | grep mongod
root       1354      1  1 21:11 ?        00:00:02 /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/bin/mongod.conf
root       1547   1187  0 21:14 pts/0    00:00:00 grep --color=auto mongod

方法三:
[root@localhost ~]# ps -ef | grep mongod
root       1354      1  1 21:11 ?        00:00:02 /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/bin/mongod.conf
root       1547   1187  0 21:14 pts/0    00:00:00 grep --color=auto mongod
[root@localhost ~]# netstat -lnupt | grep 27017
tcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN      1354/mongod

方法四:
[root@localhost ~]# lsof -i tcp:27017
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
mongod  1354 root   12u  IPv4  21147      0t0  TCP *:27017 (LISTEN)

10)使用MongoDB

[root@localhost ~]# mongo
MongoDB shell version v4.4.14
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("30c2e185-ca3c-45f1-98d2-d4560308df52") }
MongoDB server version: 4.4.14
---
The server generated these startup warnings when booting: 
        2022-06-01T21:11:46.164+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
        2022-06-01T21:11:46.164+08:00: You are running this process as the root user, which is not recommended
        2022-06-01T21:11:46.164+08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
        2022-06-01T21:11:46.164+08:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'
        2022-06-01T21:11:46.164+08:00: Soft rlimits too low
        2022-06-01T21:11:46.164+08:00:         currentValue: 1024
        2022-06-01T21:11:46.164+08:00:         recommendedMinimum: 64000
---
---
        Enable MongoDB's free cloud-based monitoring service, which will then receive and display
        metrics about your deployment (disk utilization, CPU, operation statistics, etc).

        The monitoring data will be available on a MongoDB website with a unique URL accessible to you
        and anyone you share the URL with. MongoDB may use this information to make product
        improvements and to suggest MongoDB products and deployment options to you.

        To enable free monitoring, run the following command: db.enableFreeMonitoring()
        To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
> 
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐