实践环境

  • 系统环境:debian12
  • mongodb版本:6.0.12
  • 单台服务器多实例(实用环境可以分布部署到多台服务器)

基本安装

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
## 二进制包下载
root@master-100:~# wget -c https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-debian11-6.0.12.tgz

## 解压移动
root@master-100:~# tar xf mongodb-linux-x86_64-debian11-6.0.12.tgz
root@master-100:~# mv mongodb-linux-x86_64-debian11-6.0.12 /usr/local/mongo

## 删除其他文件,创建所需目录
root@master-100:/usr/local/mongo# ls
bin LICENSE-Community.txt MPL-2 README THIRD-PARTY-NOTICES
root@master-100:/usr/local/mongo# ls | grep -v bin | xargs rm -f
root@master-100:/usr/local/mongo# ls
bin
root@master-100:/usr/local/mongo# mkdir data etc run log
root@master-100:/usr/local/mongo# ls
bin data etc log run

## 确保环境变量可用
root@master-100:/usr/local/mongo# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/elastic/jdk/bin:/usr/local/node/bin:/usr/local/mysql/bin:/usr/local/mongo/bin:/usr/local/redis/bin

## 查看版本,发现缺少运行依赖库
root@master-100:/usr/local/mongo# ls bin
install_compass mongod mongos
root@master-100:/usr/local/mongo# mongod --version
mongod: error while loading shared libraries: libcurl.so.4: cannot open shared object file: No such file or directory

## 编译安装openssl-1.1.1w.tar.gz
---
### 下载
root@master-100:~# wget -c https://www.openssl.org/source/openssl-1.1.1w.tar.gz

### 解压并进入
tar xf openssl-1.1.1w.tar.gz && cd openssl-1.1.1w

### 依赖安装
root@master-100:~/openssl-1.1.1w# apt update && apt upgrade -y && apt install build-essential

### 编译并安装
root@master-100:~/openssl-1.1.1w# ./config --prefix=/usr/local/openssl && make -j$(nproc) && make install
root@master-100:~/openssl-1.1.1w# ls /usr/local/openssl/
bin include lib share ssl

### 加载库配置
root@master-100:~# cat /etc/ld.so.conf.d/x86_64-linux-gnu.conf
# Multiarch support
/usr/local/lib/x86_64-linux-gnu
/lib/x86_64-linux-gnu
/usr/lib/x86_64-linux-gnu
/usr/local/openssl/lib
root@master-100:~# ldconfig
---

## 再次运行,缺少其他依赖库,安装后,再次运行正常
root@master-100:~# mongod --version
mongod: error while loading shared libraries: libcurl.so.4: cannot open shared object file: No such file or directory
root@master-100:~# apt install libcurl4
root@master-100:~# mongod --version
db version v6.0.12
Build Info: {
"version": "6.0.12",
"gitVersion": "21e6e8e11a45dfbdb7ca6cf95fa8c5f859e2b118",
"openSSLVersion": "OpenSSL 1.1.1w 11 Sep 2023",
"modules": [],
"allocator": "tcmalloc",
"environment": {
"distmod": "debian11",
"distarch": "x86_64",
"target_arch": "x86_64"
}
}

多实例配置

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
130
131
132
133
134
135
136
137
138
139
## 创建三份数据目录
root@master-100:/usr/local/mongo# mkdir data/{27017,27018,27019}
root@master-100:/usr/local/mongo# ls data
27017 27018 27019

## 创建配置文件
root@master-100:/usr/local/mongo# cat etc/27017.conf
storage:
dbPath: /usr/local/mongo/data/27017
engine: wiredTiger
wiredTiger:
engineConfig:
cacheSizeGB: 0.3
directoryForIndexes: true
journalCompressor: zstd
collectionConfig:
blockCompressor: zstd
indexConfig:
prefixCompression: true
journal:
enabled: true
systemLog:
destination: file
logAppend: true
path: /usr/local/mongo/log/27017.log
net:
port: 27017
bindIp: 0.0.0.0
processManagement:
timeZoneInfo: /usr/share/zoneinfo
fork: true
pidFilePath: /usr/local/mongo/run/27017.pid
replication:
replSetName: rs-mongodb

