Database Ports Explained: MySQL, MSSQL & Exposed Databases (Animated)
Behind almost every website is a database. Apps connect to it over its own protocol: MySQL on 3306, Microsoft SQL Server on 1433, PostgreSQL on 5432, MongoDB on 27017, Redis on 6379. Databases should only ever be reachable by the apps that need them; every year, thousands of breaches come from databases left open to the internet.
1. Connect & log in: the app logs in to the database
The database server greets the client with its version and a random salt; the app answers with its username and a password hash response.
- GREETING (Database server → Web app server): The greeting reveals the exact version, which scanners collect.
- LOGIN (Web app server → Database server): The app logs in with its own limited account.
- OK (Database server → Web app server): Connected.
Wireshark filter:
mysql
2. Run a query: sql in, rows out
Every page view becomes one or more SQL queries.
- QUERY (Web app server → Database server): A parameterised query: user input is never glued into the SQL text.
- RESULT (Database server → Web app server): The rows come back and the page is built.
Wireshark filter:
mysql.query
3. Exposed database: attack: the database answers the whole internet
A firewall mistake or a cloud setting puts port 3306 on the internet. Automated scanners find it within hours and try default and common passwords; once in, they copy or delete the data and often leave a ransom note.
- LOGIN (Attacker → Database server): Automated login attempts arrive from the internet.
- OK (Database server → Attacker): One works.
- QUERY (Attacker → Database server): The data is copied out: a reportable breach.
Wireshark filter:
tcp.dstport == 3306 && !(ip.src == 10.0.1.10)
4. Lock it down: defence: private network, firewall, least privilege
Bind the database to a private address, allow only the app server through the firewall, require TLS, remove default accounts, give the app a least-privilege account, and alert on logins from unknown sources.
- SYN (Attacker → Database server): From outside, the port simply isn’t there.
- QUERY (Web app server → Database server): The app keeps working as before.
My.cnf + firewall:
bind-address = 10.0.1.20 require_secure_transport = ON Allow TCP 3306 only from 10.0.1.10
Practise this in a real lab
Find exposed database ports with Nmap in a lab, review database logins in logs, and harden MySQL and MSSQL configurations.
Explore ER Segment training →FAQ
Which database ports should a SOC know?
MySQL/MariaDB 3306, MSSQL 1433, PostgreSQL 5432, Oracle 1521, MongoDB 27017, Redis 6379, Elasticsearch 9200.
Is it OK if the database has a password?
A password helps, but the database still should not be reachable from the internet at all.
What is least privilege for databases?
The app’s account can only do what the app needs, for example read and write its own tables, never drop the database or read other schemas.