Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

mongodb

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

"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.

like image 936
jcollum Avatar asked Feb 19 '12 03:02

jcollum


People also ask

What port is my MongoDB listening on?

By default, MongoDB starts at port 27017. But you can access it in a web browser not at that port, rather, at a port number 1000 more than the port at which MongoDB is started. So if you point your browser to http://localhost:28017, you can see MongoDB web interface.

How do I view collections in mongo shell?

To list all collections in Mongo shell, you can use the function getCollectionNames().

Which command is used to see a connection in MongoDB?

The command connPoolStats returns information regarding the open outgoing connections from the current database instance to other members of the sharded cluster or replica set.

How to find out what ports mongod is listening on?

However, assuming you only have access to the mongo shell (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 mongod is listening based on that information.

How can I see what a process is doing in Mongo?

From the system shell you can use lsof or netstat -an as you mentioned to view what a process is doing in terms of open ports. As an alternative you can run the db.getCmdLineOpts () command from the mongo shell.

Where can I find information on the new MongoDB shell (mongosh)?

For information on the new MongoDB Shell ( mongosh) refer to the mongosh documentation. To understand the differences between the two shells, see Comparison of the mongo Shell and mongosh. In addition to the documentation in the MongoDB Manual, the mongo shell provides some additional information in its "online" help system.

Can I run Mongo on a docker container?

this one worked for me, running mongo on a docker container. MongoDB only listens on one port by default (27017). If the --rest interface is active, port 28017 (27017+1000) will also be open handling web requests for details.


4 Answers

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

sudo lsof -iTCP -sTCP:LISTEN | grep mongo
like image 69
Derick Avatar answered Oct 19 '22 21:10

Derick


From the system shell you can use lsof (see Derick's answer below) or netstat -an to view what a process is actually doing. However, assuming you only have access to the mongo shell (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 mongod is listening based on that information. Here's an example:

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 mongod will 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:

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

like image 37
Adam Comerford Avatar answered Oct 19 '22 21:10

Adam Comerford


Try this:

db.runCommand({whatsmyuri : 1})

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

like image 43
Ganu Avatar answered Oct 19 '22 19:10

Ganu


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

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

like image 13
Gates VP Avatar answered Oct 19 '22 19:10

Gates VP