## 生成另外两份
root@master-100:/usr/local/mongo/etc# cat 27017.conf | sed 's@27017@27018@g' > 27018.conf
root@master-100:/usr/local/mongo/etc# cat 27017.conf | sed 's@27017@27019@g' > 27019.conf

## 启动脚本
root@master-100:/usr/local/mongo# vim /etc/init.d/mongod_27017
#!/bin/bash

### BEGIN INIT INFO
# Provides: mongod_27017
# Required-Start: $network $local_fs $remote_fs
# Required-Stop: $network $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Should-Start: $syslog $named
# Should-Stop: $syslog $named
# Short-Description: start and stop mongod_27017
# Description: mongod daemon
### END INIT INFO

SER_NAME=mongod_27017
EXEC=/usr/local/mongo/bin/mongod
PIDFILE=/usr/local/mongo/run/27017.pid
CONF=/usr/local/mongo/etc/27017.conf

case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting $SER_NAME server..."
$EXEC --config $CONF
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$EXEC --shutdown --config $CONF
while [ -x /proc/${PID} ]
do
echo "Waiting for $SER_NAME to shutdown ..."
sleep 1
done
echo "$SER_NAME stopped" && rm -f $PIDFILE
fi
;;
status)
if [ -f ${PIDFILE} ]
then
PID=$(cat $PIDFILE)
echo "$SER_NAME is running, process is $PID"
else
echo "$SER_NAME is not running"
fi
;;
restart)
$0 stop
$0 start
;;
reload)
PID=$(cat $PIDFILE)
/bin/kill -1 $PID
;;
*)
echo "Please use start, stop, restart, reload or status as first argument"
;;
esac

## 其余两份
root@master-100:/usr/local/mongo# cat /etc/init.d/mongod_27017 | sed 's@27017@27018@g' > /etc/init.d/mongod_27018
root@master-100:/usr/local/mongo# cat /etc/init.d/mongod_27017 | sed 's@27017@27019@g' > /etc/init.d/mongod_27019

## 启动并检查
root@master-100:/usr/local/mongo# chmod +x /etc/init.d/mongod_2701*
root@master-100:/usr/local/mongo# ls /etc/init.d/mongod_2701*
/etc/init.d/mongod_27017 /etc/init.d/mongod_27018 /etc/init.d/mongod_27019
root@master-100:/usr/local/mongo# systemctl daemon-reload
root@master-100:/usr/local/mongo# service mongod_27017 start
root@master-100:/usr/local/mongo# service mongod_27018 start
root@master-100:/usr/local/mongo# service mongod_27019 start
root@master-100:/usr/local/mongo# /etc/init.d/mongod_27017 status
mongod_27017 is running, process is 13431
root@master-100:/usr/local/mongo# /etc/init.d/mongod_27018 status
mongod_27018 is running, process is 13494
root@master-100:/usr/local/mongo# /etc/init.d/mongod_27019 status
mongod_27019 is running, process is 13557
root@master-100:/usr/local/mongo# netstat -lnpt |grep mongo
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 13431/mongod
tcp 0 0 0.0.0.0:27019 0.0.0.0:* LISTEN 13557/mongod
tcp 0 0 0.0.0.0:27018 0.0.0.0:* LISTEN 13494/mongod
root@master-100:/usr/local/mongo# ps -elf |grep mongo
5 S root 13431 1 1 80 0 - 687636 futex_ 14:06 ? 00:00:04 /usr/local/mongo/bin/mongod --config /usr/local/mongo/etc/27017.conf
5 S root 13494 1 1 80 0 - 687636 futex_ 14:06 ? 00:00:04 /usr/local/mongo/bin/mongod --config /usr/local/mongo/etc/27018.conf
5 S root 13557 1 2 80 0 - 687083 futex_ 14:06 ? 00:00:04 /usr/local/mongo/bin/mongod --config /usr/local/mongo/etc/27019.conf
0 S root 13670 625 0 80 0 - 1583 pipe_r 14:10 pts/0 00:00:00 grep --color=auto mongo

# 安装命令客户端MongoDB Shell
root@master-100:~# wget -c https://downloads.mongodb.com/compass/mongodb-mongosh_2.1.1_amd64.deb
root@master-100:~# dpkg -i mongodb-mongosh_2.1.1_amd64.deb

