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:
/etc/my.cnf
/etc/mysql/my.cnf
/usr/local/mysql/etc/my.cnf
~/.my.cnf
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:\Windows\my.ini
C:\Windows\my.cnf
C:\my.ini
C:\my.cnf
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 grep
and 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 /usr/local/etc/my.cnf
Once you find the .cnf
(or .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 3306
.
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 3306
.
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.