DEV Community

Cover image for Changing MySQL Default Port number
Vinod Devasia
Vinod Devasia

Posted on

Changing MySQL Default Port number

Sometimes, when you install a new application on your server, they may compete/clash with port numbers already been used by other applications. This may cause your server to slow down, some applications to crash or even servers to crash occasionally.

One such instance is MySQL crashing periodically after I installed a certain application. Unfortunately, this application uses port 3306, and it has no method to specify a different port. MySQL default port is 3306. And it has good flexibility to change its port.

So let us see how.

Step 1 Edit the MySQL my.ini

Go to [path-to-xampp-folder]/mysql/bin/ and open the file my.ini. Or from Xampp, click on Config at the MySQL Module.

Locate the line(s) containing '3306' no. and change it to '3390' or something else and save. Note to change both the server and client parts as shown in the figure below.

Lets change our port to 3390

Please make sure the new port number is not used by any other service. You can use 'Netstat' to check all used ports on your system.

NetStat displaying the port used by diff apps

Step 2 Edit the phpMyAdmin file

We need to change the way phpMyAdmin connects to MySQL so that it knows that it has to use the new port number instead of the default one. Failing to do so, will cause the following error whenever you try to connect to MySQL.

phpMyAdmin failed to connect error

From xampp control panel, click on the config for the apache server, and select the phpMyAdmin.ini and make the following changes as shown below

phpMyAdmin

Step 3 Edit all website connections to use the new port

Edit all configuration files in your webserver that connect to this MySQL instance with the new port number.

Failing to do this will cause the web apps to fail.

MySQL Connection error

Based on how your app connects to the MySQL server, either in the config file or a function, ensure that it knows that we are not connecting via the default port anymore.

In my case, my App had a function connecting to the MySQL

add the port number

And everything is back working!!!

App now connected to the new port

Conclusion:
If you ever have to change the port of your MySQL server, ensure that you configure it both on servers, client connections, phpMyAdmin, and the connecting app(s).

Discussion (0)