使用yum安装MongoDB
一、安装之前
- 使用清华的镜像源,新建 vim /etc/yum.repos.d/mongodb.repo ,内容为
[mongodb-org]name=MongoDB Repositorybaseurl=https://mirrors.tuna.tsinghua.edu.cn/mongodb/yum/el$releasever/gpgcheck=0enabled=1
二、安装
2. 刷新缓存并安装 mongodb-org 即可sudo yum makecachesudo yum install -y mongodb-org
三、启动
3.设置mongod启动与开机启动sudo systemctl start mongod && systemctl enable mongod#或 sudo service mongod start && chkconfig mongod on#启动sudo systemctl start mongod#或 service mongod start#重启sudo systemctl restart mongod#或 service mongod restart#开机启动sudo systemctl enable mongod#或 sudo service mongod star#关闭开机启动sudo systemctl disable mongod#或 sudo chkconfig mongod off
三、设置调整防火墙
FirewallD是Centos 8上的默认防火墙解决方案 在安装过程中,Nginx使用预定义的规则创建防火墙服务文件,以允许访问HTTP(80)和 HTTPS(443) 端口
4.使用以下命令永久打开必要的端口systemctl start firewalldsudo firewall-cmd --permanent --zone=public --add-service=httpsudo firewall-cmd --permanent --zone=public --add-service=httpsfirewall-cmd --zone=public --add-port=7001/tcp --permanentfirewall-cmd --zone=public --add-port=8080/tcp --permanentfirewall-cmd --zone=public --add-port=27017/tcp --permanentsudo firewall-cmd --reloadsystemctl restart firewalld
四、修改mongod.conf
- 修改mongod.conf配置文件ggdG 全删 ggyG 全部复制 ggvG ggVG 高亮显示 sudo vim /etc/mongod.conf 编辑完成重载配置文件 systemctl restart mongod
# where to write logging data.systemLog:destination: filelogAppend: truepath: /var/log/mongodb/mongod.log# Where and how to store data.storage:dbPath: /var/lib/mongojournal:enabled: true# engine:# wiredTiger:# how the process runsprocessManagement:fork: true # fork and run in backgroundpidFilePath: /var/run/mongodb/mongod.pid # location of pidfiletimeZoneInfo: /usr/share/zoneinfo# network interfacesnet:port: 27017bindIp: 0.0.0.0 #Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.#security:#operationProfiling:#replication:#sharding:## Enterprise-Only Options#auditLog:#snmp:
快速部署mongodb
#设置清华的mongodb镜像源 vim /etc/yum.repos.d/mongodb.repo[mongodb-org]name=MongoDB Repositorybaseurl=https://mirrors.tuna.tsinghua.edu.cn/mongodb/yum/el$releasever/gpgcheck=0enabled=1#-------------------------------------------sudo yum makecachesudo yum install mongodb-orgsystemctl start firewalldsudo firewall-cmd --permanent --zone=public --add-service=httpsudo firewall-cmd --permanent --zone=public --add-service=httpsfirewall-cmd --zone=public --add-port=7001/tcp --permanentfirewall-cmd --zone=public --add-port=8080/tcp --permanentfirewall-cmd --zone=public --add-port=27017/tcp --permanentsudo firewall-cmd --reloadsystemctl restart firewalldsudo systemctl start mongod && systemctl enable mongod
查看
查看redis文件所在地whereis mongod && which mongod && find / -name mongod查看redis状态systemctl status mongod查看redis进程ps -ef |grep mongod 或 ps aux | grep mongod查看端口情况netstat -lnp | grep 27017lsof -i:27017 -t#查看启动项的服务列表systemctl list-unit-files --type=service | grep enabled#查找mongod资源yum search mongod | grep -i --color mongod
关闭与卸载
#查看 reids 是否在运行,如果在运行则先关闭方式1: kill -9 <pid进程>方式2: sudo pkill mongod方式3: systemctl stop mongod#杀死端口kill -9 $(lsof -i:27017 -t)#卸载redis数据库yum -y remove mongod
安装包模式
一、下载解压编译安装
选择在Linux下安装redis,现在采用虚拟机安装的centos8 进行安装的
#1.1 下载redis安装包wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.6.tgz#1.2 解压redis安装包并且进入redis目录tar -zxvf ./mongodb-linux-x86_64-3.0.6.tgzmkdir -p /usr/local && mv ./mongodb-linux-x86_64-3.0.6 /usr/local/mongodbcd /usr/local/mongodb
二、设置全局变量
- 为redis设置全局变量 vim ~/.bash_profile 或 vim /etc/profile
```bash
设置暂时性全局变量
export PATH=$PATH:/usr/local/mongodb/binexport PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin #重置export
3.设置永久性全局变量 方式1:使用>>输入进文件 echo ‘export PATH=”$PATH:/usr/local/mongodb/bin”‘ >> ~/.bash_profile source ~/.bash_profile
方式2: vim ~/.bash_profile 或者 vim /etc/profile,添加下面代码
PATH=$PATH:/usr/local/mongodb/bin
```
使用永久性全局变量别忘了使文件生效
source ~/.bash_profile 或 source /etc/profile
