Centos 7安裝MongoDB 一.配置mongodb的yum源 二.yum安裝mondodb 三.啓動及配置 四.開啓認證後mongodb重啓方式 五.配置文件 參考

備註:
MongoDB 4.2 版本
CentOS 7.8

一.配置mongodb的yum源

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

二.yum安裝mondodb

[root@10-31-1-124 ~]# yum install -y mongodb-org
已加載插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.163.com
 * updates: mirrors.163.com
base                                                                                                                                                                                 | 3.6 kB  00:00:00     
extras                                                                                                                                                                               | 2.9 kB  00:00:00     
mongodb-org-4.2                                                                                                                                                                      | 2.5 kB  00:00:00     
updates                                                                                                                                                                              | 2.9 kB  00:00:00     
mongodb-org-4.2/7/primary_db                                                                                                                                                         |  47 kB  00:00:00     
正在解決依賴關係
--> 正在檢查事務
---> 軟件包 mongodb-org.x86_64.0.4.2.10-1.el7 將被 安裝
--> 正在處理依賴關係 mongodb-org-mongos = 4.2.10,它被軟件包 mongodb-org-4.2.10-1.el7.x86_64 需要
--> 正在處理依賴關係 mongodb-org-tools = 4.2.10,它被軟件包 mongodb-org-4.2.10-1.el7.x86_64 需要
--> 正在處理依賴關係 mongodb-org-shell = 4.2.10,它被軟件包 mongodb-org-4.2.10-1.el7.x86_64 需要
--> 正在處理依賴關係 mongodb-org-server = 4.2.10,它被軟件包 mongodb-org-4.2.10-1.el7.x86_64 需要
--> 正在檢查事務
---> 軟件包 mongodb-org-mongos.x86_64.0.4.2.10-1.el7 將被 安裝
---> 軟件包 mongodb-org-server.x86_64.0.4.2.10-1.el7 將被 安裝
---> 軟件包 mongodb-org-shell.x86_64.0.4.2.10-1.el7 將被 安裝
---> 軟件包 mongodb-org-tools.x86_64.0.4.2.10-1.el7 將被 安裝
--> 解決依賴關係完成

依賴關係解決

============================================================================================================================================================================================================
 Package                                               架構                                      版本                                              源                                                  大小
============================================================================================================================================================================================================
正在安裝:
 mongodb-org                                           x86_64                                    4.2.10-1.el7                                      mongodb-org-4.2                                    5.9 k
爲依賴而安裝:
 mongodb-org-mongos                                    x86_64                                    4.2.10-1.el7                                      mongodb-org-4.2                                     15 M
 mongodb-org-server                                    x86_64                                    4.2.10-1.el7                                      mongodb-org-4.2                                     25 M
 mongodb-org-shell                                     x86_64                                    4.2.10-1.el7                                      mongodb-org-4.2                                     17 M
 mongodb-org-tools                                     x86_64                                    4.2.10-1.el7                                      mongodb-org-4.2                                     62 M

事務概要
============================================================================================================================================================================================================
安裝  1 軟件包 (+4 依賴軟件包)

