先说下环境:
CentOS7.3,当然Debian/Ubuntu也基本一致。
步骤:
1.下载二进制安装包:
wget -c
https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.4.0.tgz
2.解压缩:
tar -zxvf mongodb-linux-x86_64-3.4.0.tgz
3.把二进制文件放到/usr/local/mongodb下面,当然不是必须要放这里的:
cp mongodb-linux-x86_64-3.4.0 /usr/local/mongodb -f
4.设置环境变量:
# 编辑/etc/profile文件
vi /etc/profile
# 增加一条环境变量设置
export PATH=/usr/local/mongodb/bin:$PATH
5.增加mongodb启动用户:
useradd -g mongod mongod
6.创建数据库文件夹,并给mongod权限,这里给放到/data/mongo下:
mkdir -p /data/mongo
chown -R mongod:mongod /data/mongo
7.创建日志目录,并给mongod权限
mkdir -p /var/log/mongodb
chown -R mongod:mongod /var/log/mongodb
8.创建进程管理目录,并给mongod权限
mkdir -p /var/run/mongodb
chown -R mongod:mongod /var/run/mongodb
9.创建mongodb配置文件mongod.conf,并给mongod权限
vi /etc/mongod.conf
chown mongod:mongod /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: /var/log/mongodb/mongod.log
# Where and how to store data.
storage:
dbPath: /data/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
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1 # Listen to local interface only, comment to listen on all interfaces.
#security:
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options
#auditLog:
#snmp:
保存
10.增加启动脚本,这里使用systemctl:
vi /usr/lib/systemd/system/mongod.service
内容如下:
[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target
Documentation=https://docs.mongodb.org/manual
[Service]
User=mongod
Group=mongod
Environment="OPTIONS=--quiet -f /etc/mongod.conf"
ExecStart=/usr/local/mongodb/bin/mongod $OPTIONS run
PIDFile=/var/run/mongodb/mongod.pid
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# 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
保存
11.启用启动脚本
systemctl enable mongod.service
12.使用服务:
# 启动:
service mongod start
# 停止:
service mongod stop
# 重启:
service mongod restart
如果mongo命令不可用,请检查环境变量的设置;如果mongo命令报错,请查看日志/var/log/mongodb/mongod.log
13.卸载:
# 停止服务:
service mongod stop
# 删除启动脚本
rm /usr/lib/systemd/system/mongod.service -f
# 重新加载脚本
systemctl daemon-reload
# 删除配置文件
rm /etc/mongod.conf -f
# 删除日志文件夹
rm /var/log/mongodb -rf
#删除进程管理文件夹
rm /var/run/mongodb -rf
#删除安装文件
rm /usr/local/mongodb -rf
#删除数据库文件
rm /data/mongo -rf
#清除环境变量
vi /etc/profile
#删除 export PATH=/usr/local/mongodb/bin:$PATH,并保存
(完)
本文作者:老徐
本文链接:https://bigger.ee/archives/19.html
转载时须注明出处及本声明