MySQL is very popular database in the opensource community. While developing applications or connecting MySQL database we need to specify the MySQL port implicitly or explicitly. In this tutorial we will learn MySQL default port number and alternative port numbers.
TCP 3306
The default port for the MySQL service is TCP 3306. This port can be also used for MariaDB database server.
List with netstat Command
We can use netstat
command in order to list MySQL/MariaDB database server port number.
$ sudo netstat -p -l -n | grep mysql

We can see from output that port 3306
port is listening on the local interface 127.0.0.1
For MariaDB change grep mysql
with grep mariadb
Find From DB Config File
What if MySQL server default port is different from 3306
. We have some options. One of them is looking to the application database config file. We can get the port number from there. In this example the port number 3307
is specified explicitly.
$link = mysql_connect('poftut.com:3307', 'mysql_user', 'mysql_password');
Find By Nmap Scan
If we had no clue about the port number we can use some security network scanning tool named nmap
. We can scan popular or all port on the host and find MySQL service. Following command will scan top used 1000 ports.
$ nmap -sC -sV 192.168.1.10
OR we can scan all ports of the given host.
$ nmap -p- -sC -sV 192.168.1.10
Find with SQL Query
Configuration parameter of the MySQL or MariaDB Database server is stored in the variables too. We can use some SQL inorder to print the port information from environment variables like below.
SHOW VARIABLES WHERE Variable_name = 'port';

We can see that port
variable is current set to 3306
.