7

I know that default mongo port is 27017 and I can connect to this instance on such port.

But I tried to figure it out by other means.

I found that mongo is running by ps aux | grep mongo with it I can see /usr/bin/mongod --config /etc/mongod.conf

At /etc/mongod.conf port is 27018

Netstat netstat -tulnp | grep 2701 gives me

tcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN      -               
tcp        0      0 0.0.0.0:27018           0.0.0.0:*               LISTEN      -

I don't know why I don't see name of the process. Is there any other way to be sure that mongo works on 27017?

  • You can launch gnome system monitor and see what is the pid (say 12345) of mongo. Then, you can use this command: ps -aux | grep 12345 – Raphael Nov 10 '15 at 19:04
  • Did you run netstat as root? – muru Nov 10 '15 at 19:06
  • @muru no, should it be root? – Viacheslav Kondratiuk Nov 10 '15 at 19:07
  • @viakondratiuk if you want to see the processes, yes. It will warn you: Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all. – muru Nov 10 '15 at 19:08
8
lsof -i | grep mongo

This will list all open files (lsof) that are listening on "Internet addresses" (-i). The output of this is piped to grep, which is used to filter by text. In this case, we're looking for all files open, related to the Internet, that match the word mongo.

| improve this answer | |
  • That will work only if there is already a client accessing to the database. I may suggest start "mongostat" for example on a different terminal and let it run while executing lsof -n -i | grep mongo (-n is faster because it does not try to resolve names) – Fran Marzoa Feb 27 '18 at 18:27
4

Actually you can use:

lsof -i -ac mongod

lsof can do more than just look up network connections. the "-a" is for "And" so it will have to match both criteria. Otherwise it defaults to "Or". The "-c" is for the command. You can also specify user name or PID or lots of other stuff.

| improve this answer | |
  • 1
    Worked like a charm. Thanks! – Xonshiz Jul 5 '19 at 8:27
3

You could use nmap to scan ports in range from 1 to 65535 with -p- flag

For instance,

Downloads:$ nmap -p- localhost

Starting Nmap 6.40 ( http://nmap.org ) at 2015-11-10 12:07 MST
Nmap scan report for localhost (127.0.0.1)
Host is up (0.00036s latency).
Not shown: 65529 closed ports
PORT      STATE SERVICE
21/tcp    open  ftp
22/tcp    open  ssh
25/tcp    open  smtp
631/tcp   open  ipp
37818/tcp open  unknown
46400/tcp open  unknown
| improve this answer | |
  • 2
    nmap -p 27017 127.0.0.1 – earthmeLon Nov 10 '15 at 20:38
  • @earthmeLon why 27017 ? – Sergiy Kolodyazhnyy Nov 10 '15 at 20:41
  • 2
    Just providing a complete example. I interpreted he doesn't want to scan all ports. Just a comment in case he didn't know. – earthmeLon Nov 10 '15 at 23:47
1

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. That output will give you all the arguments passed on the command line (argv) to the server 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:

{
        "argv" : [
               "/usr/bin/mongod",
               "--config",
               "/etc/mongod.conf",
               "--fork"
        ],
        "parsed" : {
               "bind_ip" : "127.0.0.1",
               "config" : "/etc/mongodb/mongodb.conf",
               "dbpath" : "/srv/mongodb",
               "fork" : true,
               "logappend" : "true",
               "logpath" : "/var/log/mongodb/mongod.log",
               "quiet" : "true",
               "port" : 30001
        },
        "ok" : 1
}

If you have not passed specific port options like the one above, then the mongod will be listening on 27017 (normal) and 28017 (http status interface) 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

I mention this because your lsof output also shows port 27018 open, which is the default if you have the shardsvr option set. Hence, your output suggests that you may have a mongos on 27017 and a (shard) mongod on 27018. If you connect to each and run the getCmdLineOpts() you should be able to figure out how things are configured.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.