CentOS 7.9下MongoDB 5.0安装和配置手册


1. 安装前的准备

1.1 安装环境

1.1.1 操作系统内核版本

[root@localhost ~]# uname -a
Linux localhost.localdomain 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

1.1.2 操作系统发行版本

[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)

1.2 安装文件

1.2.1 安装文件下载

1.2.2 安装文件列表

  • mongodb-org-server-5.0.3-1.el7.x86_64.rpm
  • mongodb-org-mongos-5.0.3-1.el7.x86_64.rpm
  • mongodb-org-shell-5.0.3-1.el7.x86_64.rpm
  • mongodb-database-tools-rhel70-x86_64-100.5.0.rpm

2. 安装和配置

2.1 软件安装

2.1.1 安装前的检测

  • 检测软件是否已安装
[root@localhost ~]# rpm -qa | grep mongodb
mongodb-database-tools-100.5.0-1.x86_64
mongodb-org-shell-5.0.3-1.el7.x86_64
mongodb-org-server-5.0.3-1.el7.x86_64
mongodb-org-mongos-5.0.3-1.el7.x86_64
  • 查看运行进程
[root@localhost ~]# ps -ef | grep mongodb
root 47876 46534 0 07:57 pts/1 00:00:00 grep mongodb
  • 卸载软件文件
[root@localhost ~]# rpm -e mongodb-database-tools-100.5.0-1.x86_64 --nodeps
  • 官网推荐彻底卸载
[root@localhost ~]# sudo yum erase $(rpm -qa | grep mongodb-org)
Loaded plugins: product-id, refresh-packagekit, search-disabled-repos, security, subscriptionmanager
This system is not registered with an entitlement server. You can use subscription-manager to
register.
Setting up Remove Process
Resolving Dependencies
--> Running transaction check
---> Package mongodb-org-mongos.x86_64 0:4.4.8-1.el6 will be erased
---> Package mongodb-org-server.x86_64 0:4.4.8-1.el6 will be erased
---> Package mongodb-org-shell.x86_64 0:4.4.8-1.el6 will be erased
--> Finished Dependency Resolution
Dependencies Resolved
====================================================================
=====================================
Package Arch Version Repository
Size
====================================================================
=====================================
Removing:
mongodb-org-mongos x86_64 4.4.8-1.el6 @/mongodb-org-mongos4.4.8-1.el6.x86_64 62 M
mongodb-org-server x86_64 4.4.8-1.el6 @/mongodb-org-server-4.4.8-
1.el6.x86_64 79 M
mongodb-org-shell x86_64 4.4.8-1.el6 @/mongodb-org-shell-4.4.8-
1.el6.x86_64 51 M
Transaction Summary
Page 5 of 21
====================================================================
=====================================
Remove 3 Package(s)
Installed size: 192 M
Is this ok [y/N]: y
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
 Erasing : mongodb-org-server-4.4.8-1.el6.x86_64
1/3
warning: /var/log/mongodb/mongod.log saved as /var/log/mongodb/mongod.log.rpmsave
warning: /etc/mongod.conf saved as /etc/mongod.conf.rpmsave
 Erasing : mongodb-org-shell-4.4.8-1.el6.x86_64
2/3
 Erasing : mongodb-org-mongos-4.4.8-1.el6.x86_64
3/3
 Verifying : mongodb-org-mongos-4.4.8-1.el6.x86_64
1/3
 Verifying : mongodb-org-shell-4.4.8-1.el6.x86_64
2/3
 Verifying : mongodb-org-server-4.4.8-1.el6.x86_64
3/3
Removed:
 mongodb-org-mongos.x86_64 0:4.4.8-1.el6 mongodb-org-server.x86_64
0:4.4.8-1.el6
 mongodb-org-shell.x86_64 0:4.4.8-1.el6
Complete!

2.1.2 安装MongoDB

  • 切换至软件目录;
[root@localhost ~]# cd /home/Software/MongoDB/
[root@localhost MongoDB]# ls
mongodb-database-tools-rhel70-x86_64-100.5.0.rpm  mongodb-org-server-5.0.3-1.el7.x86_64.rpm
mongodb-org-mongos-5.0.3-1.el7.x86_64.rpm         mongodb-org-shell-5.0.3-1.el7.x86_64.rpm
  • 安装“mongodb-org-server-5.0.3-1.el7.x86_64.rpm”;
