MongoDB部署
# 一、单机部署
# 1.1 下载安装包
# 官方地址:https://www.mongodb.com/download-center/community
[root@db20 tools]# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.18.tgz
1
2
2
# 1.2 创建目录
[root@db20 tools]# useradd mongo
[root@db20 tools]# mkdir /opt/mongodb/{data,logs,conf} -p
[root@db20 tools]# tar -xf mongodb-linux-x86_64-4.0.18.tgz
[root@db20 tools]# cp -r mongodb-linux-x86_64-4.0.18/bin /opt/mongodb/
[root@db20 ~]# chown -R mongo.mongo /opt/mongodb
1
2
3
4
5
2
3
4
5
# 1.3 编辑配置文件
[root@db20 ~]# vim /opt/mongodb/conf/mongo.conf
systemLog:
destination: file
path: "/opt/mongodb/logs/mongodb.log" --日志位置
logAppend: true --日志以追加模式记录
--数据存储有关
storage:
journal:
enabled: true
dbPath: "/opt/mongodb/data" --数据路径的位置
-- 进程控制
processManagement:
fork: true --后台守护进程
pidFilePath: <string> --pid文件的位置,一般不用配置,可以去掉这行,自动生成到data中
--网络配置有关
net:
bindIp: 127.0.0.1 -- 监听地址
port: 27017 -- 端口号,默认不配置端口号,是27017
-- 安全验证有关配置
security:
authorization: enabled --是否打开用户名密码验证
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 1.4 配置系统启动
[root@db20 ~]# vim /etc/systemd/system/mongod.service
[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target
[Service]
User=mongo
Type=forking
ExecStart=/opt/mongodb/bin/mongod --config /opt/mongodb/conf/mongo.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/opt/mongodb/bin/mongod --config /opt/mongodb/conf/mongo.conf --shutdown
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@db20 ~]# systemctl daemon-reload
[root@db20 ~]# systemctl enable mongod
[root@db20 ~]# systemctl restart mongod
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 1.5 配置系统变量
# 为方便管理我们把mongodb的命令添加至系统变量
[root@db20 ~]# vim /etc/profile
export PATH=/opt/mongodb/bin:$PATH
[root@db20 ~]# source /etc/profile
1
2
3
4
2
3
4
# 二、docker部署(单机)
# 2.1 下载镜像
# docker地址:https://hub.docker.com/_/mongo?tab=tags
[root@db20 ~]# docker pull mongo:4.0
1
2
2
# 2.2 创建映射目录
[root@db20 ~]# mkdir data/mongodb/{logs,db,conf} -p
[root@db20 ~]# chown 999 /data/mongodb/*
[root@db20 ~]# vim /data/mongodb/conf/mongod.conf
systemLog:
destination: file
path: "/data/log/mongod.log"
logAppend: true
storage:
dbPath: /data/db/
journal:
enabled: true
#processManagement:
# fork: true --- 注释,因为在容器中,所以不能在后台运行
net:
bindIp: 127.0.0.1
port: 27017
setParameter:
enableLocalhostAuthBypass: false
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 2.3 启动
[root@db20 ~]# docker run -p 27017:27017 -d --name mongo \
-v /etc/localtime:/etc/localtime \
-v /data/mongodb/conf/mongod.conf:/data/configdb/mongod.conf \
-v /data/mongodb/logs:/data/log \
-v /data/mongodb/db:/data/db \
mongo:4.0 --config /data/configdb/mongod.conf
[root@db20 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bc93eb97ad0b mongo:4.0 "docker-entrypoint.s…" 9 minutes ago Up 9 minutes 0.0.0.0:27017->27017/tcp mongo
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 三、复制集(Replica Sets)
# 3.1 机器规划
主机名 | IP | 角色 |
---|---|---|
db20 | 172.16.10.20 | 主 |
db21 | 172.16.10.21 | 从 |
db22 | 172.16.10.22 | 从 |
# 3.2 目录准备
三个节点都需要创建对应的目录,用于存放数据、日志等信息
[root@db20 ~]# mkdir /opt/mongodb/27017/{data,logs,conf} -p
[root@db21 ~]# mkdir /opt/mongodb/27017/{data,logs,conf} -p
[root@db22 ~]# mkdir /opt/mongodb/27017/{data,logs,conf} -p
1
2
3
2
3
# 3.3 准备配置文件
配置文件三个节点除了绑定ip,其它都一样,这里以db20为例,其它节点复制即可(注意修改绑定ip)
[root@db20 ~]# vim /opt/mongodb/27017/conf/mongod.conf
systemLog:
destination: file
path: /opt/mongodb/27017/logs/mongodb.log
logAppend: true
storage:
journal:
enabled: true
dbPath: /opt/mongodb/27017/data
directoryPerDB: true
#engine: wiredTiger
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
processManagement:
fork: true
net:
bindIp: 172.16.10.20,127.0.0.1
port: 27017
replication:
oplogSizeMB: 2048
replSetName: my_mongo
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
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
# 3.4 启动
# #复制启动管理命令
[root@db20 ~]# tar -xf mongodb-linux-x86_64-4.0.18.tgz
[root@db20 ~]# cp -r mongodb-linux-x86_64-4.0.18/bin /opt/mongodb/27017/
[root@db21 ~]# tar -xf mongodb-linux-x86_64-4.0.18.tgz
[root@db21 ~]# cp -r mongodb-linux-x86_64-4.0.18/bin /opt/mongodb/27017/
[root@db22 ~]# tar -xf mongodb-linux-x86_64-4.0.18.tgz
[root@db22 ~]# cp -r mongodb-linux-x86_64-4.0.18/bin /opt/mongodb/27017/
# # 启动
[root@db20 ~]# /opt/mongodb/27017/bin/mongod -f /opt/mongodb/27017/conf/mongod.conf
[root@db21 ~]# /opt/mongodb/27017/bin/mongod -f /opt/mongodb/27017/conf/mongod.conf
[root@db22 ~]# /opt/mongodb/27017/bin/mongod -f /opt/mongodb/27017/conf/mongod.conf
# # 查看启动状态
[root@db20 ~]# netstat -nlpt|grep 27017
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 3.5 配置集群
# 初始化主节点 PRIMARY(db20节点)
[root@db20 ~]# mongo
> rs.initiate() -- 指定当前节点为主节点
{
"info2" : "no configuration specified. Using a default configuration for the set",
"me" : "172.16.10.20:27017",
"ok" : 1,
"operationTime" : Timestamp(1590644520, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1590644520, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
my_mongo:PRIMARY> rs.conf() -- 查看配置信息
# 添加从节点
my_mongo:PRIMARY> rs.add("172.16.10.21:27017")
{
"ok" : 1,
"operationTime" : Timestamp(1590645369, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1590645369, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
my_mongo:PRIMARY> rs.add("172.16.10.22:27017")
{
"ok" : 1,
"operationTime" : Timestamp(1590645375, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1590645375, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
# 设置从节点只读(2台从节点操作)
my_mongo:SECONDARY> rs.slaveOk()
# 副本集状态查看
my_mongo:PRIMARY> rs.status()
.....................
"members" : [
{
"_id" : 0,
"name" : "172.16.10.20:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 173488,
"optime" : {
"ts" : Timestamp(1590647641, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-05-28T06:34:01Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1590644520, 2),
"electionDate" : ISODate("2020-05-28T05:42:00Z"),
"configVersion" : 3,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "172.16.10.21:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 2277,
"optime" : {
"ts" : Timestamp(1590647641, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1590647641, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-05-28T06:34:01Z"),
"optimeDurableDate" : ISODate("2020-05-28T06:34:01Z"),
"lastHeartbeat" : ISODate("2020-05-28T06:34:06.577Z"),
"lastHeartbeatRecv" : ISODate("2020-05-28T06:34:05.160Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "172.16.10.20:27017",
"syncSourceHost" : "172.16.10.20:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 3
},
{
"_id" : 2,
"name" : "172.16.10.22:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 2271,
"optime" : {
"ts" : Timestamp(1590647641, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1590647641, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-05-28T06:34:01Z"),
"optimeDurableDate" : ISODate("2020-05-28T06:34:01Z"),
"lastHeartbeat" : ISODate("2020-05-28T06:34:06.739Z"),
"lastHeartbeatRecv" : ISODate("2020-05-28T06:34:05.402Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "172.16.10.21:27017",
"syncSourceHost" : "172.16.10.21:27017",
"syncSourceId" : 1,
"infoMessage" : "",
"configVersion" : 3
}
],
.....................
注意:members中state,如果这个值为 1,说明是主控节点(master);如果是2,说明是从属节点slave。在上面显示的当前主节点写入数据,到从节点上查看发现数据会同步。
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# 3.6 测试
# 主节点创建库,查看同步状态
my_mongo:PRIMARY> use sit
switched to db sit
my_mongo:PRIMARY> db.createCollection("123")
{
"ok" : 1,
"operationTime" : Timestamp(1590646947, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1590646947, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
# 从节点查看数据是否同步
my_mongo:SECONDARY> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
sit 0.000GB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 3.7命令补充
rs.status(); //查看整体复制集状态
rs.isMaster(); // 查看当前是否是主节点
rs.conf(); //查看复制集配置信息
rs.remove("ip:port"); // 删除一个节点
rs.add("ip:port"); // 新增从节点
rs.addArb("ip:port"); // 新增仲裁节点
1
2
3
4
5
6
2
3
4
5
6
编辑 (opens new window)
上次更新: 2023/06/09, 09:57:05