DEV Community

Cover image for Simple Ways to Identify the MySQL Port
Roman Agabekov
Roman Agabekov

Posted on • Originally published at releem.com

Simple Ways to Identify the MySQL Port

For anyone working with MySQL – from seasoned DBAs to developers just dipping their toes into MySQL for the first time – knowing which port your server is running on is a must. The port number is key for establishing database connections, configuring firewalls, and enabling communication between applications and the database. Incorrect port settings can lead to connectivity issues and disrupt the flow of data within your applications.

This tutorial is independent of the operating system you are using, so whether you’re on Windows, macOS, or Linux, the steps outlined here can help you identify your MySQL port settings.

Using the MySQL Command Line (Windows, Linux, MacOS)

The MySQL command line option is universal across all operating systems. Here’s how you can check the port directly from within MySQL.

Open your terminal or command prompt and access your MySQL instance:

mysql -u root -p
Enter fullscreen mode Exit fullscreen mode

Once you're logged in, run the following SQL command:

SHOW VARIABLES LIKE 'port';
Enter fullscreen mode Exit fullscreen mode

OR

show variables where variable_name in ('hostname','port');
Enter fullscreen mode Exit fullscreen mode

After running this command, you’ll see an output that looks something like this:

+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3306 |
+---------------+-------+
1 row in set (0.01 sec)
Enter fullscreen mode Exit fullscreen mode

Checking the MySQL Configuration File (Windows, MacOS, Linux)

If you prefer to check the MySQL configuration file, here's how to do it for each operating system. The process involves locating and opening the configuration file, and then looking for the port setting.

File Locations

For most installs, the location for the MySQL configuration file will be located at:

  • Linux: /etc/mysql/my.cnf OR /etc/my.cnf
  • Windows: C:\ProgramData\MySQL\MySQL Server 8.0\my.ini
  • macOS: /usr/local/mysql/my.cnf

View with Text Editor

Use a text editor to open the configuration file. Common editors include Nano or Vim on Linux, Notepad on Windows, and Nano or TextEdit on macOS.

Look for the port directive under the [mysqld] section. It should look something like this:

[mysqld]
port = 3306
basedir = "C:/Program Files/MySQL/MySQL Server 8.0/"
datadir = "C:/ProgramData/MySQL/MySQL Server 8.0/Data/"
socket = "MySQL"
key_buffer_size = 16M
Enter fullscreen mode Exit fullscreen mode

If the port line is missing, this means MySQL is using the default port 3306.

Using netstat (Linux)

If you're on a Linux system, you can also use the handy netstat command to find out which port MySQL is using. Here’s how:

Start by opening your terminal. Enter the following command to display network connections related to MySQL:

sudo netstat -tuln | grep mysql
Enter fullscreen mode Exit fullscreen mode

This command lists all network connections, filtering for MySQL. You should see an output line similar to this:

tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1234/mysqld
Enter fullscreen mode Exit fullscreen mode

In this output, 3306 is the port number MySQL is listening on.

Try Releem to monitor, tune performance, optimize queries and check schema of your database server.

Top comments (0)