[root@localhost MongoDB]# sudo yum install -y mongodb-org-server-5.0.3-1.el7.x86_64.rpm 
Loaded plugins: fastestmirror, langpacks
Examining mongodb-org-server-5.0.3-1.el7.x86_64.rpm: mongodb-org-server-5.0.3-1.el7.x86_64
Marking mongodb-org-server-5.0.3-1.el7.x86_64.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package mongodb-org-server.x86_64 0:5.0.3-1.el7 will be installed
--> Finished Dependency Resolution
base/7/x86_64                                                                                                                              | 3.6 kB  00:00:00     
docker-ce-stable/7/x86_64                                                                                                                  | 3.5 kB  00:00:00     
epel/x86_64/metalink                                                                                                                       | 6.9 kB  00:00:00     
extras/7/x86_64                                                                                                                            | 2.9 kB  00:00:00     
updates/7/x86_64                                                                                                                           | 2.9 kB  00:00:00     

Dependencies Resolved

==================================================================================================================================================================
 Package                               Arch                      Version                          Repository                                                 Size
==================================================================================================================================================================
Installing:
 mongodb-org-server                    x86_64                    5.0.3-1.el7                      /mongodb-org-server-5.0.3-1.el7.x86_64                    103 M

Transaction Summary
==================================================================================================================================================================
Install  1 Package

Total size: 103 M
Installed size: 103 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : mongodb-org-server-5.0.3-1.el7.x86_64                                                                                                          1/1 
Created symlink from /etc/systemd/system/multi-user.target.wants/mongod.service to /usr/lib/systemd/system/mongod.service.
  Verifying  : mongodb-org-server-5.0.3-1.el7.x86_64                                                                                                          1/1 

Installed:
  mongodb-org-server.x86_64 0:5.0.3-1.el7                                                                                                                         

Complete!
  • 安装“mongodb-org-mongos-5.0.3-1.el7.x86_64.rpm”;
[root@localhost MongoDB]# sudo yum install -y mongodb-org-mongos-5.0.3-1.el7.x86_64.rpm
Loaded plugins: fastestmirror, langpacks
Examining mongodb-org-mongos-5.0.3-1.el7.x86_64.rpm: mongodb-org-mongos-5.0.3-1.el7.x86_64
Marking mongodb-org-mongos-5.0.3-1.el7.x86_64.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package mongodb-org-mongos.x86_64 0:5.0.3-1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==================================================================================================================================================================
 Package                               Arch                      Version                          Repository                                                 Size
==================================================================================================================================================================
Installing:
 mongodb-org-mongos                    x86_64                    5.0.3-1.el7                      /mongodb-org-mongos-5.0.3-1.el7.x86_64                     71 M

Transaction Summary
==================================================================================================================================================================
Install  1 Package

Total size: 71 M
Installed size: 71 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : mongodb-org-mongos-5.0.3-1.el7.x86_64                                                                                                          1/1 
  Verifying  : mongodb-org-mongos-5.0.3-1.el7.x86_64                                                                                                          1/1 

Installed:
  mongodb-org-mongos.x86_64 0:5.0.3-1.el7                                                                                                                         

Complete!
  • 安装“mongodb-org-shell-5.0.3-1.el7.x86_64.rpm”;
[root@localhost MongoDB]# sudo yum install -y mongodb-org-shell-5.0.3-1.el7.x86_64.rpm
Loaded plugins: fastestmirror, langpacks
Examining mongodb-org-shell-5.0.3-1.el7.x86_64.rpm: mongodb-org-shell-5.0.3-1.el7.x86_64
Marking mongodb-org-shell-5.0.3-1.el7.x86_64.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package mongodb-org-shell.x86_64 0:5.0.3-1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==================================================================================================================================================================
 Package                               Arch                       Version                         Repository                                                 Size
==================================================================================================================================================================
Installing:
 mongodb-org-shell                     x86_64                     5.0.3-1.el7                     /mongodb-org-shell-5.0.3-1.el7.x86_64                      55 M

Transaction Summary
==================================================================================================================================================================
Install  1 Package

Total size: 55 M
Installed size: 55 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : mongodb-org-shell-5.0.3-1.el7.x86_64                                                                                                           1/1 
  Verifying  : mongodb-org-shell-5.0.3-1.el7.x86_64                                                                                                           1/1 

Installed:
  mongodb-org-shell.x86_64 0:5.0.3-1.el7                                                                                                                          

Complete!		
  • 安装“mongodb-database-tools-rhel70-x86_64-100.5.0.rpm”。
[root@localhost MongoDB]# sudo yum install -y mongodb-database-tools-rhel70-x86_64-100.5.0.rpm
Loaded plugins: fastestmirror, langpacks
Examining mongodb-database-tools-rhel70-x86_64-100.5.0.rpm: mongodb-database-tools-100.5.0-1.x86_64
Marking mongodb-database-tools-rhel70-x86_64-100.5.0.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package mongodb-database-tools.x86_64 0:100.5.0-1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==================================================================================================================================================================
 Package                                 Arch                    Version                     Repository                                                      Size
==================================================================================================================================================================
Installing:
 mongodb-database-tools                  x86_64                  100.5.0-1                   /mongodb-database-tools-rhel70-x86_64-100.5.0                  125 M

