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

Mongodb

Mongodb Problem Overview


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.

Mongodb Solutions


Solution 1 - Mongodb

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

sudo lsof -iTCP -sTCP:LISTEN | grep mongo

Solution 2 - Mongodb

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

Solution 3 - Mongodb

Try this:

db.runCommand({whatsmyuri : 1})

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

Solution 4 - Mongodb

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

Solution 5 - Mongodb

You can try, from the mongo shell:

  • db.getMongo()

> Use this command to test that the mongo shell has a connection to the > proper database instance.

connection to <IP>:<PORT>
  • db.collection.explain()

> For unsharded collections, explain returns the following serverInfo > information for the MongoDB instance:

"serverInfo" : {
   "host" : <string>,
   "port" : <int>,
   "version" : <string>,
   "gitVersion" : <string>
}

Default MongoDB Port

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjcollumView Question on Stackoverflow
Solution 1 - MongodbDerickView Answer on Stackoverflow
Solution 2 - MongodbAdam ComerfordView Answer on Stackoverflow
Solution 3 - MongodbGanuView Answer on Stackoverflow
Solution 4 - MongodbGates VPView Answer on Stackoverflow
Solution 5 - MongodbHaniel BaezView Answer on Stackoverflow