Troubleshooting

在本页面

运行 sudo apt-key adv 时出错

在导入安装 MongoDB 社区版过程的程序包 Management 系统步骤所使用的公钥期间,您可能会遇到"gpg: no valid OpenPGP data found."错误。

确保完全按照文档所述复制命令。该操作应以OK响应。

要检查系统上是否存在 MongoDB 公共 GPG 密钥,请在终端中运行以下命令:

sudo apt-key list

输出中应包含类似于以下内容的条目:

pub   rsa4096 2016-12-14 [SC] [expires: 2023-12-09]
      2930 ADAE 8CAF 5059 EE73  BB4B 5871 2A22 91FA 4AD5
uid           [ unknown] MongoDB 3.6 Release Signing Key <packaging@mongodb.com>

运行 sudo apt 更新时出错

安装 MongoDB 社区版过程中运行sudo apt-get update时,您可能会遇到与以下类似的一个或多个错误:

W: GPG error: https://repo.mongodb.org/apt/ubuntu <release>/mongodb-org/3.6 Release: \
   The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 4B7C549A058F8B6B
E: The repository 'https://repo.mongodb.org/apt/ubuntu <release>/mongodb-org/3.6 Release' \
   is not signed.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.

这些错误表明在步骤安装 MongoDB 社区版过程的程序包 Management 系统使用的导入公共密钥中未添加 MongoDB 公共 GPG 密钥。

重复执行安装 MongoDB 社区版过程的程序包 Management 系统步骤中的“导入公共密钥”通常可以解决此问题。确保完全按照文档所述复制命令和键。

您可以通过在终端中运行以下命令来验证系统上是否存在 MongoDB 公共 GPG 密钥:

sudo apt-key list

输出中应包含类似于以下内容的条目:

pub   4096R/91FA4AD5 2016-12-14 [expires: 2023-12-09]
      Key fingerprint = 2930 ADAE 8CAF 5059 EE73  BB4B 5871 2A22 91FA 4AD5
uid                  MongoDB 3.6 Release Signing Key <packaging@mongodb.com>

运行 sudo apt install -y mongodb-org 时出错

在步骤安装 MongoDB 社区版过程的为 MongoDB 创建列表文件中运行sudo apt install -y mongodb-org时,您可能会遇到类似于以下错误:

Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package mongodb-org

此错误表明/etc/apt/sources.list.d/mongodb-org-3.6.list可能配置错误或丢失。

要查看mongodb-org-3.6.list文件的内容,请在终端或 Shell 程序中运行以下命令:

cat /etc/apt/sources.list.d/mongodb-org-3.6.list

如果在上面链接的步骤中文件内容与您的 Ubuntu 版本的文档不完全匹配,请删除该文件,然后重复“为 MongoDB 创建列表文件”步骤。如果文件不存在,请在该步骤中创建它。

验证mongodb-org-3.6.list文件存在并具有正确的内容后,请运行sudo apt update更新apt存储库并重试sudo apt install -y mongodb-org

由于 dpkg-deb 无法安装软件包:错误

安装mongodb-org软件包时,您可能会遇到类似于以下错误:

dpkg: error processing archive /var/cache/apt/archives/mongodb-org-server_3.6.0_amd64.deb (--unpack):
trying to overwrite '/usr/bin/mongod', which is also in package mongodb-server-core 1:3.6.3-0ubuntu1

此错误表明在安装正式的 MongoDB Inc. mongodb-org软件包之前,系统已经安装了 Ubuntu mongodb软件包。要确认主机是否已安装 Ubuntu mongodb软件包,请在终端或 Shell 程序中运行以下命令:

sudo apt list --installed | grep mongo

如果您的输出类似于以下内容,则必须**卸载 Ubuntu mongodb软件包,然后重试安装 MongoDB 社区版过程:

mongodb/trusty,now 1:3.6.3-0ubuntu1 amd64 [installed]
mongodb-clients/trusty,now 1:3.6.3-0ubuntu1 amd64 [installed,automatic]
mongodb-server/trusty,trusty,now 1:3.6.3-0ubuntu1 all [installed,automatic]
mongodb-server-core/trusty,now 1:3.6.3-0ubuntu1 amd64 [installed,automatic]

