How can I see which mongo ports are listening from the mongo shell?

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

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

Which is close ... but no. Suggestions? I read the docs and cannot find any team that will do this.

+73
mongodb
Feb 19 '12 at 3:23
source share
4 answers

In the system shell, you can use lsof (see Derick's answer below) or netstat -an to see what the process actually does. However, assuming you have access to the mongo shell (which implies the name of the question), you can run the serverCmdLineOpts() command. This output will give you all the arguments passed on the command line (argv), and those from the configuration file (parsed), and you can conclude that the mongod ports mongod listened based on this information. Here is 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 did not pass certain port parameters, such as above, then mongod will listen on 27017 and 28017 ( http console ) by default. Note. There are several other arguments that ports can change without being explicit, see here:

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

+59
Feb 20 2018-12-12T00:
source share

You can do this from the operating system shell by doing:

 sudo lsof -iTCP -sTCP:LISTEN | grep mongo 
+175
Feb 19 '12 at 10:40
source share

Try the following:

 db.runCommand({whatsmyuri : 1}) 

It will display both the IP address and port number.

+20
Dec 07 '15 at 12:20
source share

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

MongoDB supports the getParameter command, but this only works if you are already connected to the database (at this point you already know the port).

+10
Feb 19 '12 at 6:14
source share



All Articles