Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: March 4, 2025
MySQL uses port 3306 by default, making it easy for applications to connect without extra setup. This default setting ensures everything works smoothly. However, since the port is well-known, attackers might target it by scanning for open ports linked to common services.
Changing the default port won’t completely secure our database, but it adds an extra layer of protection by making it harder for attackers to find MySQL. While this step doesn’t replace key security measures like firewalls or strong passwords, it’s a simple way to reduce the risk of unauthorized access without much hassle.
In this tutorial, we’ll explore how to change the default MySQL port on Linux and Windows operating systems.
MySQL listens on port 3306 by default, but there are scenarios where changing the port may be necessary:
This way, changing the default port of MySQL adds an extra layer of security.
On Linux systems, the MySQL configuration file is generally found at: “/etc/mysql/my.cnf” or “/etc/mysql/mysql.conf.d/mysqld.cnf“. These files contain MySQL settings, including the default port (3306). We can edit these files using a text editor like Nano or Vim, update the port, and then restart the MySQL service for the changes to take effect.
We can open the configuration file using the Nano editor with sudo privileges:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
In the MySQL configuration file, the [mysqld] section may not explicitly include the port directive because it defaults to 3306. Therefore, if we need to change the port, we can add or update the port directive under the [mysqld] section:
port=3307
We can replace 3307 with any custom port number for MySQL:
Finally, we save the changes in the configuration file and exit.
After updating the default port, we need to run the systemctl command to restart MySQL service:
sudo systemctl restart mysql
Let’s verify MySQL Server’s running port with the netstat command:
sudo netstat -tulnp | grep mysql
The screenshot shows that MySQL is listening on port 3307 for SQL connections:
If a firewall is in use, its settings should be updated to allow traffic through the new port:
sudo ufw allow 3307/tcp
This step ensures proper communication for MySQL and prevents incoming and outgoing connections from being blocked:
We can edit the “my.ini” file to change the default MySQL port on the Windows operating system. This file can be located at the “C:\ProgramData\MySQL\MySQL Server version_number\my.ini“.
First, we open the my.ini file, navigate to the [mysqld] section, and then, we can add or modify the port line:
port=3307
Let’s replace 3307 with any custom port and save the file:
Let’s restart the MySQL service through the Windows Services Manager. To do this, first, we press Win + R, type services.msc, and press Enter. After this, we locate MySQL in the Windows Services Manager and Restart it:
Let’s verify the altered port by accessing MySQL with the new port:
mysql -u root -p --port=3307
After specifying the correct password, the MySQL prompt (mysql>) appears, which indicates a successful connection:
To ensure the new port is allowed through the Windows Firewall, we need to access the Windows Defender Firewall from the Control Panel:
Next, we need to navigate to the Advanced Settings to open the firewall configuration, select Inbound Rules, and then create a new rule by clicking the New Rule… button:
We choose Port as the rule type, then select TCP or UDP and enter the new port number:
Also, we allow the connection, specify when the rule applies, and give the rule a Name (such as “Allow MySQL Port”):
Finally, we click the Finish button to complete the process.
Although changing the port improves security, it can lead to maintenance challenges. For instance, when MySQL’s port is changed, all connected applications must be updated. WordPress, for example, expects MySQL to use the default port (3306). To connect to a different port, we need to edit the wp-config.php file, such as: define(‘DB_HOST’, ‘localhost:3307’), where 3307 is the new port.
Additionally, identifying all applications that rely on the default port can be difficult, and changing it may cause some applications to stop working unexpectedly. Without proper documentation, debugging issues related to the port change can become more challenging in the future.
In this article, we demonstrated steps to change the default port of MySQL on Linux and Windows operating systems. By following the steps discussed in this article, we can efficiently change the default MySQL port to a custom port.
Changing the default MySQL port from 3306 to a custom port can add an extra layer of security by reducing the likelihood of malicious attacks targeting the default port. However, it’s important to consider the potential challenges.