Check & Change MySQL Port Number with Security Tips

1. About MySQL’s Default Port Number

MySQL is one of the relational database management systems (RDBMS) and plays a crucial role in supporting communication between web applications and database servers. The foundation of this communication is the “port number,” and MySQL uses port 3306 by default. A port number specifies the destination that a client connects to when accessing a database server and serves as an identifier to distinguish the communication path from other applications and services.

Why MySQL Uses Port 3306

Port 3306 is the number set as the default when a MySQL server is installed, and it is generally left unchanged unless there is a specific reason. Through this port, the client and MySQL server exchange data. Because MySQL operates over the TCP (Transmission Control Protocol), explicitly setting the port number is recommended for security reasons.

2. How to Check the Port Number

If the MySQL port number has been changed from the default 3306 or you want to verify the default settings, you can easily check it using the commands below.

Checking with the SHOW VARIABLES command

After logging into MySQL, you can verify the currently used port number by running the following command.
SHOW VARIABLES LIKE 'port';
Running this command displays the numeric value associated with the variable name “port,” allowing you to see the current port number.

Checking with the status command

Another method is to use MySQL’s status command to display the current configuration. This command shows basic database information along with the port number, making it a convenient way to verify settings. The steps are: first log into MySQL and then enter the following command.
status;
With this method, you can retrieve the status information of the connected database, which includes the port number.

3. How to Change the Port Number

To change MySQL’s default port number, edit the MySQL configuration file “my.cnf” or “my.ini”. This configuration file is typically located in the directory where MySQL is installed.

How to Edit the Configuration File

  1. Open my.cnf or my.ini in a text editor.
  2. Find the [mysqld] section and specify the port number as shown below.
   [mysqld]
   port = 3310
  1. After changing the settings, restart the MySQL server.
After the restart, the MySQL server will start with the new port number. This procedure allows you to customize the port to avoid conflicts with specific applications or services.

Specifying the Port on the Command Line

When connecting from a client and you need to specify a particular port, use the port number as shown below.
mysql -h localhost -P 3310 -u root -p
In this way, you can connect to the MySQL server using the specified port number.

4. Points to Note When Changing Ports

When changing MySQL’s port number, it is important to pay attention to the following points.

Check for Conflicting Ports

When changing the port number, you need to ensure it does not conflict with other applications or services. For example, on Linux systems, you can use commands such as netstat or lsof to check which ports are currently in use.

Consider Security Measures

Changing the default port number can help reduce the risk of attacks from outside. Attackers typically target default ports, so changing the port can serve as a security measure. However, changing the port alone does not provide complete security, so it is advisable to also consider additional measures such as firewall configuration and IP address restrictions.

5. Port Changes and Security Measures

Changing MySQL’s port number from the default 3306 can improve security in some cases. However, it is advisable to adopt stronger protection measures rather than relying solely on changing the port number as a security measure.

Remote Connections Using an SSH Tunnel

When connecting to a MySQL server remotely and securely, using an SSH tunnel is recommended. An SSH tunnel allows you to route traffic through an encrypted channel, preventing eavesdropping and tampering with the data. For example, you can set up an SSH tunnel with steps like the following.
  1. Use an SSH client to connect to the server and configure the tunnel.
  2. Specify MySQL’s port number as the destination port, establishing encrypted communication between the local and remote sides.

6. Summary

This article explained how to check and change MySQL’s default port number. The port number is one of the basic settings related to accessing the database server and security, and knowing how to verify and properly modify it is important for database operations. In the future, when dealing with port conflicts or security risks, refer to this article to review your settings.