前言
IOT产品中的组态,因为数据存储的多样性,以及数据量在单个字段中较大,综合考虑,团队采用mongodb存储组态相关的数据
一、下载文件
wget https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.0/x86_64/RPMS/mongodb-org-server-4.0.5-1.el7.x86_64.rpm
wget https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.0/x86_64/RPMS/mongodb-org-shell-4.0.5-1.el7.x86_64.rpm
wget https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.0/x86_64/RPMS/mongodb-org-tools-4.0.5-1.el7.x86_64.rpm
wget https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.0/x86_64/RPMS/mongodb-org-mongos-4.0.5-1.el7.x86_64.rpm
二、安装程序
安装mongodb服务端
[root@localhost u1]# rpm -ivh mongodb-org-server-4.0.5-1.el7.x86_64.rpm
安装mongodb命令行
rpm -ivh mongodb-org-shell-4.0.5-1.el7.x86_64.rpm
安装mongodb工具
如果需要一些附加工具,例如数据导入导出,则需要安装 tool 程序包
rpm -ivh mongodb-org-tools-4.0.5-1.el7.x86_64.rpm
集群
如果需要做集群,才需要这一步,我们采用单节点部署,所以这一步没有做
rpm -ivh mongodb-org-mongos-4.0.5-1.el7.x86_64.rpm
三、修改配置文件
先备份,初始化的mongodb配置文件,已方便还原最初始配置
cp /etc/mongod.conf /etc/mongod.conf_bak
创建mongodb专属目录
mkdir /data/mongodb/data
mkdir /data/mongodb/log
vim /etc/mongod.conf 修改如下
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /data/mongodb/log/mongod.log #日志文件
# Where and how to store data.
storage:
dbPath: /data/mongodb/data #数据文件
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /data/mongodb/mongod.pid # location of pidfile # 修改mongodb进程PID,同时需要修改 /usr/lib/systemd/system/mongod.service中mongdobpid
timeZoneInfo: /usr/share/zoneinfo
# 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. #绑定所有机器IP
#security:
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options
#auditLog:
#snmp:
修改启动方法中mongodb的pid路径,不然启动失败
vim /usr/lib/systemd/system/mongod.service
[Unit]
Description=MongoDB Database Server
After=network.target
Documentation=https://docs.mongodb.org/manual
[Service]
User=mongod
Group=mongod
Environment="OPTIONS=-f /etc/mongod.conf"
EnvironmentFile=-/etc/sysconfig/mongod
ExecStart=/usr/bin/mongod $OPTIONS
ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb
ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb
ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb
PermissionsStartOnly=true
PIDFile=/data/mongodb/mongod.pid #需要把这个mongodb pid配置和上面/etc/mongo.conf文化中的pid路径保持一致
Type=forking
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# locked memory
LimitMEMLOCK=infinity
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false
# Recommended limits for for mongod as specified in
# http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings
[Install]
WantedBy=multi-user.target
四、启动mongodb
systemctl daemon-reload
systemctl start mongod
校验如下
mongo --port 27017
启动,如果报错:
mongodb --fork报错 -ERROR: child process failed, exited with error number 1
一般这种情况是权限问题
解决办法就是
chown -R mongod:mongod /data/mongodb
其中这个mongod用户是刚启动报错时,mongodb自己创建的用户
参考:
https://www.jianshu.com/p/d3b31b7aa182