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

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


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

sudo lsof -iTCP -sTCP:LISTEN | grep mongo

Try this:

db.runCommand({whatsmyuri : 1})

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

Tags:

Mongodb