Linux/Debian/Ubuntu 安装 MongoDB

准备工作

导入包管理系统使用的公钥

在终端上,发出以下命令以从https://www.mongodb.org/static/pgp/server-4.2.asc导入MongoDB公共GPG密钥:

root@VM-4-9-debian:~# wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt
-key add -
E: gnupg, gnupg2 and gnupg1 do not seem to be installed, but one of them is required for this
operation

gnupg

提示gnupg没安装,

root@VM-4-9-debian:~# sudo apt-get install gnupg

创建列表文件

root@VM-4-9-debian:~# echo "deb http://repo.mongodb.org/apt/debian buster/mongodb-org/4.2 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list
deb http://repo.mongodb.org/apt/debian buster/mongodb-org/4.2 main
root@VM-4-9-debian:~# sudo apt-get update
Hit:1 http://mirrors.tencentyun.com/debian bookworm InRelease
Get:2 http://mirrors.tencentyun.com/debian bookworm-updates InRelease [52.1 kB]
Get:3 http://mirrors.tencentyun.com/debian-security bookworm-security InRelease [48.0 kB]
Get:4 http://mirrors.tencentyun.com/debian-security bookworm-security/main amd64 Packages [135 kB]
Get:5 http://mirrors.tencentyun.com/debian-security bookworm-security/main Translation-en [80.3 kB]
Ign:6 http://repo.mongodb.org/apt/debian buster/mongodb-org/4.2 InRelease
Get:7 http://repo.mongodb.org/apt/debian buster/mongodb-org/4.2 Release [1,118 B]
Get:8 http://repo.mongodb.org/apt/debian buster/mongodb-org/4.2 Release.gpg [866 B]
Get:9 http://repo.mongodb.org/apt/debian buster/mongodb-org/4.2/main amd64 Packages [24.1 kB]
Fetched 341 kB in 4s (97.1 kB/s)
Reading package lists... Done
W: http://repo.mongodb.org/apt/debian/dists/buster/mongodb-org/4.2/Release.gpg: Key is stored in legacy trusted.gpg keyring (/etc/apt/trusted.gpg), see the DEPRECATION section in apt-key(8) for details.

libssl

  • 下载
root@VM-4-9-debian:~# wget http://archive.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.1f-1ubuntu2_amd64.deb
--2024-02-03 20:40:55-- http://archive.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.1f-1ubuntu2_amd64.deb
Resolving archive.ubuntu.com (archive.ubuntu.com)... 185.125.190.39, 91.189.91.81, 185.125.190.36, ...
Connecting to archive.ubuntu.com (archive.ubuntu.com)|185.125.190.39|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1318204 (1.3M) [application/x-debian-package]
Saving to: ‘libssl1.1_1.1.1f-1ubuntu2_amd64.deb’

libssl1.1_1.1.1f-1ubunt 100%[============================>] 1.26M 410KB/s in 3.1s

2024-02-03 20:40:59 (410 KB/s) - ‘libssl1.1_1.1.1f-1ubuntu2_amd64.deb’ saved [1318204/1318204]
  • 安装
root@VM-4-9-debian:~# sudo dpkg -i libssl1.1_1.1.1f-1ubuntu2_amd64.deb
Selecting previously unselected package libssl1.1:amd64.
(Reading database ... 63879 files and directories currently installed.)
Preparing to unpack libssl1.1_1.1.1f-1ubuntu2_amd64.deb ...
Unpacking libssl1.1:amd64 (1.1.1f-1ubuntu2) ...
Setting up libssl1.1:amd64 (1.1.1f-1ubuntu2) ...
Processing triggers for libc-bin (2.36-9+deb12u3) ...

安装 MongoDB

默认安装的肯定是最新版本的,没必要 …

  • 最新版本
sudo apt-get install -y mongodb-org
  • 指定版本
