Which processes have connections to my database?

Quick Linux Tip #26:

Try: sudo lsof -iTCP:3306 -sTCP:ESTABLISHED

Info: lsof -iTCP:PORT lists active network connections on a port. -sTCP:ESTABLISHED filters out listening sockets to show active connections only.

Examples:

  • $ sudo lsof -iTCP:80 -sTCP:LISTEN  # Active listeners on port 80
  • $ sudo lsof -i @192.168.1.100  # Filter by remote IP
  • $ sudo lsof -Pn -iTCP:3306  # Fast mode (no DNS or port resolution)

Note: lsof can be slow on busy servers because it resolves hostnames and port names by default. Add -Pn to disable resolution for instant output. Common states: LISTEN, ESTABLISHED, TIME_WAIT.

Follow @LinuxTeck for more #LinuxTips




LinuxTeck.com
linuxteck@ubuntu:~$ sudo lsof -iTCP:3306 -sTCP:ESTABLISHED
COMMAND   PID    USER       FD   TYPE DEVICE SIZE/OFF NODE NAME
mysqld    2134   mysql      34u  IPv4 1234567 0t0     TCP localhost:mysql->localhost:54321 (ESTABLISHED)
python3   4567   linuxteck  3u   IPv4 1234568 0t0     TCP localhost:54321->localhost:mysql (ESTABLISHED)
java      5678   tomcat     9u   IPv4 1234569 0t0     TCP localhost:54322->localhost:mysql (ESTABLISHED)
linuxteck@ubuntu:~$

PREVIOUS ARTICLE Quick Linux Tip #25: How do I keep /var/www identical on two servers with minimal transfer? NEXT ARTICLE Quick Linux Tip #27: How do I find a complex command I ran in the past without scrolling through history?
About John Britto

John Britto Founder & Chief-Editor @LinuxTeck. A Computer Geek and Linux Intellectual having more than 20+ years of experience in Linux and Open Source technologies.

View all posts by John Britto →

Leave a Reply

Your email address will not be published.