下载MongoDB安装文件:https://download.csdn.net/download/qq_30788845/10520448
解压丶更名丶移动
tar -zxvf mongodb-linux-x86_64-4.0.0.tgz
mv mongodb-linux-x86_64-4.0.0 mongodb
mv mongodb /usr/local
创建数据和日志存储路径
cd /usr/local/mongodb
mkdir data && mkdir data/db
touch data/logs
编写配置文件
cd /usr/local/mongodb/bin
vi mongodb.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/logs/mongodb.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: /var/run/mongodb/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27017
#bindIp 必须为0.0.0.0否则无法远程连接
bindIp: 0.0.0.0 # Listen to local interface only, comment to listen on all interfaces.
#security:
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options
#auditLog:
#snmp:
注意:开启守护进程模式fork的时候,一定要设置log日志; 设置log日志时,logpath路径必须为文件路径;
设置dbpath时,路径必须为文件夹路径;
启动
./mongod -f mongodb.conf
查看输出确认启动成功
测试
./mongo 127.0.0.1
查看输出确认测试成功
自启动配置
cd /usr/lib/systemd/system
vi mongodb.service
[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/bin/mongodb.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/usr/local/mongodb/bin/mongod --shutdown -f /usr/local/mongodb/bin/mongodb.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
开启启动
systemctl enable mongodb.service
启动
systemctl start mongodb.service
关闭
systemctl stop mongodb.service