1.配置MongoDB的yum源
创建yum源文件:
#cd /etc/yum.repos.d
#vim mongodb-org-4.0.repo
添加以下内容:(我们这里使用阿里云的源)
[mngodb-org]
name=MongoDB Repository
baseurl=http://mirrors.aliyun.com/mongodb/yum/redhat/7Server/mongodb-org/4.0/x86_64/
gpgcheck=0
enabled=1
这里可以修改 gpgcheck=0, 省去gpg验证
安装之前先更新所有包 :
# yum update
2.安装MongoDB
安装命令:
yum -y install mongodb-org
安装完成后
查看mongo安装位置 whereis mongod
[root@localhost ~]# whereis mongod
mongod: /usr/bin/mongod /etc/mongod.conf /usr/share/man/man1/mongod.1
查看修改配置文件 : vim /etc/mongod.conf
bindIp: 172.0.0.1 改为 bindIp: 0.0.0.0
(注意冒号与ip之间需要一个空格)
3.启动MongoDB
启动mongodb :sudo systemctl start mongod.service
停止mongodb :sudo systemctl stop mongod.service
4.查到mongodb的状态:systemctl status mongod.service
[root@localhost ~]# systemctl status mongod.service
● mongod.service - MongoDB Database Server
Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
Active: active (running) since 六 2021-01-02 17:04:26 CST; 18min ago
Docs: https://docs.mongodb.org/manual
Main PID: 12259 (mongod)
Tasks: 34
Memory: 154.1M
CGroup: /system.slice/mongod.service
└─12259 /usr/bin/mongod -f /etc/mongod.conf
1月 02 17:04:23 localhost.localdomain systemd[1]: Starting MongoDB Database Server...
1月 02 17:04:23 localhost.localdomain mongod[12256]: about to fork child process, waiting until server is ready for connections.
1月 02 17:04:23 localhost.localdomain mongod[12256]: forked process: 12259
1月 02 17:04:26 localhost.localdomain mongod[12256]: child process started successfully, parent exiting
1月 02 17:04:26 localhost.localdomain systemd[1]: Started MongoDB Database Server.
4.外网访问需要关闭防火墙:
CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙。
关闭firewall:
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动
vim /etc/sysconfig/iptables
iptables文件添加
-A INPUT -m state --state NEW -m tcp -p tcp --dport 27017 -j ACCEPT
(注意:-A INPUT -m state --state NEW -m tcp -p tcp --dport 27017 -j ACCEPT要加在-A INPUT -j REJECT --reject-with icmp-host-prohibited之前,不然启动无效)
重启iptables
service iptables restart
5.设置开机启动
systemctl enable mongod.service
6.启动Mongo shell
命令:mongo
7.需要的话启用权限控制:
编辑mongod.conf注释bindIp,并重启mongodb.
vim /etc/mongod.conf
重启mongodb:systemctl restart mongod.service