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 安裝文件下載
- 官網地址:https://www.mongodb.com/
- 下載地址:https://www.mongodb.com/try/download
- 下載地址(社區):https://www.mongodb.com/try/download/community
- 下載地址(工具):https://www.mongodb.com/try/download/tools
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