MongoDB

Centos7 安装mongodb 4.x

2018-12-15  本文已影响357人  Devops海洋的渔夫

原创内容,转载请注明出处

参考文献

官方文档

概述

使用本教程使用.rpm 软件包在Red Hat Enterprise Linux或CentOS Linux版本6和7上安装MongoDB Community Edition 。

本安装指南仅支持64位系统。有关更多信息,请参阅 支持的平台

生产笔记

在生产环境中部署MongoDB之前,请考虑 生产注释文档。

安装MongoDB的社区版

注意
要安装不同版本的MongoDB,请参阅该版本的文档。要安装以前的版本,请参阅3.6版的教程。

本安装指南仅支持64位系统。有关更多信息,请参阅 支持的平台

安装MongoDB社区版的方式

官方介绍安装有两种方式

首先下面来看看yum安装方式。

使用yum安装

配置包管理系统(yum)。

创建一个/etc/yum.repos.d/mongodb-org-4.0.repo文件,以便您可以使用yum以下命令直接安装MongoDB :

mongodb-org-4.0.repo文件如下:

[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
在服务器上写上yum源文件

注意
您可以在存储库本身中找到.repo每个版本的文件。奇数次要版本(例如3.5)是开发版本,不适合生产使用

安装MongoDB包。

要安装最新的稳定版MongoDB,请发出以下命令:

sudo yum install -y mongodb-org
安装完毕

查看一下安装完毕之后,mongo是什么版本:

[root@server81 yum.repos.d]# mongo
mongo         mongodump     mongofiles    mongorestore  mongostat     
mongod        mongoexport   mongoimport   mongos        mongotop      
[root@server81 yum.repos.d]# mongo --version
MongoDB shell version v4.0.4
git version: f288a3bdf201007f3693c58e140056adf8b04839
OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
allocator: tcmalloc
modules: none
build environment:
    distmod: rhel70
    distarch: x86_64
    target_arch: x86_64
[root@server81 yum.repos.d]# 

那么假设需要安装特定的版本号呢?
可以先卸载掉sudo yum erase -y mongodb-org,再安装看看。

要安装特定版本的MongoDB,请单独指定每个组件包,并将版本号附加到包名称,如以下示例所示:

sudo yum install -y mongodb-org-4.0.4 mongodb-org-server-4.0.4 mongodb-org-shell-4.0.4 mongodb-org-mongos-4.0.4 mongodb-org-tools-4.0.4

这种安装方式虽然方便,但是存在yum源解决的情况。为了保证可以方便重复安装,最好对yum源的rpm包进行离线。

您可以指定任何可用的MongoDB版本。但是yum ,当有更新的版本可用时,请升级软件包。为防止意外升级,请将包装固定。要固定包,请将以下exclude指令添加到您的/etc/yum.conf文件中:

exclude=mongodb-org,mongodb-org-server,mongodb-org-shell,mongodb-org-mongos,mongodb-org-tools

缓存yum安装包

为了方便下次再次安装同一个版本,我习惯将yum安装的版本的rpm包下载下来,可以再下次安装的时候直接使用。

yum install -y mongodb-org --downloadonly --downloaddir=/work/mongodb

下载下来的rpm包如下:

[root@server81 mongodb]# ls -ll -h
total 77M
-rw-r--r-- 1 root root 5.9K Nov  7 04:12 mongodb-org-4.0.4-1.el7.x86_64.rpm
-rw-r--r-- 1 root root  13M Nov  7 04:12 mongodb-org-mongos-4.0.4-1.el7.x86_64.rpm
-rw-r--r-- 1 root root  22M Nov  7 04:12 mongodb-org-server-4.0.4-1.el7.x86_64.rpm
-rw-r--r-- 1 root root  14M Nov  7 04:12 mongodb-org-shell-4.0.4-1.el7.x86_64.rpm
-rw-r--r-- 1 root root  30M Nov  7 04:12 mongodb-org-tools-4.0.4-1.el7.x86_64.rpm
[root@server81 mongodb]# 

执行安装下载的rpm包

[root@server81 mongodb]# rpm -ivh mongodb-org-server-4.0.4-1.el7.x86_64.rpm 
Preparing...                          ################################# [100%]
Updating / installing...
   1:mongodb-org-server-4.0.4-1.el7   ################################# [100%]
Created symlink from /etc/systemd/system/multi-user.target.wants/mongod.service to /usr/lib/systemd/system/mongod.service.
[root@server81 mongodb]# 
[root@server81 mongodb]# rpm -ivh mongodb-org-mongos-4.0.4-1.el7.x86_64.rpm 
Preparing...                          ################################# [100%]
Updating / installing...
   1:mongodb-org-mongos-4.0.4-1.el7   ################################# [100%]
[root@server81 mongodb]# 
[root@server81 mongodb]# rpm -ivh mongodb-org-shell-4.0.4-1.el7.x86_64.rpm 
Preparing...                          ################################# [100%]
Updating / installing...
   1:mongodb-org-shell-4.0.4-1.el7    ################################# [100%]
[root@server81 mongodb]# 
[root@server81 mongodb]# rpm -ivh mongodb-org-tools-4.0.4-1.el7.x86_64.rpm 
Preparing...                          ################################# [100%]
Updating / installing...
   1:mongodb-org-tools-4.0.4-1.el7    ################################# [100%]
[root@server81 mongodb]# 
[root@server81 mongodb]# rpm -ivh mongodb-org-4.0.4-1.el7.x86_64.rpm 
Preparing...                          ################################# [100%]
Updating / installing...
   1:mongodb-org-4.0.4-1.el7          ################################# [100%]
[root@server81 mongodb]# 
[root@server81 mongodb]# rpm -qa | grep mongodb
mongodb-org-shell-4.0.4-1.el7.x86_64
mongodb-org-4.0.4-1.el7.x86_64
mongodb-org-tools-4.0.4-1.el7.x86_64
mongodb-org-server-4.0.4-1.el7.x86_64
mongodb-org-mongos-4.0.4-1.el7.x86_64
[root@server81 mongodb]# 

整理一下安装的shell脚本吧。

安装脚本install_mongodb.sh

[root@server81 mongodb]# vim install_mongodb.sh 

rpm -ivh mongodb-org-server-4.0.4-1.el7.x86_64.rpm
rpm -ivh mongodb-org-mongos-4.0.4-1.el7.x86_64.rpm
rpm -ivh mongodb-org-shell-4.0.4-1.el7.x86_64.rpm
rpm -ivh mongodb-org-tools-4.0.4-1.el7.x86_64.rpm
rpm -ivh mongodb-org-4.0.4-1.el7.x86_64.rpm

卸载脚本erase_mongodb.sh

[root@server81 mongodb]# vim erase_mongodb.sh 

rpm -qa | grep mongodb | while read line
do
#    echo $line
    rpm -e --nodeps $line
done

运行MongoDB社区版

先决条件 - 配置selinux

重要

如果您使用SELinux,则必须配置SELinux以允许MongoDB在基于Red Hat Linux的系统(Red Hat Enterprise Linux或CentOS Linux)上启动。

要配置SELinux,管理员有三个选项:

semanage port -a -t mongod_port_t -p tcp 27017
SELINUX=disabled

必须重新引导系统才能使更改生效。

SELINUX=permissive

必须重新引导系统才能使更改生效。

您可以改为使用setenforce更改为permissive模式。 setenforce不需要重新启动但不是持久性的。

或者,您可以选择在安装Linux操作系统时不安装SELinux软件包,或选择删除相关软件包。此选项是最具侵入性的,不建议使用。

目录和权限

警告

在RHEL 7.0上,如果更改数据路径,则默认 SELinux策略将阻止mongod在不更改安全上下文时对新数据路径具有写访问权限。

默认情况下,MongoDB实例存储:

如果通过包管理器安装,则会在安装期间创建目录。

如果通过下载tarball手动安装,则可以使用或根据将运行MongoDB的用户创建目录。(有关和的信息,请参阅Linux手册页。)mkdir -p <directory>``sudo mkdir -p <directory>``mkdir``sudo

默认情况下,MongoDB使用mongod用户帐户运行。如果更改运行MongoDB进程的用户,则还必须修改/var/lib/mongo/var/log/mongodb 目录的权限,以授予此用户访问这些目录的权限。

要指定不同的日志文件目录和数据文件目录,请编辑systemLog.path和中的storage.dbPath设置/etc/mongod.conf。确保运行MongoDB的用户可以访问这些目录。

ulimit的配置

大多数类Unix操作系统限制会话可能使用的系统资源。这些限制可能会对MongoDB操作产生负面影响。有关更多信息,请参见UNIX ulimit设置

[root@server81 ~]# ulimit -n
1024
[root@server81 ~]# 
[root@server81 ~]# ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 15012
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 15012
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
[root@server81 ~]# 

从上面可以看出centos7默认的ulimit只能打开1024的文件数量,如果是生产环境,应该适当调大文件打开数目进行优化。

启动mongodb

sudo service mongod start

[root@server81 ~]# sudo service mongod start
Redirecting to /bin/systemctl start mongod.service
[root@server81 ~]# 
[root@server81 ~]# 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: active (running) since Sat 2018-12-15 16:33:26 HKT; 6min ago
     Docs: https://docs.mongodb.org/manual
  Process: 1031 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=0/SUCCESS)
  Process: 1029 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 1025 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 1014 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
 Main PID: 1489 (mongod)
   Memory: 22.9M
   CGroup: /system.slice/mongod.service
           └─1489 /usr/bin/mongod -f /etc/mongod.conf