sudo apt-get install -y mongodb-org=4.2.6 mongodb-org-server=4.2.6 mongodb-org-shell=4.2.6 mongodb-org-mongos=4.2.6 mongodb-org-tools=4.2.6
root@VM-4-9-debian:~# sudo apt-get install -y mongodb-org=4.2.6 mongodb-org-server=4.2.6 mongodb-org-shell=4.2.6 mongodb-org-mongos=4.2.6 mongodb-org-tools=4.2.6
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following package was automatically installed and is no longer required:
linux-headers-6.1.0-9-common
Use 'sudo apt autoremove' to remove it.
The following NEW packages will be installed:
mongodb-org mongodb-org-mongos mongodb-org-server mongodb-org-shell mongodb-org-tools
0 upgraded, 5 newly installed, 0 to remove and 69 not upgraded.
Need to get 97.7 MB of archives.
After this operation, 296 MB of additional disk space will be used.
Get:1 http://repo.mongodb.org/apt/debian buster/mongodb-org/4.2/main amd64 mongodb-org-shell amd64 4.2.6 [12.1 MB]
Get:2 http://repo.mongodb.org/apt/debian buster/mongodb-org/4.2/main amd64 mongodb-org-server amd64 4.2.6 [18.5 MB]
Get:3 http://repo.mongodb.org/apt/debian buster/mongodb-org/4.2/main amd64 mongodb-org-mongos amd64 4.2.6 [10.2 MB]
Get:4 http://repo.mongodb.org/apt/debian buster/mongodb-org/4.2/main amd64 mongodb-org-tools amd64 4.2.6 [57.0 MB]
Get:5 http://repo.mongodb.org/apt/debian buster/mongodb-org/4.2/main amd64 mongodb-org amd64 4.2.6 [3,532 B]
Fetched 97.7 MB in 16s (6,031 kB/s)
Selecting previously unselected package mongodb-org-shell.
(Reading database ... 63889 files and directories currently installed.)
Preparing to unpack .../mongodb-org-shell_4.2.6_amd64.deb ...
Unpacking mongodb-org-shell (4.2.6) ...
Selecting previously unselected package mongodb-org-server.
Preparing to unpack .../mongodb-org-server_4.2.6_amd64.deb ...
Unpacking mongodb-org-server (4.2.6) ...
Selecting previously unselected package mongodb-org-mongos.
Preparing to unpack .../mongodb-org-mongos_4.2.6_amd64.deb ...
Unpacking mongodb-org-mongos (4.2.6) ...
Selecting previously unselected package mongodb-org-tools.
Preparing to unpack .../mongodb-org-tools_4.2.6_amd64.deb ...
Unpacking mongodb-org-tools (4.2.6) ...
Selecting previously unselected package mongodb-org.
Preparing to unpack .../mongodb-org_4.2.6_amd64.deb ...
Unpacking mongodb-org (4.2.6) ...
Setting up mongodb-org-server (4.2.6) ...
Adding system user `mongodb' (UID 103) ...
Adding new user `mongodb' (UID 103) with group `nogroup' ...
Not creating `/nonexistent'.
Adding group `mongodb' (GID 110) ...
Done.
Adding user `mongodb' to group `mongodb' ...
Done.
Setting up mongodb-org-shell (4.2.6) ...
Setting up mongodb-org-tools (4.2.6) ...
Setting up mongodb-org-mongos (4.2.6) ...
Setting up mongodb-org (4.2.6) ...
Processing triggers for man-db (2.11.2-2) ...

运行 MongoDB

路径

  • 数据文件:/var/lib/mongodb
  • 日志文件: /var/log/mongodb
  • 配置文件:/etc/mongod.conf

常用命令

  • 启动
sudo systemctl start mongod
  • 验证启动成功
root@VM-4-9-debian:~# sudo systemctl status mongod
● mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; disabled; preset: enabled)
Active: active (running) since Sat 2024-02-03 20:43:15 CST; 16s ago
Docs: https://docs.mongodb.org/manual
Main PID: 1804477 (mongod)
Memory: 88.3M
CPU: 669ms
CGroup: /system.slice/mongod.service
└─1804477 /usr/bin/mongod --config /etc/mongod.conf
作者

江北饮马、江南折花

发布于

2024-02-03

更新于

2024-02-12

许可协议

评论