CentOS7 部署MongoDB

安装MongoDB

wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.6.tgz
wget https://downloads.mongodb.com/compass/mongosh-0.15.3-linux-x64.tgz
tar zxvf mongodb-linux-x86_64-rhel70-4.4.6.tgz
tar zxvf mongosh-0.15.3-linux-x64.tgz
mkdir -p /usr/local/mongodb/bin
mv mongodb-linux-x86_64-rhel70-4.4.6/bin/* /usr/local/mongodb/bin
mv mongosh-0.15.3-linux-x64/bin/* /usr/local/mongodb/bin
rm -rf mongodb-linux-x86_64-rhel70-4.4.6
rm -rf mongosh-0.15.3-linux-x64
rm -rf /usr/local/mongodb/bin/install_compass
mkdir -p /data/mongodb/mongo
mkdir -p /data/mongodb/log
/usr/sbin/groupadd mongod
/usr/sbin/useradd -g mongod mongod
chown -R mongod:mongod /data/mongodb
echo -e '\nexport PATH=$PATH:/usr/local/mongodb/bin/\n' >> /etc/profile && source /etc/profile

可以将上述过程打包成mongobd-install.sh执行


配置mongod.conf

vi /usr/local/mongodb/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
  quiet: true
  logAppend: true
  path: /data/mongodb/log/mongod.log

# Where and how to store data.
storage:
  dbPath: /data/mongodb/mongo
  journal:
    enabled: true
  #engine:
  wiredTiger:
    engineConfig:
      cacheSizeGB: 1

# 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:
  #authorization: enabled

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:

如需使用密码登录,应修改以下参数:

# 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.


security:
  authorization: enabled

之后创建mongodb账户,并开启防火墙的27017端口


创建mongod服务

vi /etc/systemd/system/mongod.service
输入以下内容:

[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network-online.target
Wants=network-online.target

[Service]
User=mongod
Group=mongod
Environment="OPTIONS=-f /usr/local/mongodb/mongod.conf"
#EnvironmentFile=-/etc/sysconfig/mongod
ExecStart=/usr/local/mongodb/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=/var/run/mongodb/mongod.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 mongod as specified in
# https://docs.mongodb.com/manual/reference/ulimit/#recommended-ulimit-settings

[Install]
WantedBy=multi-user.target

启动mongod服务

systemctl enable mongod
systemctl start mongod