文章目录
环境准备
本文主要介绍如何在Centos7环境下安装部署MongoDB,通过yum、rpm包、tar包三种方式实现。
yum安装
修改yum源
vim /etc/yum.repos.d/mongodb-org-4.4.repo
[mongodb-org-4.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc
安装
yum install -y mongodb-org
rpm包安装
创建相关目录
mkdir /usr/local/mongodb/
下载
cd /usr/local/mongodb/
wget https://repo.mongodb.org/yum/redhat/7Server/mongodb-org/4.4/x86_64/RPMS/mongodb-org-server-4.4.10-1.el7.x86_64.rpm
安装
rpm -ivh /usr/local/mongodb/mongodb-org-server-4.4.10-1.el7.x86_64.rpm
从安装提示可以看出用rpm -ivh安装缺少依赖,需要单独下载依赖包安装
yum install -y compat-openssl10
如果环境有公网访问权限可以直接用yum localinstall安装
yum localinstall mongodb-org-server-4.4.10-1.el7.x86_64.rpm
从安装信息可以看出,mongodb安装完成后已经自动开机启动项中,这样就可以解决服务器重启后还有手动启动mongodb服务的问题
tar包安装
创建相关目录
mkdir -p /usr/local/mongodb/data/db
mkdir /usr/local/mongodb/logs
下载
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.10.tgz
解压
tar -zxvf mongodb-linux-x86_64-rhel70-4.4.10.tgz
mv /usr/local/mongodb/mongodb-linux-x86_64-rhel70-4.4.10/* /usr/local/mongodb/
创建配置文件
MongoDB引入了一个YAML-based格式的配置文件。
vim /etc/mongod.conf
# 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: /var/log/mongodb/mongod.log
# Where and how to store data.
storage:
dbPath: /var/lib/mongo
journal:
enabled: true
# engine:
# 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: 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:
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options
#auditLog:
#snmp:
配置开机启动
新建mongod.service文件
vim /lib/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 /etc/mongod.conf"
EnvironmentFile=-/etc/sysconfig/mongod
ExecStart=/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/mongodb.conf
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.service权限
chmod +x mongod.service
添加环境变量
直接用export命令
export PATH=$PATH:/usr/local/mongodb/bin
修改profile文件
vim /etc/profile
新增:
export PATH="$PATH:/usr/local/mongodb/bin"
刷新profile文件
source /etc/profile
修改.bashrc文件
vim /root/.bashrc
新增:
export PATH="$PATH:/usr/local/mongodb/bin"
启动
/usr/local/mongodb/bin/mongod --config mongodb.conf
从提示信息可以看出缺少libcrypto.so.10依赖包,先解决依赖问题
yum install -y compat-openssl10
此时再次启动就可以成功
/usr/local/mongodb/bin/mongod --config mongodb.conf
启动
也可以用启动服务
systemctl start mongod
停止服务
systemctl stop mongod
添加开机自启动
systemctl enable mongod
重启服务
systemctl restart mongod
查看启动状态
systemctl status mongod
查看端口状态
netstat -ntlp
测试
链接
mongo
查看表
db
show dbs