Transaction Summary
==================================================================================================================================================================
Install  1 Package

Total size: 125 M
Installed size: 125 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : mongodb-database-tools-100.5.0-1.x86_64                                                                                                        1/1 
  Verifying  : mongodb-database-tools-100.5.0-1.x86_64                                                                                                        1/1 

Installed:
  mongodb-database-tools.x86_64 0:100.5.0-1                                                                                                                       

Complete!
  • 启动数据库服务
[root@localhost ~]# sudo service mongod start
Redirecting to /bin/systemctl start mongod.service

2.2 软件配置

2.2.1 访问数据库(默认无密码)

[root@localhost ~]# mongo
MongoDB shell version v5.0.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("ce409fe0-299c-4962-8052-0564b5aa3e64") }
MongoDB server version: 5.0.3
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
We recommend you begin using "mongosh".
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
	https://community.mongodb.com
---
The server generated these startup warnings when booting: 
        2021-09-26T15:51:52.979+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
        2021-09-26T15:51:52.979+08:00: You are running on a NUMA machine. We suggest launching mongod like this to avoid performance problems: numactl --interleave=all mongod [other options]
        2021-09-26T15:51:52.980+08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
        2021-09-26T15:51:52.980+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.2.2 设置登录账号

[root@localhost ~]# mongo
> use admin
switched to db admin
> db.createUser({user:"hhAdmin", pwd:"Db0AdMiN1!", roles:[{role:"dbAdminAnyDatabase", db:"admin"}]})
> exit

2.2.3 添加远程连接权限

  • 修改 mongod.conf 配置文件 第一步:将 bindIp 修改为 0.0.0.0;第二步:在 bindIp 下方新增 bindIpAll: true。
[root@localhost ~]# vim /etc/mongod.conf

# mongod.conf

# network interfaces
net:
 port: 27017
 bindIp: 0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use
the net.bindIpAll setting.
 bindIpAll: true
  • 重启数据库服务
[root@localhost ~]# sudo service mongod restart
Redirecting to /bin/systemctl restart mongod.service

2.2.4 修改账号密码

2.2.5 修改默认端口

  • 修改 mongod.conf 配置文件 第一步:将 port 修改为其他端口号。
[root@localhost ~]# vim /etc/mongod.conf
# mongod.conf
# network interfaces
net:
Page 12 of 21
 port: 27017
 bindIp: 127.0.0.1 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively,
use the net.bindIpAll setting.
bindIpAll: true
  • 重启数据库服务
[root@localhost ~]# sudo service mongod restart
Redirecting to /bin/systemctl restart mongod.service

3. 启动和验证

3.1 软件启动

3.1.1 启动数据库服务

[root@localhost MongoDB]# sudo service mongod start
Redirecting to /bin/systemctl start mongod.service

3.1.2 重启数据库服务

[root@localhost MongoDB]# sudo service mongod restart
Redirecting to /bin/systemctl restart mongod.service

3.2 软件验证

3.2.1 查看软件版本

[root@localhost ~]# mongod -version | mongos -version | mongo -version
db version v5.0.3
Build Info: {
    "version": "5.0.3",
    "gitVersion": "657fea5a61a74d7a79df7aff8e4bcf0bc742b748",
    "openSSLVersion": "OpenSSL 1.0.1e-fips 11 Feb 2013",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "rhel70",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

(特别说明:mongod -version、mongos-version、mongo -version 三个命令都可以查看当前软件版本)

3.2.2 查看运行状态

[root@localhost MongoDB]# sudo service mongod status
Redirecting to /bin/systemctl status mongod.service
● mongod.service - MongoDB Database Server
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
   Active: failed (Result: timeout) since Sat 2021-09-25 08:52:02 PDT; 1min 35s ago
     Docs: https://docs.mongodb.org/manual
  Process: 46243 ExecStart=/usr/bin/mongod $OPTIONS (code=killed, signal=TERM)
  Process: 46240 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 46235 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 46234 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
 Main PID: 45401 (code=exited, status=0/SUCCESS)

Sep 25 08:50:24 192.168.0.108 systemd[1]: Starting MongoDB Database Server...
Sep 25 08:50:25 192.168.0.108 mongod[46243]: about to fork child process, waiting until server is ready for connections.
Sep 25 08:50:25 192.168.0.108 mongod[46243]: forked process: 46249
Sep 25 08:51:55 192.168.0.108 systemd[1]: mongod.service start operation timed out. Terminating.
Sep 25 08:52:02 192.168.0.108 systemd[1]: Failed to start MongoDB Database Server.
Sep 25 08:52:02 192.168.0.108 systemd[1]: Unit mongod.service entered failed state.
Sep 25 08:52:02 192.168.0.108 systemd[1]: mongod.service failed.

3.2.3 登录数据库(无密码)

[root@SHIEP MongoDB]# mongo
MongoDB shell version v5.0.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("7b3b9d7b-8acf-4f5d-8460-c1717ebc26e0") }
MongoDB server version: 5.0.3
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
We recommend you begin using "mongosh".
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
---
The server generated these startup warnings when booting: 
        2021-09-26T15:58:58.297+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
        2021-09-26T15:58:58.298+08:00: You are running on a NUMA machine. We suggest launching mongod like this to avoid performance problems: numactl --interleave=all mongod [other options]
        2021-09-26T15:58:58.299+08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
        2021-09-26T15:58:58.299+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()
