database mongodb 正在运行吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5091624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 08:04:15  来源:igfitidea点击:

Is mongodb running?

mongodbunixdatabasenosql

提问by Tilo

I have installed mongodb and the php drivers on my unix server.

我已经在我的 unix 服务器上安装了 mongodb 和 php 驱动程序。

My question is how can I tell if mongodb is running? Is there a simple command line query to check status? If I start it once from the shell will it keep running if I exit the shell (this doesn't seem to be the case). How can I make the mongodb connection persistent and auto start on server reboot?

我的问题是如何判断 mongodb 是否正在运行?是否有一个简单的命令行查询来检查状态?如果我从 shell 启动它一次,如果我退出 shell,它会继续运行(这似乎不是这种情况)。如何使 mongodb 连接持久化并在服务器重启时自动启动?

I can run:

我可以跑:

-bash-3.2$ su
Password:
[root@xxx]# cd /var/lib
[root@xxx]# ./mongodb-linux-i686-1.6.5/bin/mongod
./mongodb-linux-i686-1.6.5/bin/mongod --help for help and startup options
Wed Feb 23 08:06:54 MongoDB starting : pid=7271 port=27017 dbpath=/data/db/ 32-bit

** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data
** see http://blog.mongodb.org/post/137788967/32-bit-limitations

** WARNING: You are running in OpenVZ. This is known to be broken!!!

Wed Feb 23 08:06:54 db version v1.6.5, pdfile version 4.5
Wed Feb 23 08:06:54 git version: 0eb017e9b2828155a67c5612183337b89e12e291
Wed Feb 23 08:06:54 sys info: Linux domU-12-31-39-01-70-B4 2.6.21.7-2.fc8xen #1 SMP Fri
Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_37
Wed Feb 23 08:06:54 [initandlisten] waiting for connections on port 27017
Wed Feb 23 08:06:54 [websvr] web admin interface listening on port 28017

-bash-3.2$ su
密码:
[root@xxx]# cd /var/lib
[root@xxx]# ./mongodb-linux-i686-1.6.5/bin/mongod
./mongodb-linux-i686-1.6. 5/bin/mongod --help 帮助和启动选项
Wed Feb 23 08:06:54 MongoDB 开始:pid=7271 port=27017 dbpath=/data/db/ 32-bit

** 注意:使用 32 位 MongoDB 时,您的数据限制为大约 2 GB
** 请参阅http://blog.mongodb.org/post/137788967/32-bit-limitations

** 警告:您正在 OpenVZ 中运行。这个被破解了!!!

2 月 23 日星期三 08:06:54 db 版本 v1.6.5,pdfile 版本 4.5
星期三 2 月 23 日 08:06:54 git 版本:0eb017e9b2828155a67c5612183337b89e12e291
星期三 8 月 10 日 - 203 年 2 月 3 日 - Linux 系统 - 2019 年-70-B4 2.6.21.7-2.fc8xen #1 SMP Fri
Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_37
Wed Feb 23 08:06:54 [initandlisten] 等待端口 2008
:20:20 上的连接06:54 [websvr] Web 管理界面监听端口 28017

If I open a seperate shell I can then then connect to mongodb:

如果我打开一个单独的外壳,然后我可以连接到 mongodb:

-bash-3.2$ cd /var/lib
-bash-3.2$ ./mongodb-linux-i686-1.6.5/bin/mongo
MongoDB shell version: 1.6.5
connecting to: test
db.foo.find()
{ "_id" : ObjectId("4d63d7d3eb95985ab19c8feb"), "a" : 1 }

-bash-3.2$ cd /var/lib
-bash-3.2$ ./mongodb-linux-i686-1.6.5/bin/mongo
MongoDB shell 版本:1.6.5
连接到:test
db.foo.find()
{ " _id": ObjectId("4d63d7d3eb95985ab19c8feb"), "a": 1 }

However if I close the initial shell I can't connect:

但是,如果我关闭初始外壳,则无法连接:

-bash-3.2$ cd /var/lib
-bash-3.2$ ./mongodb-linux-i686-1.6.5/bin/mongo
MongoDB shell version: 1.6.5
connecting to: test
Wed Feb 23 08:25:10 Error: couldn't connect to server 127.0.0.1 (anon):1154
exception: connect failed

-bash-3.2$ cd /var/lib
-bash-3.2$ ./mongodb-linux-i686-1.6.5/bin/mongo
MongoDB shell 版本:1.6.5
连接到:测试
Wed Feb 23 08:25:10 错误:无法连接到服务器 127.0.0.1(匿名):1154
异常:连接失败

回答by Tilo

check with either:

检查:

   ps -edaf | grep mongo | grep -v grep  # "ps" flags may differ on your OS

or

或者

   /etc/init.d/mongodb status     # for MongoDB version < 2.6

   /etc/init.d/mongod status      # for MongoDB version >= 2.6

or

或者

   service mongod status

to see if mongod is running (you need to be root to do this, or prefix everything with sudo). Please note that the 'grep' command will always also show up as a separate process.

查看 mongod 是否正在运行(您需要是 root 才能执行此操作,或者在所有内容前加上sudo)。请注意,'grep' 命令也将始终显示为一个单独的进程。

check the log file /var/log/mongo/mongo.log to see if there are any problems reported

检查日志文件 /var/log/mongo/mongo.log 看看是否有任何报告的问题

回答by Mark Gia Bao Nguyen

I find:

我发现:

ps -ax | grep mongo

To be a lot more consistent. The value returned can be used to detect how many instances of mongod there are running

要更加一致。返回的值可用于检测有多少 mongod 实例正在运行

回答by PiyushW

For quickly checking if mongodb is running, this quick nc trick will let you know.

为了快速检查 mongodb 是否正在运行,这个快速的 nc 技巧会让你知道。

nc -zvv localhost 27017

The above command assumes that you are running it on the default port on localhost.

上面的命令假设您在 localhost 的默认端口上运行它。

For auto-starting it, you might want to look at this thread.

要自动启动它,您可能需要查看此线程

回答by Vinu Joseph

this should work fine...

这应该可以正常工作...

pgrep mongod

pgrep mongod

回答by magma

To check current running status of mongodb use: sudo service mongodb status

要检查 mongodb 的当前运行状态,请使用: sudo service mongodb status

回答by AdaTheDev

Correct, closing the shell will stop MongoDB. Try using the --forkcommand line arg for the mongod process which makes it run as a daemon instead. I'm no Unix guru, but I'm sure there must be a way to then get it to auto start when the machine boots up.

正确,关闭 shell 将停止 MongoDB。尝试将--fork命令行 arg 用于 mongod 进程,使其作为守护进程运行。我不是 Unix 专家,但我确信一定有办法让它在机器启动时自动启动。

e.g.

例如

mongod --fork --logpath /var/log/mongodb.log --logappend

Check out the full documentation on Starting and Stopping Mongo.

查看有关启动和停止 Mongo的完整文档。

回答by Suneth

You can use the below command, to check MongoDB status, e.g: sudo service MongoDB statuswhich displays the status of MongoDB service as like the screenshot:

您可以使用以下命令来检查 MongoDB 状态,例如:sudo service MongoDB status它显示 MongoDB 服务的状态,如屏幕截图所示:

MongoDB status

MongoDB 状态

回答by G Kiran Kumar Reddy

Probably because I didn't shut down my dev server properly or a similar reason. To fix it, remove the lock and start the server with: sudo rm /var/lib/mongodb/mongod.lock ; sudo start mongodb

可能是因为我没有正确关闭我的开发服务器或类似的原因。要修复它,请移除锁并使用以下命令启动服务器: sudo rm /var/lib/mongodb/mongod.lock ; sudo start mongodb

回答by B T

I know this is for php, but I got here looking for a solution for node. Using mongoskin:

我知道这是针对 php 的,但我来这里是为了寻找 node.js 的解决方案。使用mongoskin:

mongodb.admin().ping(function(err) {
    if(err === null)
        // true - you got a conntion, congratulations
    else if(err.message.indexOf('failed to connect') !== -1)
        // false - database isn't around
    else
        // actual error, do something about it
})

With other drivers, you can attempt to make a connection and if it fails, you know the mongo server's down. Mongoskin needs to actually make some call (like ping) because it connects lazily. For php, you can use the try-to-connect method. Make a script!

使用其他驱动程序,您可以尝试建立连接,如果连接失败,您就知道 mongo 服务器已关闭。Mongoskin 需要实际进行一些调用(如 ping),因为它连接延迟。对于 php,您可以使用 try-to-connect 方法。写个剧本!

PHP:

PHP:

$dbIsRunning = true
try {
  $m = new MongoClient('localhost:27017');
} catch($e) {
  $dbIsRunning = false
}