副本集创建

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
## 连接其中一个实例
root@master-100:~# mongosh --port 27017 或 root@master-100:~# mongosh mongodb://127.0.0.1:27017
Current Mongosh Log ID: 658a701121e3220f6524dba8
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.1.1
Using MongoDB: 6.0.12
Using Mongosh: 2.1.1

## 初始化
test> rs.initiate(
... {
... _id: "rs-mongodb",
... version: 1,
... members: [
... { _id: 0, host : "127.0.0.1:27017", priority: 1, votes: 1 },
... { _id: 1, host : "127.0.0.1:27018", priority: 1, votes: 1 },
... { _id: 2, host : "127.0.0.1:27019", priority: 1, votes: 1 }
... ]
... }
... )
{ ok: 1 }

## 查看集群信息
rs-mongodb [direct: primary] test> rs.status()
{
set: 'rs-mongodb',
date: ISODate('2023-12-26T06:28:42.783Z'),
myState: 1,
term: Long('1'),
syncSourceHost: '',
syncSourceId: -1,
heartbeatIntervalMillis: Long('2000'),
......

## replication方式连接
root@master-100:~# mongosh "mongodb://127.0.0.1:27017,127.0.0.1:27018,127.0.0.1:27019?replicaSet=rs-mongodb&readPreference=secondaryPreferred"
Current Mongosh Log ID: 658a7604ad491f8e96942a6d
Connecting to: mongodb://127.0.0.1:27017,127.0.0.1:27018,127.0.0.1:27019/?replicaSet=rs-mongodb&readPreference=secondaryPreferred&serverSelectionTimeoutMS=2000&appName=mongosh+2.1.1
Using MongoDB: 6.0.12
Using Mongosh: 2.1.1

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

------
The server generated these startup warnings when booting
2023-12-26T14:06:09.470+08:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
2023-12-26T14:06:10.080+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2023-12-26T14:06:10.080+08:00: You are running this process as the root user, which is not recommended
2023-12-26T14:06:10.080+08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
2023-12-26T14:06:10.081+08:00: Soft rlimits for open file descriptors too low
------

rs-mongodb [primary] test>

集群配置参数修改

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
## 修改参数
rs-mongodb [direct: primary] test> cfg = rs.conf();
rs-mongodb [direct: primary] test> cfg.members[0].priority = 10;
10
rs-mongodb [direct: primary] test> rs.reconfig(cfg);
{
ok: 1,
'$clusterTime': {
clusterTime: Timestamp({ t: 1703572553, i: 1 }),
signature: {
hash: Binary.createFromBase64('AAAAAAAAAAAAAAAAAAAAAAAAAAA=', 0),
keyId: Long('0')
}
},
operationTime: Timestamp({ t: 1703572553, i: 1 })
}

## 查看配置
rs-mongodb [direct: primary] test> rs.conf();
{
_id: 'rs-mongodb',
version: 2,
term: 1,
members: [
{
_id: 0,
host: '127.0.0.1:27017',
arbiterOnly: false,
buildIndexes: true,
hidden: false,
priority: 10,
tags: {},
secondaryDelaySecs: Long('0'),
votes: 1
},
......

主节点转移测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
## 关闭主节点
root@master-100:~# /etc/init.d/mongod_27017 stop

## 发现27018实例自动成为primary主节点
root@master-100:~# mongosh --port 27018
Current Mongosh Log ID: 658a77ccd0fb1d6b075f4b9a
Connecting to: mongodb://127.0.0.1:27018/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.1.1
Using MongoDB: 6.0.12
Using Mongosh: 2.1.1

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

------
The server generated these startup warnings when booting
2023-12-26T14:06:15.060+08:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
2023-12-26T14:06:15.674+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2023-12-26T14:06:15.675+08:00: You are running this process as the root user, which is not recommended
2023-12-26T14:06:15.675+08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
2023-12-26T14:06:15.675+08:00: Soft rlimits for open file descriptors too low
------

rs-mongodb [direct: primary] test>

## 恢复27017后,几秒内27017自动变为primary主节点