MySQL defaults to port 3306 when installed but sometimes for another reason, it may be listening on a different port. In this post, I list some ways in which you can find out which port your MySQL instance is running on.
Using the MySQL configuration file to determine which port it is running on
If you are running linux, then this is an easy one liner. On my test machine, my config is stored in /etc/mysql:
cat /etc/mysql/my.cnf | grep port
On my test machine this returns
port = 3306
If you are running Windows, then find your my.ini file and open it in a text editor such as notepad. Perform a search for the term – “port”
Using the MySQL client to determine the MySQL port
MySQL can tell you which port it is running on. Log into it and use the “show variables like ‘port{3a76cbff1b5ce5ace3bd34bbdd68f71285d81fb9af60114c7a6db80d3a8688de}'”;
root@ubuntu:~# mysql -uroot -p; mysql> show global variables like 'port{96f5cbaad583fb885ff5d18ecce5734bf1e8596949521e20c290401929518f75}';
+---------------+-------+ | Variable_name | Value | +---------------+-------+ | port | 3306 | +---------------+-------+ 1 row in set (0.00 sec)
If you are using a client such as MySQL Workbench, you use the above syntax or click on “Server Status” over on the left hand side, for example in Windows, it looks like this:
Using the netstat command to check which port MySQL is running on
If you’re wanting to know how to check if mysql is running then there are some useful one line commands that can be run.
This command is very useful as a mysql port check either on a unix or windows operation system – “netstat” which is short for “network statistics”
netstat -tln | grep mysql
In the results below (unix OS), the port is listed in this section as “0 0.0.0.0:3306”
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1115/mysqld
If running on windows, you could do
netstat -anob
It’s a little hard then to find the one which is relevant as the list can be quite large, scrolling past quickly. It’s possible to pipe it to make the output page or dump it to file and then look for mysqld.exe in the list.
Paging example
netstat -anob | more
Dumping to file example
netstat -anob > output.txt
In each case find the mysqld.exe lines where the status is LISTENING
Leave a Reply