Mongo守护进程无法通过service mongod start运行。

7
最近我按照这个网页(http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/)安装了mongodb包。我使用docker和ubuntu14.04的镜像来部署我的服务器。
问题出现在首次运行 mongod 服务时:
# service mongod start

我收到了以下内容:

Rather than invoking init scripts through /etc/init.d, use the service(8)
utility, e.g. service mongod restart

Since the script you are attempting to invoke has been converted to an
Upstart job, you may also use the stop(8) and then start(8) utilities,
e.g. stop mongod ; start mongod. The restart(8) utility is also available.

我尝试了这个:

# start mongod

但是没有输出。

接下来我想要查看日志,但是却没有日志!

ls /var/log/mongodb -a #empty

好的,接下来我尝试启动mongo shell:

# mongo
2014-08-12T17:42:44.431+0000 warning: Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2014-08-12T17:42:44.432+0000 Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed at src/mongo/shell/mongo.js:146
exception: connect failed

好的,经过搜索和查看Mongodb在Linux服务器上https://wiki.archlinux.org/index.php/MongoDB(故障排除部分)中提供的所有答案后,我仍然无法解决问题。

只有在后台直接运行mongod时,mongo shell才能工作:

mongod --verbose &

[DataFileSync] BackgroundJob starting: DataFileSync
shardKeyTest passed
isInRangeTest passed
shardObjTest passed
[initandlisten] MongoDB starting : pid=451 port=27017 dbpath=/data/db 64-bit host=a9d816faea4c
[initandlisten] db version v2.6.4
[initandlisten] git version: 3a830be0eb92d772aa855ebb711ac91d658ee910
[initandlisten] build info: Linux build7.nj1.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
[initandlisten] allocator: tcmalloc
[initandlisten] options: { systemLog: { verbosity: 1 } }
[initandlisten] flushing directory /data/db
[initandlisten] journal dir=/data/db/journal
[initandlisten] recover : no journal files present, no recovery needed
[initandlisten] flushing directory /data/db/journal
[initandlisten] flushing directory /data/db/journal
[initandlisten] opening db:  local
[initandlisten] enter repairDatabases (to check pdfile version #)
[initandlisten]     local
[initandlisten] done repairDatabases
[initandlisten] opening db:  admin
[initandlisten] query admin.system.roles planSummary: EOF ntoreturn:0 ntoskip:0 keyUpdates:0 numYields:0 locks(micros) W:119 r:106 nreturned:0 reslen:20 0ms
[ClientCursorMonitor] BackgroundJob starting: ClientCursorMonitor
[PeriodicTaskRunner] BackgroundJob starting: PeriodicTaskRunner
[TTLMonitor] BackgroundJob starting: TTLMonitor
[initandlisten] fd limit hard:1048576 soft:524288 max conn: 419430
[IndexRebuilder] BackgroundJob starting: IndexRebuilder
[IndexRebuilder] opening db:  local
[initandlisten] create collection local.startup_log { size: 10485760, capped: true }
[initandlisten] command local.$cmd command: create { create: "startup_log", size: 10485760, capped: true } ntoreturn:1 keyUpdates:0 numYields:0  reslen:75 0ms
[initandlisten] insert local.startup_log ninserted:1 keyUpdates:0 numYields:0  0ms
[initandlisten] waiting for connections on port 27017
[IndexRebuilder] checking complete2014-08-12T17:48:29.837+0000 [DataFileSync] BackgroundJob starting: DataFileSync

现在我有以下内容:

/var/lib/mongodb (mongodb:mongodb) empty
/var/log/mongodb (mongodb:nogroup) empty
/data/db (mongo:nogroup) #useless

# mongod.conf
dbpath=/var/lib/mongodb
logpath=/var/log/mongodb/mongod.log
logappend=true
port = 27017
bind_ip = 0.0.0.0
...

这里发生了什么事?我完全不明白。

1
a. 你检查过在 /etc/init.d/ 下是否安装了任何内容吗? b. 如果你在后台启动 mongod 而没有告诉它查看你的配置文件,它将使用默认目录,而不是你指定的目录。要手动启动,请尝试这样做:mongod --config /your/path/to/mongod.conf - rhealitycheck
我得出结论,最好像你注意到的那样手动启动mongod:mongod --config /your/path/to/mongod.conf。目前一切正常(暂时如此...)这么做是正确的吗? - Timur Fayzrakhmanov
1
这是一个 init 脚本的示例。请查看 /etc/init.d/mongod 指向的文件是否与此类似:https://gist.github.com/naholyr/4275302(注意,这是针对不同程序的,但语法应该相似)。 - rhealitycheck
说实话,我不明白该怎么做... 我对Linux的知识不是很深(因为我是新手),所以我需要明确的操作步骤。 - Timur Fayzrakhmanov
包括 dpkg -L mongodb-orgdpkg -L mongodb-org-server 的输出信息。 - hbogert
显示剩余6条评论
1个回答

5
Docker容器通常没有完整的init系统,而在docker容器内部与upstart的交互将不起作用。(理论上是可能的,但这违背了拥有轻量级堆栈的目的)。
这意味着当你启动一个docker容器时,它将运行一个单一的命令“/usr/bin/mongod”。
在docker容器中运行mongodb的示例: https://docs.docker.com/samples/library/mongo/ 另外,由于您正在使用交互式docker容器运行安装命令,因此就docker而言,您的shell解释器是单个命令。一旦进入交互会话,您可以在后台运行mongod(正如您所做的那样),并启动mongo客户端会话。
另一种方法是将这些指令作为Dockerfile的一部分运行。您可以参考mongodb example
您可能还想考虑一些已经发布在docker hub上的官方mongo db镜像。

https://registry.hub.docker.com/_/mongo/


1
Docker链接无效。 - mujaffars
谢谢@mujaffars指出这一点。我已经更新了链接。看起来这些示例已经被移动了。 - sukrit007
链接又挂了 @mujaffars。 - Nadjib Mami

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接