1、更新yum源
yum update
2、配置系统yum源
vim /etc/yum.repos.d/mongodb-org-4.2.repo [mongodb-org-4.2] name = MongoDB Repository baseurl = https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.2/x86_64/ gpgcheck = 1 enabled = 1 gpgkey = https://www.mongodb.org/static/pgp/server-4.2.asc
3、安装mongodb
yum install -y mongodb-org
4 、验证安装结果
rpm -qa |grep mongodb
5、启动mongodb
systemctl start mongod.service
6、MongoDB默认端口是27017,查看是否开启
netstat -natp | grep 27017
7、检查数据库是否安装成功
ps -aux | grep mongod
8、验证服务是否开启
mongo
sudo service mongod start 或者 systemctl start mongod.service # 开启MongoDB sudo chkconfig mongod on # 加入开机启动 sudo service mongod restart # 重启MongoDB sudo service mongod stop # 关闭防火墙 sudo yum erase $(rpm -qa | grep mongodb-org) # 卸载MongoDB sudo rm -r /var/log/mongodb # 删除日志文件 sudo rm -r /var/lib/mongo # 删除数据文件
远程连接
vi /etc/mongod.conf net: port: 27017 bindIp: 0.0.0.0
对外开放端口
systemctl status firewalld # 查看防火墙状态 firewall-cmd --zone=public --add-port=27017/tcp --permanent # mongodb默认端口号 firewall-cmd --reload # 重新加载防火墙 firewall-cmd --zone=public --query-port=27017/tcp # 查看端口号是否开放成功,输出yes开放成功,no则失败 or iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 27017 -j ACCEPT
远程连接
mongo 10.128.218.14:27017
修改mongodb.conf文件,启用身份验证
/etc/mongod.conf security: authorization: "enabled" # disable or enabled
mongo 10.128.218.14:27017:27017/database -u username -p password