mongodb 如何从 mongo shell 中查看 mongo 正在侦听哪些端口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9346431/
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-09 12:29:50  来源:igfitidea点击:

how can I see what ports mongo is listening on from mongo shell?

mongodb

提问by jcollum

If I have a mongo instance running, how can I check what port numbers it is listening on from the shell? I thought that db.serverStatus()would do it but I don't see it. I see this

如果我有一个 mongo 实例正在运行,我如何从 shell 中检查它正在侦听的端口号?我以为db.serverStatus()会这样做,但我没有看到。我看到这个

"connections" : {
    "current" : 3,
    "available" : 816

Which is close... but no. Suggestions? I've read the docs and can't seem to find any command that will do this.

这很接近......但没有。建议?我已经阅读了文档,但似乎找不到任何可以执行此操作的命令。

采纳答案by Adam Comerford

From the system shell you can use lsof(see Derick's answer below) or netstat -anto view what a process is actually doing. However, assuming you only have access to the mongoshell (which your question title implies), then you can run the serverCmdLineOpts()command. That output will give you all the arguments passed on the command line (argv) and the ones from the config file (parsed) and you can infer the ports mongodis listening based on that information. Here's an example:

您可以从系统外壳使用lsof(请参阅下面的德里克的回答)或netstat -an查看进程实际在做什么。但是,假设您只能访问mongoshell(您的问题标题暗示了这一点),那么您可以运行该serverCmdLineOpts()命令。该输出将为您提供在命令行 (argv) 上传递的所有参数以及来自配置文件的参数(已解析),您可以mongod根据该信息推断端口正在侦听。下面是一个例子:

db.serverCmdLineOpts()
{
    "argv" : [
        "./mongod",
        "-replSet",
        "test",
        "--rest",
        "--dbpath",
        "/data/test/r1",
        "--port",
        "30001"
    ],
    "parsed" : {
        "dbpath" : "/data/test/r1",
        "port" : 30001,
        "replSet" : "test",
        "rest" : true
    },
    "ok" : 1
}

If you have not passed specific port options like the ones above, then the mongodwill be listening on 27017 and 28017 (http console) by default. Note: there are a couple of other arguments that can alter ports without being explicit, see here:

如果您没有像上面那样传递特定的端口选项,那么默认情况下mongod将侦听 27017 和 28017(http 控制台)。注意:还有一些其他参数可以在不明确的情况下更改端口,请参见此处:

https://docs.mongodb.org/manual/reference/configuration-options/#sharding.clusterRole

https://docs.mongodb.org/manual/reference/configuration-options/#sharding.clusterRole

回答by Derick

You can do this from the Operating System shell by running:

您可以通过运行以下命令从操作系统 shell 执行此操作:

sudo lsof -iTCP -sTCP:LISTEN | grep mongo

回答by Ganu

Try this:

尝试这个:

db.runCommand({whatsmyuri : 1})

It will display both the IP address and the port number.

它将显示 IP 地址和端口号。

回答by Gates VP

MongoDB only listens on one port by default (27017). If the --restinterface is active, port 28017 (27017+1000) will also be open handling web requests for details.

MongoDB 默认只监听一个端口 (27017)。如果--rest接口处于活动状态,端口 28017 (27017+1000) 也将打开处理 Web 请求以获取详细信息。

MongoDB supports a getParametercommand, but that only works if you're already connected to the Database (at which point you already know the port).

MongoDB 支持getParameter命令,但只有在您已经连接到数据库时才有效(此时您已经知道端口)。

回答by nixxo_raa

Try the followed command, this one work for me:

尝试以下命令,这对我有用:

sudo lsof -iTCP -sTCP:LISTEN | grep mongo