0%

2019-11-25 centos7 下安装和配置 mongodb 以及防火墙开启

centos7 下安装和配置 mongodb 以及防火墙开启

参考链接:https://www.jianshu.com/p/9e16af3eb7ee

mongodb 官网:https://www.mongodb.com

官网下载地址:https://www.mongodb.com/download-center/community

1、下载安装包

由于本人的服务器是 Linux 下的 centos 系统,因此 Version(版本号)选择最新的,但 os(系统)中找不到 centos 这让我很是懵逼。

简单搜索后发现 centos 下要安装的是 Amazon Linux 这一个。

而 Package 一般选择 TGZ 也就是压缩包安装。

所以当前 centos 下最新版本的下载链接为:https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-amazon-4.2.1.tgz

如图所示。(最后注意下压缩包后缀名是 tgz 应该就是对的了)

mark

剩下的参考https://www.jianshu.com/p/9e16af3eb7ee 即可

此外附上 mongodb.conf 的配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# mongod.conf

# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.配置数据存储路径
storage:
dbPath: /usr/local/mongodb/data
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:

# where to write logging data.
# 配置日志路径
systemLog:
destination: file
logAppend: true
path: /usr/local/mongodb/log/mongodb.log

# network interfaces 配置端口
net:
port: 27017
bindIp: 0.0.0.0


# how the process runs
#processManagement:
# timeZoneInfo: /usr/share/zoneinfo
processManagement:
fork: true
#启用权限验证
security:
authorization: enabled
setParameter:
enableLocalhostAuthBypass: true
#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:

2、解压

tar -zxvf mongodb-linux-x86_64-rhel70-4.0.4.tgz


3、创建 mongodb 文件夹

mkdir -p /usr/local/mongodb


4、移动到指定位置

mv mongodb-linux-x86_64-rhel70-4.0.4 /usr/local/mongodb


5、在/usr/local/mongodb 下创建文件夹

mkdir -p /data/db

mkdir logsplainplainplainplainplainplainplainplainplainplainplainplainplainplainplainplainplainplainplainplain

6、在/usr/local/mongodb/bin 下新建配置

vi mongodb.conf

dbpath = /usr/local/mongodb/data/db #数据文件存放目录

logpath = /usr/local/mongodb/logs/mongodb.log #日志文件存放目录

logappend=true #日志追加

port = 27017 #端口

auth=true # 启用验证

fork = true #以守护程序的方式启用,即在后台运行

bind_ip=0.0.0.0 修改配置文件的 bind_ip, 默认是 127.0.0.1 只限于本机连接。所以安装完成后必须把这个修改为 0.0.0.0 ,否则通过别的机器是没法连接的


7、环境变量配置

vi /etc/profile

export MONGODB_HOME=/usr/local/mongodb

exportPATH=$PATH:$MONGODB_HOME/bin

保存后,重启系统配置

source /etc/profile

8、启动

在/usr/local/mongodb/bin 下

mongod -f mongodb.conf 或 ./mongod -f mongodb.conf

9、关闭

mongod -f ./mongodb.conf –shutdown 或./mongod -f ./mongodb.conf –shutdown

如果在自己服务器上想开启远端访问功能,需要开启防火墙

1、通过 systemctl status firewalld 查看 firewalld 状态,发现当前是 dead 状态,即防火墙未开启。

img

2、通过 systemctl start firewalld 开启防火墙,没有任何提示即开启成功

img

3、再次通过 systemctl status firewalld 查看 firewalld 状态,显示 active(running)即已开启了

img

4、如果要关闭防火墙设置,可能通过 systemctl stop firewalld 这条指令来关闭该功能,没有任何提示则关闭成功。

img

5、开启端口,再次执行执行 firewall-cmd –permanent –zone=public –add-port=27017/tcp –permanent,提示 success,表示设置成功,这样就可以继续后面的设置了。

img

6、查看端口,执行 firewall-cmd –permanent –query-port=27017/tcp,提示 yes,即查询成功

img

7、重启防火墙,执行 firewall-cmd –reload,提示 success 即执行成功

img

本文作者:草梅友仁
本文地址: https://blog.cmyr.ltd/archives/852edd17.html
版权声明:转载请注明出处!

坚持原创技术分享,您的支持将鼓励我继续创作!