This article will show you how to find the default or current MySQL (or MariaDB) port and how to change it.
What is a Port?
A port is a numerical identifier for a communication endpoint on a computer network. Or, put simpler, it’s a unique number on your computer that points to a specific program that is running so that other programs can connect to it.
MySQL/Maria DB, being networked database services, expose themselves to other computers via a configured port, allowing other computers to connect to and read from the databases being hosted by MySQL.
A port is usually displayed following a : (colon) character:
Where HOSTNAME is the network address or hostname of the computer and PORT is the port number.
Default MySQL Port
By default, MySQL operates on port:
This port will be in use on a fresh installation of MySQL.
Changing the MySQL Port
The port is configured in the my.cnf MySQL configuration file. This file is usually located at:
However, the location of this file may vary depending on your OS, so you can find it by running:
mysql --verbose --help | less
If you look through the output, you will be able to see which .cnf file is loaded.
This file can then be edited using the nano text editor by running:
sudo nano /etc/mysql/my.cnf
Then, scroll down to the line where the port is configured:
port = 3306
…and change the port number to whatever new port you wish to use.
Finding the current MySQL Port
The netstat command can tell you which port a running process is using and whether it is open:
The above command will list running processes and the ports and interfaces they are bound to – the output will look something like this:
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 27788/mysqld
Under the Local Address column, you can see that MySQL is running on port 3306.
Specifying the Port When Connecting
If you are connecting using the MySQL client from the command line, you can specify the port with the -port option:
mysql --host=localhost --user=username --password databasename --port=3306
If you are using the default port, there is no need to specify it.
If you are connecting from another application, for example, a PHP framework or GUI database manager, that software will provide an option to specify the port, which should be documented in their own documentation.