MySQL MariaDB

How to Determine Which Port MySQL is Running On

When the MySQL server is configured, the user needs to set the suitable port. Later, a specified port is used to run the MySQL server on the local system. Sometimes, users encounter multiple situations where they want to determine the port number. During that time, multiple SQL queries can be used.

This post will discuss multiple ways to get which port MySQL runs.

How to Find Which Port MySQL is Running?

Multiple commands can be used to get which port MySQL is running on, such as:

  • SHOW variable WHERE variable_name = ‘port’;
  • SELECT @@port;
  • SHOW GLOBAL variable LIKE ‘port’;
  • SHOW variable WHERE variable_name in (‘hostname’,’port’);

Let’s check out the working of all commands one by one!

Initially, connect with the MySQL server by executing the following command:

mysql -u root -p

Run the “SHOW” command with a “WHERE” clause to get the port number:

SHOW variables WHERE variable_name = 'port';

Here:

  • SHOW” command will display all specified records.
  • WHERE” clause is utilized for extracting the required records.
  • variable_name” is the resultant column name.
  • port” is added to get the port number.

As you can see, the running port of the MySQL server is obtained as “3306”:

The “SELECT” command can also be used for determining the running port of the MySQL server:

SELECT @@port;

In the above-stated command, the “@@port” is the system variable:

Another way to get the current MySQL running port number is this:

SHOW GLOBAL variables LIKE 'port';

Here:

  • GLOBAL variable” statement is used to show the global system variable value.
  • LIKE” clause searches for a provided pattern in the table column:

If you want to get the port number along with the hostname, run the below-provided command:

SHOW variables WHERE variable_name in ('hostname','port');

It can be observed that the hostname is “Haier-PC” and the port number is “3306”:

We have provided different ways to find which port MySQL is running on.

Conclusion

To determine which port MySQL is running on, the “SHOW variable WHERE variable_name = ‘port’;” command, the “SELECT @@port;” command, the “SHOW GLOBAL variable LIKE ‘port’;” command, and the “SHOW variable WHERE variable_name in (‘hostname’,’port’);” command can be used. This post demonstrated several ways to get which port MySQL is running on.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.