1、官网下载tar包:
https://www.mongodb.com/download-center#community
2、解压
tar -xf mongodb-linux-x86_64-rhel70-3.6.5.tgz && mv mongodb-linux-x86_64-rhel70-3.6.5 /usr/local/mongodb
3、创建数据、日志、配置文件目录;创建用户、授权
mkdir -p /usr/local/mongodb/{data,log,conf}
4、编写配置文件
vim /usr/local/mongodb/conf/mongod.conf
systemLog:
destination: file
path: /usr/local/mongodb/log/logs
logAppend: true
storage:
journal:
enabled: true
dbPath: /usr/local/mongodb/data
directoryPerDB: true
engine: wiredTiger
wiredTiger:
engineConfig:
cacheSizeGB: 16
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
net:
port: 27017
processManagement:
fork: true
5、创建用户、授权
useradd mongod
chown -R mongod:mongod /usr/local/mongodb/
5、启动mongodb
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongod.conf
6、编写启动脚本
vim /lib/systemd/system/mongodb.service
[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/conf/mongod.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/usr/local/mongodb/bin/mongod --shutdown --config /usr/local/mongodb/conf/mongod.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
7、加执行权限:
chmod 754 /lib/systemd/system/mongodb.service
8、启动关闭服务,设置开机自启
systemctl start mongodb.service
systemctl stop mongodb.service
#开机启动
systemctl enable mongodb.service
9、centos 6开机自启和启动脚本
vim /etc/rc.d/init.d/mongodb
#!/bin/bash
#chkconfig: 2345 80 90
#description: mongodb
start() {
/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/conf/mongod.conf
}
stop() {
/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/conf/mongod.conf --shutdown
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo
$"Usage: $0 {start|stop|restart}"
exit 1
esac
10、设置开机自启
chmod +x /etc/rc.d/init.d/mongod
chkconfig --add mongod
chkconfig --level 345 mongod on
chkconfig --list mongod
service mongodb start