在 Linux Ubuntu 20.04 上安装 MongoDB 时遇到问题

在 Linux Ubuntu 20.04 上安装 MongoDB 时遇到问题

其他人也问过这个问题,但我没有找到适合我的答案。我正在尝试在 Ubuntu 20.04 上安装 MongoDB。我一直在遵循以下指南 https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/,但在安装过程中遇到了麻烦。安装过程中出现错误(下面第一个错误),然后我使用 --version 命令验证安装是否成功,似乎已安装,我尝试运行,但出现后续失败(未找到 mongod.service)。

Errors were encountered while processing:
 /tmp/apt-dpkg-install-kD4T9j/0-mongodb-database-tools_100.5.0_amd64.deb
 /tmp/apt-dpkg-install-kD4T9j/3-mongodb-org-server_5.0.2_amd64.deb
 /tmp/apt-dpkg-install-kD4T9j/4-mongodb-org-mongos_5.0.2_amd64.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)
jared@Pandora:~$ mongo --version
MongoDB shell version v5.0.2
Build Info: {
    "version": "5.0.2",
    "gitVersion": "6d9ec525e78465dcecadcff99cce953d380fedc8",
    "openSSLVersion": "OpenSSL 1.1.1f  31 Mar 2020",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "ubuntu2004",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}
jared@Pandora:~$ sudo systemctl start mongod
Failed to start mongod.service: Unit mongod.service not found.
jared@Pandora:~$ sudo systemctl daemon-reload
jared@Pandora:~$ sudo systemctl start mongod
Failed to start mongod.service: Unit mongod.service not found.

然后我再次尝试仔细检查以删除以前的文件,如 Cool Kev 的以下建议所示。以下是完整的新安装和错误打印输出:

jared@Pandora:~$ sudo apt install mongodb-org
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  mongodb-database-tools mongodb-mongosh mongodb-org-database
  mongodb-org-database-tools-extra mongodb-org-mongos mongodb-org-server
  mongodb-org-shell mongodb-org-tools
The following NEW packages will be installed:
  mongodb-database-tools mongodb-mongosh mongodb-org mongodb-org-database
  mongodb-org-database-tools-extra mongodb-org-mongos mongodb-org-server
  mongodb-org-shell mongodb-org-tools
0 upgraded, 9 newly installed, 0 to remove and 23 not upgraded.
Need to get 0 B/147 MB of archives.
After this operation, 449 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
(Reading database ... 197683 files and directories currently installed.)
Preparing to unpack .../0-mongodb-database-tools_100.5.0_amd64.deb ...
Unpacking mongodb-database-tools (100.5.0) ...
Selecting previously unselected package mongodb-mongosh.
Preparing to unpack .../1-mongodb-mongosh_1.0.6_amd64.deb ...
Unpacking mongodb-mongosh (1.0.6) ...
Selecting previously unselected package mongodb-org-shell.
Preparing to unpack .../2-mongodb-org-shell_5.0.2_amd64.deb ...
Unpacking mongodb-org-shell (5.0.2) ...
Preparing to unpack .../3-mongodb-org-server_5.0.2_amd64.deb ...
Unpacking mongodb-org-server (5.0.2) ...
dpkg: error processing archive /tmp/apt-dpkg-install-h1NnWS/3-mongodb-org-server
_5.0.2_amd64.deb (--unpack):
 trying to overwrite '/usr/bin/mongod', which is also in package mongodb-server-
core 1:3.6.9+really3.6.8+90~g8e540c0b6d-0ubuntu5.2
dpkg-deb: error: paste subprocess was killed by signal (Broken pipe)
Preparing to unpack .../4-mongodb-org-mongos_5.0.2_amd64.deb ...
Unpacking mongodb-org-mongos (5.0.2) ...
dpkg: error processing archive /tmp/apt-dpkg-install-h1NnWS/4-mongodb-org-mongos
_5.0.2_amd64.deb (--unpack):
 trying to overwrite '/usr/bin/mongos', which is also in package mongodb-server-
