Mongo Db 启动错误(每次都是)

Mongo Db 启动错误(每次都是)

我无法启动 mongo db。每次都出现相同的错误。

2017-07-19T07:39:16.964-0700 I CONTROL  [initandlisten] MongoDB starting : pid=80283 port=27017 dbpath=/data/db 64-bit host=ubuntu
2017-07-19T07:39:16.964-0700 I CONTROL  [initandlisten] db version v3.4.6
2017-07-19T07:39:16.964-0700 I CONTROL  [initandlisten] git version: c55eb86ef46ee7aede3b1e2a5d184a7df4bfb5b5
2017-07-19T07:39:16.964-0700 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016
2017-07-19T07:39:16.964-0700 I CONTROL  [initandlisten] allocator: tcmalloc
2017-07-19T07:39:16.964-0700 I CONTROL  [initandlisten] modules: none
2017-07-19T07:39:16.964-0700 I CONTROL  [initandlisten] build environment:
2017-07-19T07:39:16.964-0700 I CONTROL  [initandlisten]     distmod: ubuntu1604
2017-07-19T07:39:16.964-0700 I CONTROL  [initandlisten]     distarch: x86_64
2017-07-19T07:39:16.964-0700 I CONTROL  [initandlisten]     target_arch: x86_64
2017-07-19T07:39:16.964-0700 I CONTROL  [initandlisten] options: {}
2017-07-19T07:39:16.964-0700 I STORAGE  [initandlisten] exception in initAndListen: 98 Unable to lock file: /data/db/mongod.lock Resource temporarily unavailable. Is a mongod instance already running?, terminating
2017-07-19T07:39:16.964-0700 I NETWORK  [initandlisten] shutdown: going to close listening sockets...
2017-07-19T07:39:16.964-0700 I NETWORK  [initandlisten] shutdown: going to flush diaglog...
2017-07-19T07:39:16.965-0700 I CONTROL  [initandlisten] now exiting
2017-07-19T07:39:16.965-0700 I CONTROL  [initandlisten] shutting down with code:100

或者有时端口使用错误。

fogserver@iotlab-FogServer:~$ mongod
2017-07-19T17:24:01.299+0200 I CONTROL  [initandlisten] MongoDB starting : pid=21462 port=27017 dbpath=/data/db 64-bit host=iotlab-FogServer
2017-07-19T17:24:01.299+0200 I CONTROL  [initandlisten] db version v3.4.6
2017-07-19T17:24:01.299+0200 I CONTROL  [initandlisten] git version: c55eb86ef46ee7aede3b1e2a5d184a7df4bfb5b5
2017-07-19T17:24:01.299+0200 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016
2017-07-19T17:24:01.299+0200 I CONTROL  [initandlisten] allocator: tcmalloc
2017-07-19T17:24:01.299+0200 I CONTROL  [initandlisten] modules: none
2017-07-19T17:24:01.299+0200 I CONTROL  [initandlisten] build environment:
2017-07-19T17:24:01.299+0200 I CONTROL  [initandlisten]     distmod: ubuntu1604
2017-07-19T17:24:01.299+0200 I CONTROL  [initandlisten]     distarch: x86_64
2017-07-19T17:24:01.299+0200 I CONTROL  [initandlisten]     target_arch: x86_64
2017-07-19T17:24:01.299+0200 I CONTROL  [initandlisten] options: {}
2017-07-19T17:24:01.317+0200 E NETWORK  [initandlisten] listen(): bind() failed Address already in use for socket: 0.0.0.0:27017
2017-07-19T17:24:01.317+0200 E NETWORK  [initandlisten]   addr already in use
2017-07-19T17:24:01.317+0200 E NETWORK  [initandlisten] Failed to set up sockets during startup.
2017-07-19T17:24:01.317+0200 E STORAGE  [initandlisten] Failed to set up listener: InternalError: Failed to set up sockets
2017-07-19T17:24:01.317+0200 I NETWORK  [initandlisten] shutdown: going to close listening sockets...
2017-07-19T17:24:01.317+0200 I NETWORK  [initandlisten] shutdown: going to flush diaglog...
2017-07-19T17:24:01.317+0200 I CONTROL  [initandlisten] now exiting
2017-07-19T17:24:01.317+0200 I CONTROL  [initandlisten] shutting down with code:48
fogserver@iotlab-FogServer:~$

然后我每次都必须重新启动服务。请建议如何避免这种情况。

答案1

发生这种情况是因为 MongoDB 服务器已经在您的系统上运行,可能作为守护进程服务。

根据您的标签,您正在运行 Ubuntu 16.04,它使用systemd。您可以使用以下命令检查服务的运行状态:

sudo systemctl status mongodb

如果报告显示正在运行,则表示您已经有一个 MongoDB 服务器正在运行 - 请尝试正常连接它而无需手动启动mongod

如果您不想/不需要守护服务在系统启动时启动,您可以用以下命令禁用 MongoDB 运行:

sudo systemctl disable mongodb
sudo systemctl stop mongodb

现在,您可以使用mongod或通过启动/停止守护进程来启动和手动停止 MongoDB:

sudo systemctl start mongodb

我个人会高度建议您继续使用守护进程服务,因为您不必担心它出现故障或类似情况。如果担心安全问题,请在防火墙中阻止 MongoDB 的端口。


如果您需要正在运行的守护服务的日志条目,则可以使用journalctl以下命令:

sudo journalctl -u mongodb

如果您想要实时日志,请将-f命令行标志添加到journalctl命令中。


如果出于某种原因你需要两个都守护进程化的 MongoDB 和按需启动的 MongoDB,您需要更改其中一个实例的绑定信息和数据目录。请参阅这里作为拼图的一部分。

或者,您可以直接在运行守护进程时停止服务。

相关内容