總下載量:119 M
安裝大小:283 M
Downloading packages:
警告:/var/cache/yum/x86_64/7/mongodb-org-4.2/packages/mongodb-org-4.2.10-1.el7.x86_64.rpm: 頭V3 RSA/SHA1 Signature, 密鑰 ID 058f8b6b: NOKEY                              ]  0.0 B/s | 181 kB  --:--:-- ETA 
mongodb-org-4.2.10-1.el7.x86_64.rpm 的公鑰尚未安裝
(1/5): mongodb-org-4.2.10-1.el7.x86_64.rpm                                                                                                                                           | 5.9 kB  00:00:01     
(2/5): mongodb-org-mongos-4.2.10-1.el7.x86_64.rpm                                                                                                                                    |  15 MB  00:00:06     
(3/5): mongodb-org-shell-4.2.10-1.el7.x86_64.rpm                                                                                                                                     |  17 MB  00:00:04     
(4/5): mongodb-org-tools-4.2.10-1.el7.x86_64.rpm                                                                                                                                     |  62 MB  00:00:13     
mongodb-org-server-4.2.10-1.el FAILED                                                  84% [================================================================-            ]  1.7 B/s | 100 MB 3202:08:53 ETA 
https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.2/x86_64/RPMS/mongodb-org-server-4.2.10-1.el7.x86_64.rpm: [Errno 12] Timeout on https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.2/x86_64/RPMS/mongodb-org-server-4.2.10-1.el7.x86_64.rpm: (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')
正在嘗試其它鏡像。
mongodb-org-server-4.2.10-1.el FAILED                                                  87% [===================================================================          ]  3.5 B/s | 104 MB 1237:08:30 ETA 
https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.2/x86_64/RPMS/mongodb-org-server-4.2.10-1.el7.x86_64.rpm: [Errno 12] Timeout on https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.2/x86_64/RPMS/mongodb-org-server-4.2.10-1.el7.x86_64.rpm: (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')
正在嘗試其它鏡像。
(5/5): mongodb-org-server-4.2.10-1.el7.x86_64.rpm                                                                                                                                    |  25 MB  00:00:05     
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
總計                                                                                                                                                                        215 kB/s | 119 MB  00:09:24     
從 https://www.mongodb.org/static/pgp/server-4.2.asc 檢索密鑰
導入 GPG key 0x058F8B6B:
 用戶ID     : "MongoDB 4.2 Release Signing Key <[email protected]>"
 指紋       : e162 f504 a20c df15 827f 718d 4b7c 549a 058f 8b6b
 來自       : https://www.mongodb.org/static/pgp/server-4.2.asc
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安裝    : mongodb-org-mongos-4.2.10-1.el7.x86_64                                                                                                                                                  1/5 
  正在安裝    : mongodb-org-tools-4.2.10-1.el7.x86_64                                                                                                                                                   2/5 
  正在安裝    : mongodb-org-shell-4.2.10-1.el7.x86_64                                                                                                                                                   3/5 
  正在安裝    : mongodb-org-server-4.2.10-1.el7.x86_64                                                                                                                                                  4/5 
Created symlink from /etc/systemd/system/multi-user.target.wants/mongod.service to /usr/lib/systemd/system/mongod.service.
  正在安裝    : mongodb-org-4.2.10-1.el7.x86_64                                                                                                                                                         5/5 
  驗證中      : mongodb-org-server-4.2.10-1.el7.x86_64                                                                                                                                                  1/5 
  驗證中      : mongodb-org-4.2.10-1.el7.x86_64                                                                                                                                                         2/5 
  驗證中      : mongodb-org-shell-4.2.10-1.el7.x86_64                                                                                                                                                   3/5 
  驗證中      : mongodb-org-tools-4.2.10-1.el7.x86_64                                                                                                                                                   4/5 
  驗證中      : mongodb-org-mongos-4.2.10-1.el7.x86_64                                                                                                                                                  5/5 

已安裝:
  mongodb-org.x86_64 0:4.2.10-1.el7                                                                                                                                                                         

作爲依賴被安裝:
  mongodb-org-mongos.x86_64 0:4.2.10-1.el7           mongodb-org-server.x86_64 0:4.2.10-1.el7           mongodb-org-shell.x86_64 0:4.2.10-1.el7           mongodb-org-tools.x86_64 0:4.2.10-1.el7          

完畢!
[root@10-31-1-124 ~]# 

三.啓動及配置

/var/lib/mongo(數據文件所在目錄)
/var/log/mongodb(日誌文件所在目錄)
/etc/mongod.conf(配置文件所在目錄)

-- 啓動mongodb
systemctl start mongod
-- 查看mongodb服務的狀態
systemctl status mongod
-- 開機啓動mongodb
systemctl enable mongod
-- 重啓mongodb服務
systemctl restart mongod

測試記錄:

[root@10-31-1-124 mongodb]# systemctl start mongod
[root@10-31-1-124 mongodb]# 
[root@10-31-1-124 mongodb]# 
[root@10-31-1-124 mongodb]# systemctl status mongod
● mongod.service - MongoDB Database Server
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
   Active: active (running) since 四 2020-11-05 15:29:24 CST; 53s ago
     Docs: https://docs.mongodb.org/manual
  Process: 12568 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=0/SUCCESS)
  Process: 12566 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 12562 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 12560 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
 Main PID: 12571 (mongod)
   CGroup: /system.slice/mongod.service
           └─12571 /usr/bin/mongod -f /etc/mongod.conf

11月 05 15:29:24 10-31-1-124 systemd[1]: Starting MongoDB Database Server...
11月 05 15:29:24 10-31-1-124 mongod[12568]: about to fork child process, waiting until server is ready for connections.
11月 05 15:29:24 10-31-1-124 mongod[12568]: forked process: 12571
11月 05 15:29:24 10-31-1-124 systemd[1]: Started MongoDB Database Server.
[root@10-31-1-124 mongodb]# 
[root@10-31-1-124 mongodb]# 
[root@10-31-1-124 mongodb]# systemctl enable mongod
[root@10-31-1-124 mongodb]# 
[root@10-31-1-124 mongodb]# systemctl restart mongod
[root@10-31-1-124 mongodb]# 

開始使用mongodb

[root@10-31-1-124 mongodb]# mongo
MongoDB shell version v4.2.10
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("8230936a-78e2-4c50-88a8-8adc43b8e83c") }
MongoDB server version: 4.2.10
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
Server has startup warnings: 
2020-11-05T15:33:01.738+0800 I  CONTROL  [initandlisten] 
2020-11-05T15:33:01.738+0800 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-11-05T15:33:01.738+0800 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2020-11-05T15:33:01.738+0800 I  CONTROL  [initandlisten] 
2020-11-05T15:33:01.739+0800 I  CONTROL  [initandlisten] 
2020-11-05T15:33:01.739+0800 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2020-11-05T15:33:01.739+0800 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-11-05T15:33:01.739+0800 I  CONTROL  [initandlisten] 
2020-11-05T15:33:01.739+0800 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2020-11-05T15:33:01.739+0800 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-11-05T15:33:01.739+0800 I  CONTROL  [initandlisten] 
---
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()
---

> 

四.開啓認證後mongodb重啓方式

mongod.conf 配置文件開啓了認證
security:
  authorization: enabled

使用systemctl啓動mongod報錯
[root@10-31-1-124 ~]# systemctl start mongod
Job for mongod.service failed because the control process exited with error code. See "systemctl status mongod.service" and "journalctl -xe" for details.

此時使用指定配置文件的方式啓動mongo

-- 關閉mongo
pkill mongod
-- 啓動mongo
mongod -f /etc/mongod.conf --fork

測試記錄:

[root@10-31-1-124 ~]# pkill mongod
[root@10-31-1-124 ~]# 
[root@10-31-1-124 ~]# mongod -f /etc/mongod.conf --fork
about to fork child process, waiting until server is ready for connections.
forked process: 5502
child process started successfully, parent exiting
[root@10-31-1-124 ~]# 
[root@10-31-1-124 ~]# 

五.配置文件

mongodb的配置文件有變動,參考官方文檔:
https://docs.mongodb.com/v4.2/administration/configuration/#configure-the-database

參考

1.https://docs.mongodb.com/v4.2/tutorial/install-mongodb-on-red-hat/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章