Dec 15 16:33:05 server81 systemd[1]: Starting MongoDB Database Server...
Dec 15 16:33:20 server81 mongod[1031]: 2018-12-15T16:33:20.851+0800 I CONTROL  [m...e'
Dec 15 16:33:20 server81 mongod[1031]: about to fork child process, waiting until...s.
Dec 15 16:33:20 server81 mongod[1031]: forked process: 1489
Dec 15 16:33:26 server81 systemd[1]: Started MongoDB Database Server.
Hint: Some lines were ellipsized, use -l to show in full.
[root@server81 ~]# 

查看mongoDB的启动服务端口

查看mongoDB的配置文件/etc/mongod.conf,可以知道服务启动的端口号。当然,也可以进行修改。

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


# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# Where and how to store data.
storage:
  dbPath: /var/lib/mongo
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  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.


#security:

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:

可以从上面看出,mongoDB的服务只绑定了 127.0.0.1 的 27017 端口,并没有让本地网卡IP提供服务。
下面可以测试看看。

[root@server81 ~]# nc -vv 127.0.0.1 27017
Ncat: Version 7.50 ( https://nmap.org/ncat )
NCAT DEBUG: Using system default trusted CA certificates and those in /usr/share/ncat/ca-bundle.crt.
NCAT DEBUG: Unable to load trusted CA certificates from /usr/share/ncat/ca-bundle.crt: error:02001002:system library:fopen:No such file or directory
libnsock nsi_new2(): nsi_new (IOD #1)
libnsock nsock_connect_tcp(): TCP connection requested to 127.0.0.1:27017 (IOD #1) EID 8
libnsock nsock_trace_handler_callback(): Callback: CONNECT SUCCESS for EID 8 [127.0.0.1:27017]
Ncat: Connected to 127.0.0.1:27017.
libnsock nsi_new2(): nsi_new (IOD #2)
libnsock nsock_read(): Read request from IOD #1 [127.0.0.1:27017] (timeout: -1ms) EID 18
libnsock nsock_readbytes(): Read request for 0 bytes from IOD #2 [peer unspecified] EID 26

libnsock nsock_trace_handler_callback(): Callback: READ SUCCESS for EID 26 [peer unspecified] (1 bytes): .
libnsock nsock_trace_handler_callback(): Callback: WRITE SUCCESS for EID 35 [127.0.0.1:27017]
libnsock nsock_readbytes(): Read request for 0 bytes from IOD #2 [peer unspecified] EID 42
^C
[root@server81 ~]# 
[root@server81 ~]# nc -vv 172.16.5.181 27017
Ncat: Version 7.50 ( https://nmap.org/ncat )
NCAT DEBUG: Using system default trusted CA certificates and those in /usr/share/ncat/ca-bundle.crt.
NCAT DEBUG: Unable to load trusted CA certificates from /usr/share/ncat/ca-bundle.crt: error:02001002:system library:fopen:No such file or directory
libnsock nsi_new2(): nsi_new (IOD #1)
libnsock nsock_connect_tcp(): TCP connection requested to 172.16.5.181:27017 (IOD #1) EID 8
libnsock nsock_trace_handler_callback(): Callback: CONNECT ERROR [Connection refused (111)] for EID 8 [172.16.5.181:27017]
Ncat: Connection refused.
[root@server81 ~]# 

那么如果外部服务器需要访问该台mongodb的服务,就需要修改一下绑定的IP为0.0.0.0了。下面修改一下配置文件如下:

[root@server81 ~]# vim /etc/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.

# 重启一下mongoDB的服务
[root@server81 ~]# service mongod restart
Redirecting to /bin/systemctl restart mongod.service
[root@server81 ~]# 

# 再次测试一下是否能够用网卡IP进行端口访问
[root@server81 ~]# nc -vv 172.16.5.181 27017
Ncat: Version 7.50 ( https://nmap.org/ncat )
NCAT DEBUG: Using system default trusted CA certificates and those in /usr/share/ncat/ca-bundle.crt.
NCAT DEBUG: Unable to load trusted CA certificates from /usr/share/ncat/ca-bundle.crt: error:02001002:system library:fopen:No such file or directory
libnsock nsi_new2(): nsi_new (IOD #1)
libnsock nsock_connect_tcp(): TCP connection requested to 172.16.5.181:27017 (IOD #1) EID 8
libnsock nsock_trace_handler_callback(): Callback: CONNECT SUCCESS for EID 8 [172.16.5.181:27017]
Ncat: Connected to 172.16.5.181:27017.
libnsock nsi_new2(): nsi_new (IOD #2)
libnsock nsock_read(): Read request from IOD #1 [172.16.5.181:27017] (timeout: -1ms) EID 18
libnsock nsock_readbytes(): Read request for 0 bytes from IOD #2 [peer unspecified] EID 26

libnsock nsock_trace_handler_callback(): Callback: READ SUCCESS for EID 26 [peer unspecified] (1 bytes): .
libnsock nsock_trace_handler_callback(): Callback: WRITE SUCCESS for EID 35 [172.16.5.181:27017]
libnsock nsock_readbytes(): Read request for 0 bytes from IOD #2 [peer unspecified] EID 42

libnsock nsock_trace_handler_callback(): Callback: READ SUCCESS for EID 42 [peer unspecified] (1 bytes): .
libnsock nsock_trace_handler_callback(): Callback: WRITE SUCCESS for EID 51 [172.16.5.181:27017]
libnsock nsock_readbytes(): Read request for 0 bytes from IOD #2 [peer unspecified] EID 58
^C
[root@server81 ~]# 

从上面的步骤可以看出,将配置文件中的127.0.0.1改为0.0.0.0的IP绑定之后,就可以正常使用网卡绑定的IP提供外部访问了。

查看MongoDB的日志信息

可以根据查看日志的方式(tail -f /var/log/mongodb/mongod.log)来查看服务是否启动成功。

[root@server81 ~]# tail -f /var/log/mongodb/mongod.log
2018-12-15T16:33:26.285+0800 I STORAGE  [initandlisten] createCollection: admin.system.version with provided UUID: c996783b-c85f-427a-93cb-43437199d3c7
2018-12-15T16:33:26.415+0800 I COMMAND  [initandlisten] setting featureCompatibilityVersion to 4.0
2018-12-15T16:33:26.564+0800 I STORAGE  [initandlisten] createCollection: local.startup_log with generated UUID: be8aa834-dcb2-4129-a030-b0f6f7739157
2018-12-15T16:33:26.623+0800 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/var/lib/mongo/diagnostic.data'
2018-12-15T16:33:26.671+0800 I NETWORK  [initandlisten] waiting for connections on port 27017
2018-12-15T16:33:26.735+0800 I STORAGE  [LogicalSessionCacheRefresh] createCollection: config.system.sessions with generated UUID: de45dec3-03a6-4225-bb18-83d4953cc2fc
2018-12-15T16:33:26.801+0800 I INDEX    [LogicalSessionCacheRefresh] build index on: config.system.sessions properties: { v: 2, key: { lastUse: 1 }, name: "lsidTTLIndex", ns: "config.system.sessions", expireAfterSeconds: 1800 }
2018-12-15T16:33:26.801+0800 I INDEX    [LogicalSessionCacheRefresh]     building index using bulk method; build may temporarily use up to 500 megabytes of RAM
2018-12-15T16:33:26.804+0800 I INDEX    [LogicalSessionCacheRefresh] build index done.  scanned 0 total records. 0 secs
2018-12-15T16:33:26.822+0800 I COMMAND  [LogicalSessionCacheRefresh] command config.$cmd command: createIndexes { createIndexes: "system.sessions", indexes: [ { key: { lastUse: 1 }, name: "lsidTTLIndex", expireAfterSeconds: 1800 } ], $db: "config" } numYields:0 reslen:114 locks:{ Global: { acquireCount: { r: 2, w: 2 } }, Database: { acquireCount: { w: 2, W: 1 } }, Collection: { acquireCount: { w: 2 } } } protocol:op_msg 129ms

确保MongoDB将在系统重新启动后启动

[root@server81 ~]# sudo chkconfig mongod on
Note: Forwarding request to 'systemctl enable mongod.service'.
[root@server81 ~]# 

停止MongoDB。

[root@server81 ~]# sudo service mongod stop
Redirecting to /bin/systemctl stop mongod.service
[root@server81 ~]# 

重启MongoDB。

[root@server81 ~]# sudo service mongod restart
Redirecting to /bin/systemctl restart mongod.service
[root@server81 ~]# 

您可以通过观察/var/log/mongodb/mongod.log文件中的输出来跟踪错误或重要消息的进程状态。

卸载MongoDB的社区版

要从系统中完全删除MongoDB,您必须删除MongoDB应用程序本身,配置文件以及包含数据和日志的任何目录。以下部分将指导您完成必要的步骤。

警告
此过程将完全删除MongoDB,其配置和所有 数据库。此过程不可逆,因此请确保在继续之前备份所有配置和数据。

停止MongoDB。

sudo service mongod stop

删除rpm

sudo yum erase $(rpm -qa | grep mongodb-org)

删除数据目录

删除MongoDB数据库和日志文件。
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongo

开始使用mongo

输入mongod命令,使用默认端口27017 连接到localhost上运行的shell :

[root@server81 mongodb]# mongo
MongoDB shell version v4.0.4
connecting to: mongodb://127.0.0.1:27017
Implicit session: session { "id" : UUID("f684453b-2799-4f0c-b4c4-80a82af6e4f5") }
MongoDB server version: 4.0.4
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
    http://docs.mongodb.org/
Questions? Try the support group
    http://groups.google.com/group/mongodb-user
Server has startup warnings: 
2018-12-15T16:54:51.953+0800 I CONTROL  [initandlisten] 
2018-12-15T16:54:51.953+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-12-15T16:54:51.953+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-12-15T16:54:51.953+0800 I CONTROL  [initandlisten] 
2018-12-15T16:54:51.953+0800 I CONTROL  [initandlisten] 
2018-12-15T16:54:51.953+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-12-15T16:54:51.953+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-12-15T16:54:51.953+0800 I CONTROL  [initandlisten] 
2018-12-15T16:54:51.953+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-12-15T16:54:51.953+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-12-15T16:54:51.953+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()
---

> 

有关使用mongo shell 进行连接的更多信息,例如连接到mongod在不同主机和/或端口上运行的实例,请参阅mongo Shell

MongoDB提供了各种驱动程序版本的入门指南。有关可用版本,请参阅“ 入门”


关注微信公众号,回复【资料】、Python、PHP、JAVA、web,则可获得Python、PHP、JAVA、前端等视频资料。

上一篇下一篇

猜你喜欢

热点阅读