There might be a situation where you need to change MySQL default port 3306 to something else to hide the port number for security purposes, or MySQL port 3306 is already being used by another application.
No matter what your reason for changing ports is, in this article, you will find the steps to change the default MySQL port from 3306 to 3360 or something else.
So, let me first start with the basic steps to changing the port number in MySQL. Or, if you are having an error on XAMPP, then use the table content to read how to fix the XAMPP does not start issue on Ubuntu or Windows.
Table of Contents
How to change MySQL port number in Ubuntu
To change the MySQL port number you need to modify “mysqld.cnf” file which you will get most probably in “/etc/mysql/mysql.conf.d/” or you can also use “/etc/mysql/my.cnf” to modify port number but I would suggest you to use “mysql.conf.d” because you will find other attributes likes “pid-file”,“socket”, “log-error” and all at one place so whenever you need to make any changes you can eaily do without looking into multiple files.
Modify any of the below files to change your default port for MySQL:
/etc/mysql/conf.d/mysqld.cnf OR /etc/mysql/mysql.cnf
So, let me show you how to change your default MySQL port from 3306 to something else in Linux, open the above-mentioned file using any command-line editor with sudo privilege:
$ sudo nano /etc/mysql/conf.d/mysqld.conf
Once you open the file, search for “mysqld” block, add the port attribute if it is not available, and specify the port number like the below code snippet:
port = 3308
Your screen should look like mine. If it is there, then you are good to save this configuration file.
After that, you need to restart the MySQL service to implement new changes, to make that run the following command on your terminal window:
$ sudo systemctl restart mysql.service
And once the service is restarted, you will not get any feedback on your screen to confirm changes, so to confirm, you can use the netstat command to check the port number.
How to know MySQL port number
There are multiple ways through which you can identify the port number on which MySQL is running on your Linux machine. One of them is to use the netstat command, and the second is to use MySQL itself.
For easy lookup, you can use the netstat command to check the listening port. To find out what port number MySQL is running on, type the code below on your terminal screen and replace PORT-NO with a real one.
$ netstat tulnp | grep "PORT-NO"
In my system MYSQL is runing on port 3308 over default 3306.
Get more information on How to Check which port is currently use in Linux
You can also verify the changes from the MySQL application. To check, first log in to your MySQL and pass the below command:
show variables like '%port%' ;
The output of the above command shows the changes have been implemented successfully.
That’s all to change the MySQL port number on Linux and other Ubuntu or any other Debian-based machine.
How to fix MySQL port 3306 already in use error on XAMPP
This is quite a common problem among XAMPP users when they click on the start button to launch the XAMPP server on their system. If you too have the same problem, then you need to change the default 3306 port to something else.
If you are pondering why this occurs, then one of the reasons is that MySQL is already installed and running on your system. XAMPP starts its own MySQL server on port 3306, but port 3306 can be used by only one of the applications at a time.
To fix the above issue, you can either uninstall MySQL from your system or else you can change the MySQL port number from XAMPP.
Instead of removing MySQL, just change the MySQL port to something else in XAMPP and restart your service to fix this issue.
Let me show you how to change the port number in XAMPP.
First, open XAMPP, and go to “Manage Servers” on the right side of the window you will find the “Configure” button, click on it.
This will open another window prompt, where you can specify a different port number like in the below image, and click on OK to save changes.
Once it’s done click on “Start” to start the server.
That’s all to change the MySQL port on Linux.
In this article, you have learned how to change the default port in MySQL on Linux and other distributions of Linux. Along with that, you also find a way to fix the XAMPP does not start issue.
If there is anything you want me to highlight or want to thank, then do comment in the section.
A man with a tech effusive who has explored some of the amazing technology stuff and is exploring more. While moving towards, I had a chance to work on Android development, Linux, AWS, and DevOps with several open-source tools.