---
> 

3.2.4 登录数据库(有密码)

3.2.5 远程连接数据库

通过Navicat远程连接数据库。

  • 点击Connection,选择MongoDB,点击打开New Connection;
  • 在 Connection Name 输入连接名称;
  • 在 Host输入 IP 地址
  • 在Port输入端口;
  • Authentication选择Password;
  • Authentication Database输入数据库名;
  • User Name输入用户名;
  • Password输入密码;
  • 点击Test Connection,连接成功,点击OK关闭。

4. 停止和卸载

4.1 软件停止

4.1.1 停止数据库服务

[root@localhost MongoDB]# sudo service mongod stop
Redirecting to /bin/systemctl stop mongod.service

4.2 软件卸载

  • 检测库文件是否存在
[root@localhost ~]# rpm -qa | grep mongodb
mongodb-database-tools-100.5.0-1.x86_64
mongodb-org-shell-5.0.3-1.el7.x86_64
mongodb-org-server-5.0.3-1.el7.x86_64
mongodb-org-mongos-5.0.3-1.el7.x86_64
  • 依次卸载软件文件
[root@localhost ~]# rpm -e mongodb-org-server-4.4.8-1.el6.x86_64 --nodeps

参数选项:

rpm -e jdk 普通删除模式

rpm -e --nodeps 强力删除模式,如果使用上面命令删除时,提示有依赖的其他文件,则 用该命令可以对其进行强力删除。

  • 官网推荐彻底卸载
[root@localhost ~]# sudo yum erase $(rpm -qa | grep mongodb-org)
Loaded plugins: product-id, refresh-packagekit, search-disabled-repos, security, subscriptionmanager
This system is not registered with an entitlement server. You can use subscription-manager to
register.
Setting up Remove Process
Resolving Dependencies
--> Running transaction check
---> Package mongodb-org-mongos.x86_64 0:4.4.8-1.el6 will be erased
---> Package mongodb-org-server.x86_64 0:4.4.8-1.el6 will be erased
---> Package mongodb-org-shell.x86_64 0:4.4.8-1.el6 will be erased
--> Finished Dependency Resolution
Dependencies Resolved
====================================================================
=====================================
Package Arch Version Repository
Size
====================================================================
=====================================
Removing:
mongodb-org-mongos x86_64 4.4.8-1.el6 @/mongodb-org-mongos4.4.8-1.el6.x86_64 62 M
mongodb-org-server x86_64 4.4.8-1.el6 @/mongodb-org-server-4.4.8-
1.el6.x86_64 79 M
mongodb-org-shell x86_64 4.4.8-1.el6 @/mongodb-org-shell-4.4.8-
1.el6.x86_64 51 M
Transaction Summary
Page 5 of 21
====================================================================
=====================================
Remove 3 Package(s)
Installed size: 192 M
Is this ok [y/N]: y
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
 Erasing : mongodb-org-server-4.4.8-1.el6.x86_64
1/3
warning: /var/log/mongodb/mongod.log saved as /var/log/mongodb/mongod.log.rpmsave
warning: /etc/mongod.conf saved as /etc/mongod.conf.rpmsave
 Erasing : mongodb-org-shell-4.4.8-1.el6.x86_64
2/3
 Erasing : mongodb-org-mongos-4.4.8-1.el6.x86_64
3/3
 Verifying : mongodb-org-mongos-4.4.8-1.el6.x86_64
1/3
 Verifying : mongodb-org-shell-4.4.8-1.el6.x86_64
2/3
 Verifying : mongodb-org-server-4.4.8-1.el6.x86_64
3/3
Removed:
 mongodb-org-mongos.x86_64 0:4.4.8-1.el6 mongodb-org-server.x86_64
0:4.4.8-1.el6
 mongodb-org-shell.x86_64 0:4.4.8-1.el6
Complete!

5. 安装目录和核心文件

5.1 安装目录

5.2 执行文件

5.3 配置文件

[root@localhost ~]# vim /etc/mongod.conf

5.4 日志文件

[root@localhost ~]# cd /var/log/mongodb
mongod.log

6. 常见问题

6.1 1#问题

6.2 2#问题


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM