centos7 安装mongodb

官网下载安装介质: https://www.mongodb.com/download-center ,选择适当的版本,这
里以 linux 版本 mongodb-linux-x86_64-4.0.4 为例;
https://www.mongodb.org/dl/linux/x86_64

我下载的是mongodb-linux-x86_64-4.0.12.tgzcd

mkdir   /usr/local/soft/

cd /usr/local/soft/

解压mongodb压缩文件: tar -xzvf mongodb-linux-x86_64-4.0.12.tgzcd

重命名,方便后续操作:mv mongodb-linux-x86_64-4.0.12 mongodb

创建文件夹:mkdir -p mongodb/{data/db,log,conf}
编写配置:vi mongodb/conf/mgdb.conf
storage:
 dbPath: "/usr/local/soft/mongodb/data/db" 
systemLog:
 destination: file
 path: "/usr/local/soft/mongodb/log/mongodb.log" 
net:
 bindIp: 0.0.0.0 #本地监听 IP,0.0.0.0 表示本地所有 IP
 port: 27017  #端口,默认 27017,可以自定义
processManagement:
 fork: true
setParameter:
 enableLocalhostAuthBypass: false ##是否需要验证权限登录(用户名和密码)
配置环境变量:vi /etc/profile
export MONGODB_HOME=/usr/local/soft/mongodb
export PATH=$PATH:$MONGODB_HOME/bin

让配置文件生效:source /etc/profile

配置开机启动
vi /usr/lib/systemd/system/mongodb.service
[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target

[Service] 
Type=forking 
RuntimeDirectory=mongodb 
RuntimeDirectoryMode=0751
PIDFile=/usr/local/soft/mongodb/data/db/mongod.lock 
ExecStart=/usr/local/soft/mongodb/bin/mongod --config /usr/local/soft/mongodb/conf/mgdb.conf
ExecStop=/usr/local/soft/mongodb/bin/mongod --shutdown --config /usr/local/soft/mongodb/conf/mgdb.conf
PrivateTmp=true 
[Install] 
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start mongodb
systemctl enable mongodb
启动 mongodb
service mongodb stop
service mongodb start
 

开放端口

firewall-cmd --zone=public --add-port=27017/tcp --permanent   # 开放27017端口

##firewall-cmd --zone=public --remove-port=27017/tcp --permanent  #关闭27017端口

firewall-cmd --reload   # 配置立即生效

你可能感兴趣的:(centos7 安装mongodb)