建立yum源文件:php
#cd /etc/yum.repos.d mongodb
#vim mongodb-org-4.0.repo shell
使用阿里雲的源數據庫
[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驗證vim
安裝以前先更新全部包 :bash
# yum updatephp7
2.安裝MongoDB
安裝命令:
yum -y install mongodb-orgtcp
安裝完成後post
查看mongo安裝位置 whereis mongod阿里雲
查看修改配置文件 : vim /etc/mongod.conf
bindIp: 172.0.0.1 改成 bindIp: 0.0.0.0
(注意冒號與ip之間須要一個空格)
3.啓動MongoDB
啓動mongodb :systemctl start mongod.service
中止mongodb :systemctl stop mongod.service
![](http://static.javashuo.com/static/loading.gif)
查到mongodb的狀態:systemctl status mongod.service
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
命令:mongo
![](http://static.javashuo.com/static/loading.gif)
查看數據庫:show dbs
編輯mongod.conf註釋bindIp,並重啓mongodb.
vim /etc/mongod.conf
![](http://static.javashuo.com/static/loading.gif)
重啓mongodb:systemctl restart mongod.service
安裝完成後若是須要安裝php擴展
php7.0以上版本
使用命令
/usr/local/php7/bin/pecl install mongodb
安裝成功後
![](http://static.javashuo.com/static/loading.gif)