By default, MySQL will listen for connections from port
3306 when you run the database server.
Any MySQL client program generally will connect to port
3306 as well by default.
This includes GUI clients like MySQL workbench and SQLyog, as well as the
mysql command-line client.
Change MySQL default port
You can view and edit the port used by MySQL server by checking the configuration file used by your MySQL server.
When you run the server with the
start command, MySQL would look for a configuration file in the following locations:
The priority of the files above follows the order. It means that if MySQL already found the configuration file in
/etc/my.cnf location, the rest will be ignored.
Here’s the location priority for Windows operating system:
C:\Program Files\MySQL\MySQL Server x.x\my.ini
C:\Program Files\MySQL\MySQL Server x.x\my.cnf
Alternatively, you can pipe the result of
mysql --help command to the
ls command as shown in this StackOverflow answer.
You won’t have to look for the configuration file manually with this trick:
mysql --help | grep /my.cnf | xargs ls ls: /etc/my.cnf: No such file or directory ls: /etc/mysql/my.cnf: No such file or directory ls: ~/.my.cnf: No such file or directory /usr/local/etc/my.cnf
The above result shows that the
my.cnf file on my local computer is located on
Once you find the
.ini file for Windows) then you can add the
port option to the
[mysqld] section as shown below:
[mysqld] port = 3308 bind-address = 127.0.0.1 mysqlx-bind-address = 127.0.0.1
If there’s no
port option specified, then MySQL will default to port
Once you define the
port option, save your changes and restart your MySQL server.
In the example above, MySQL server should now listen for connection on port
3308 instead of
If you’re using a GUI client like MySQL Workbench, please don’t forget to change the port when you need to connect to the server.
But you don’t need to specify the port for
mysql command-line client because it takes the
port option from the configuration file into account when attempting to connect to the server.