下载:https://www.mongodb.com/download-center/community
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.3.tgz
解压安装
tar -xzvf mongodb-linux-x86_64-rhel70-4.2.3.tgz
mv mongodb-linux-x86_64-rhel70-4.2.3 /usr/local/mongodb
cd /usr/local/mongodb
groupadd mongodb
useradd -s /sbin/nologin -g mongodb -M mongodb
mkdir data log run
chown -R mongodb:mongodb data log run
配置
vi /usr/local/mongodb/mongodb.conf
#添加以下内容
bind_ip=0.0.0.0
port=27017
dbpath=/usr/local/mongodb/data/
logpath=/usr/local/mongodb/log/mongodb.log
pidfilepath =/usr/local/mongodb/run/mongodb.pid
logappend=true
fork=true
注册服务并设置开机启动
vi /usr/lib/systemd/system/mongodb.service
#添加以下内容
[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network.target
[Service]
Type=forking
User=mongodb
Group=mongodb
ExecStart=/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/mongodb.conf
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable mongodb
#启动
systemctl start mongodb
#关闭
systemctl stop mongodb
#查看状态
systemctl status mongodb
配置环境变量
vi /etc/profile
#最后添加一行
export PATH=/usr/local/mongodb/bin:$PATH
source /etc/profile
mongod --version
设置超级账号
mongo
use admin
db.createUser({user:"root",pwd:"123456",roles:["root"]})
exit
#重启
systemctl stop mongodb
systemctl start mongodb
参考自:https://blog.csdn.net/mrtwenty/article/details/100032821