1、查看linux版本,之所以查看linux版本,是因为centos7和以前版本用的防火墙不一样,安装完mongodb后,需要关闭防火墙(或者其他方法,专业运维人员使用,我们这里为简单,直接关闭即可)才可以让其他电脑访问到,
cat /etc/redhat-release
2、以下步骤完全按照mongodb 官方文档操作:
Configure the package management system (yum).
Create a /etc/yum.repos.d/mongodb-org-3.2.repo file so that you can install MongoDB directly, using yum.
touch /etc/yum.repos.d/mongodb-org-3.2.repo #新建文件
vi /etc/yum.repos.d/mongodb-org-3.2.repo #编辑文件
运行上面的命令,打开文件后,按键盘i键,编辑此文件,直接复制如下内容带文件,然后按Esc 键,再按shift+:键,就是打出一个冒号,然后按x键,保存,不熟悉vi命令的童鞋看这个,熟悉的飘过
[mongodb-org-3.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.2.asc
3、安装最新版的mongodb
To install the latest stable version of MongoDB, issue the following command:
sudo yum install -y mongodb-org
4、启动mongodb
You can start the mongod process by issuing the following command:
sudo service mongod start
sudo service mongod status # 查看mongodb 状态
5、设置开机启动
You can optionally ensure that MongoDB will start following a system reboot by issuing the following command:
sudo chkconfig mongod on
6、停止mongodb
Stop MongoDB.
As needed, you can stop the mongod process by issuing the following command:
sudo service mongod stop
7、重启mongodb
Restart MongoDB.
You can restart the mongod process by issuing the following command:
sudo service mongod restart
8、设置局域网其他电脑可访问(重点)
sudo vi /etc/mongod.conf
找到这一段,将默认的127.0.0.1 改为 0.0.0.0
net:
port: 27017
bindIp: 0.0.0.0 # Listen to local interface only, comment to listen on all interfaces.
9、关闭防火墙
sudo service iptables stop #即时生效,重启后会失效,默认会开启防火墙
sudo chkconfig iptables off #防火墙关闭,重启后也是关闭状态
sudo service iptables start # 开启防火墙
sudo chkconfig iptables on #防火墙开启
sudo service iptables status #查看防火墙状态
10、官网上有这么一段:
To configure SELinux, administrators have three options:
- If SELinux is in enforcing mode, enable access to the relevant ports that the MongoDB deployment will use (e.g. 27017). See Default MongoDB Port for more information on MongoDB’s default ports. For default settings, this can be accomplished by running
semanage port -a -t mongod_port_t -p tcp 27017
经本人测试,无需理会,可以忽略,如果有的同学执行这条命令提示:
semanage SELinux Command Not Found
,可以按照以下步骤安装semanage,运行下面两条命令中的任意一条即可,
yum provides /usr/sbin/semanage
或者:
yum whatprovides /usr/sbin/semanage
执行后输出类似这样的内容:
Loaded plugins: rhnplugin
policycoreutils-python-2.0.83-19.8.el6_0.x86_64 : SELinux policy core python utilities
Repo : rhel-x86_64-server-6
Matched from:
Filename : /usr/sbin/semanage
policycoreutils-python-2.0.83-19.1.el6.x86_64 : SELinux policy core python utilities
Repo : rhel-x86_64-server-6
Matched from:
Filename : /usr/sbin/semanage
然后执行下面这条命令:
yum -y install policycoreutils-python