在终端或 Shell 中运行以下命令,以完全删除 Ubuntu mongodb软件包,然后重试安装过程:

sudo apt remove mongodb
sudo apt purge mongodb
sudo apt autoremove

Note

sudo apt purge mongodb删除 Ubuntu mongodb软件包安装的所有默认配置文件。如果您已经修改了这些配置文件,并且想要保留这些修改,请将文件复制到另一个目录,例如/home/your-user-name

如果您的输出包含mongodb-orgmongodb二进制文件的混合,您可能需要先apt removeapt purgeapt autoremove mongodb-org软件包,然后再尝试删除并清除 Ubuntu mongodb软件包。清除所有与 MongoDB 相关的软件包后,请重试安装过程。

mongod 报告与打开套接字有关的错误

启动mongod时,您可能会遇到Socket is already in useFailed to unlink socket file错误。这些错误通常表示使用为mongod进程配置的端口的另一个进程,通常是系统上正在运行的另一个mongod

以下示例使用ssUtil 列出所有处于LISTEN(-l)状态的打开的 TCP(-t)或 UDP(-u)套接字,以及使用每个套接字(-p)的进程,而无需解析任何服务名或主机名(-n)。

sudo ss --tulpn

以下部分输出显示了mongod进程在27017端口上进行侦听。尝试在同一端口上运行另一个mongod进程将导致套接字错误。

Netid        State        Local Address:Port
udp          UNCONN       127.0.0.53%lo:53        users:(("systemd-resolve",pid=663,fd=12))
udp          UNCONN   10.1.16.87%enp0s3:68        users:(("systemd-network",pid=652,fd=15))
tcp          LISTEN       127.0.0.53%lo:53        users:(("systemd-resolve",pid=663,fd=13))
tcp          LISTEN             0.0.0.0:22        users:(("sshd",pid=819,fd=3))
tcp          LISTEN        192.168.1.15:27017     users:(("mongod",pid=10027,fd=12))
tcp          LISTEN           127.0.0.1:27017     users:(("mongod",pid=10027,fd=11))
tcp          LISTEN                 ::]:22        users:(("sshd",pid=819,fd=4))

mongod 报告与数据目录有关的错误

用户可能会在mongod process log中遇到类似于以下错误:

Data directory ... not found

Attempted to create lock file on a read-only directory: ...

通常,这些错误表明 MongoDB 数据目录不存在,或者mongod无法访问。

数据目录必须存在

数据目录是通过configuration file中的storage.dbPath设置指定的,或在命令行中通过mongod--dbpath选项指定的。

  • 如果您通过apt软件包 Management 器安装了 MongoDB,则默认的/etc/mongod.conf configuration file会将storage.dbPath设置为/var/lib/mongodb

  • 如果您在命令行上运行mongod,并且完全省略了--dbpath选项,则 MongoDB 使用的默认--dbpath/data/db

无论您是使用以上数据目录路径之一,还是在配置文件中或在命令行中提供自己的路径,请在启动mongod之前确保文件系统中存在数据目录。您可以使用mkdir命令在 Ubuntu 上创建目录。

数据目录必须可访问

必须为数据目录配置适当的权限和所有权设置,以允许mongod读取,写入和浏览目录内容(rwx用户或组权限)。

在启动mongod之前,分别使用chownchmod修改权限并设置user:group所有权。

使用数据目录启动 mongod

mongod作为服务启动的用户(例如,使用sudo systemctl start mongodsudo service mongod start)通常只有将/etc/mongod.conf中的默认dbPath值修改为以下内容时才会看到这些错误:

  • 不允许mongodb用户或mongodb组读取,写入或执行(rwx)目录及其内容的目录

  • 不存在的目录。

在不使用服务定义(例如使用终端启动mongod)的情况下启动mongod的用户通常会在以下情况下看到这些错误:

  • 指定为dbPath--dbpath的目录不允许用户或该用户所属的组读取,写入和执行(rwx)目录或其内容,或者

  • 指定的数据目录不存在。