MySQL default port and how to change it

Posted on Oct 07, 2021

Learn the default port used by MySQL server and how to change it

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

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:

port = 3308
bind-address =
mysqlx-bind-address =

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.

Level up your programming skills

I'm sending out an occasional email with the latest programming tutorials. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.