core 1:3.6.9+really3.6.8+90~g8e540c0b6d-0ubuntu5.2
dpkg-deb: error: paste subprocess was killed by signal (Broken pipe)
Selecting previously unselected package mongodb-org-database-tools-extra.
Preparing to unpack .../5-mongodb-org-database-tools-extra_5.0.2_amd64.deb ...
Unpacking mongodb-org-database-tools-extra (5.0.2) ...
Selecting previously unselected package mongodb-org-database.
Preparing to unpack .../6-mongodb-org-database_5.0.2_amd64.deb ...
Unpacking mongodb-org-database (5.0.2) ...
Selecting previously unselected package mongodb-org-tools.
Preparing to unpack .../7-mongodb-org-tools_5.0.2_amd64.deb ...
Unpacking mongodb-org-tools (5.0.2) ...
Selecting previously unselected package mongodb-org.
Preparing to unpack .../8-mongodb-org_5.0.2_amd64.deb ...
Unpacking mongodb-org (5.0.2) ...
Errors were encountered while processing:
 /tmp/apt-dpkg-install-h1NnWS/3-mongodb-org-server_5.0.2_amd64.deb
 /tmp/apt-dpkg-install-h1NnWS/4-mongodb-org-mongos_5.0.2_amd64.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)
jared@Pandora:~$ mongo --version
MongoDB shell version v5.0.2
Build Info: {
    "version": "5.0.2",
    "gitVersion": "6d9ec525e78465dcecadcff99cce953d380fedc8",
    "openSSLVersion": "OpenSSL 1.1.1f  31 Mar 2020",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "ubuntu2004",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}
jared@Pandora:~$ mongod
2021-09-15T21:55:30.223-0400 I CONTROL  [initandlisten] MongoDB starting : pid=25491 port=27017 dbpath=/data/db 64-bit host=Pandora
2021-09-15T21:55:30.223-0400 I CONTROL  [initandlisten] db version v3.6.8
2021-09-15T21:55:30.223-0400 I CONTROL  [initandlisten] git version: 8e540c0b6db93ce994cc548f000900bdc740f80a
2021-09-15T21:55:30.223-0400 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.1.1f  31 Mar 2020
2021-09-15T21:55:30.223-0400 I CONTROL  [initandlisten] allocator: tcmalloc
2021-09-15T21:55:30.223-0400 I CONTROL  [initandlisten] modules: none
2021-09-15T21:55:30.223-0400 I CONTROL  [initandlisten] build environment:
2021-09-15T21:55:30.223-0400 I CONTROL  [initandlisten]     distarch: x86_64
2021-09-15T21:55:30.223-0400 I CONTROL  [initandlisten]     target_arch: x86_64
2021-09-15T21:55:30.223-0400 I CONTROL  [initandlisten] options: {}
2021-09-15T21:55:30.223-0400 I STORAGE  [initandlisten] exception in initAndListen: NonExistentPath: Data directory /data/db not found., terminating
2021-09-15T21:55:30.223-0400 I CONTROL  [initandlisten] now exiting
2021-09-15T21:55:30.223-0400 I CONTROL  [initandlisten] shutting down with code:100
jared@Pandora:~$ mongo
MongoDB shell version v5.0.2
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:372:17
@(connect):2:6
exception: connect failed
exiting with code 1
jared@Pandora:~$ 

答案1

只是为了确认一下,在安装 mongodb.org 存储库中正确添加的软件包之前,您是否已经安装了 Ubuntu 提供的 MongoDB 软件包?这也许不言而喻,但存在 pkg 冲突。如果您不确定是否存在预先存在的版本,您可以随时:

sudo apt remove mongodb-org*
sudo apt remove mongodb
sudo apt autoremove

假设没有问题,重新尝试安装 mongo.org 版本

sudo apt install mongodb-org

答案2

解决了!看起来像:

sudo apt-get purge mongodb-*

能够真正清除系统中的所有旧 mongo 文件,让我可以全新安装。感谢所有提供帮助的人!

相关内容