MySQL connects on port 3306 by default, but you can configure it to use a different port

When your computer or application needs to talk to a MySQL database, it sends that request through a specific port — think of a port as a numbered door on your server. MySQL listens on port 3306 by default, which means most MySQL installations are set up to receive connections there. However, you can change this port to something else if you need to, and you should know which ports are available and how to check what your MySQL server is actually using.

The port number matters because your application has to know exactly where to send its request. If your application is looking for MySQL on port 3306 but your server is listening on port 3307, the connection fails. This guide covers the standard ports, how to find out which port your MySQL server is using, and how to change it if needed.

Key Takeaways

  • MySQL uses port 3306 by default, but this is configurable and not fixed.
  • You can change the MySQL port by editing the configuration file (usually my.cnf or my.ini) and restarting the server.
  • To find which port your MySQL server is actually listening on, run the command netstat -an | grep LISTEN on Linux or Mac, or check the configuration file directly.
  • Port numbers must be between 1024 and 65535 for non-root users, and ports below 1024 require administrator or root access.
  • Your application connection string must match the port your MySQL server is listening on, or the connection will fail.

The default MySQL port and why it matters

Port 3306 is the standard port that MySQL listens on when you first install it. This means if you install MySQL on a server and do nothing else, any application trying to connect to that server will automatically look for MySQL on port 3306. Most database tools, frameworks, and applications assume port 3306 unless you tell them otherwise.

The reason this matters is that your application needs to know the exact address and port to reach your database. A connection string typically looks like localhost:3306 or 192.168.1.100:3306 — the number after the colon is the port. If your MySQL server is listening on port 3306 but your application is trying to connect to port 3307, the connection fails immediately, even if MySQL is running fine.

How to find which port your MySQL server is using

If you inherit a server or are not sure which port MySQL is actually listening on, you can check in several ways. The most direct method is to look at the MySQL configuration file itself. On Linux and Mac systems, this file is usually located at /etc/mysql/my.cnf or /usr/local/etc/my.cnf. On Windows, it is typically C:\ProgramData\MySQL\MySQL Server 8.0\my.ini (the version number may differ).

Open the configuration file with a text editor and search for the line that says port = 3306. Whatever number appears after the equals sign is the port MySQL is configured to use. If you do not see a port line at all, MySQL is using the default port 3306.

Another way to check is to use the command line. On Linux or Mac, open a terminal and run netstat -an | grep 3306. If you see output that includes LISTEN, MySQL is listening on port 3306. If you get no output, either MySQL is not running or it is listening on a different port. You can search for other common ports by replacing 3306 with 3307, 3308, or whatever port you suspect.

On Windows, open Command Prompt and run netstat -ano | findstr 3306. This shows you whether anything is listening on that port and which process is using it.

Changing the MySQL port

To change the port MySQL listens on, you need to edit the configuration file and restart the MySQL service. First, locate your configuration file using the paths listed above. Open it with a text editor that handles plain text — Notepad on Windows or nano on Linux work fine.

Find the line that says port = 3306. If it does not exist, add it under the [mysqld] section. Change the number to whatever port you want to use. For example, to use port 3307, the line should read port = 3307. Save the file and close it.

Next, restart the MySQL service so it reads the new configuration. On Linux, run sudo systemctl restart mysql. On Mac with Homebrew, run brew services restart mysql. On Windows, open Services (search for "Services" in the Start menu), find MySQL, right-click it, and select Restart.

After the restart, verify the change worked by checking which port MySQL is now listening on using the netstat commands described above. Then update your application's connection string to use the new port.

Port number restrictions and what you can use

Not all port numbers are available to you. Ports numbered 1 through 1023 are reserved for system services and require administrator or root access to use. If you are running MySQL as a regular user (not root or administrator), you must choose a port between 1024 and 65535.

Some ports in the 1024 to 65535 range are also commonly used by other services. Port 3307 is often used as an alternative MySQL port. Ports 8000, 8080, and 8888 are frequently used by web servers. Before you change MySQL to a new port, check whether anything else is already using it. You can do this with the netstat commands mentioned above — if you get no output when you search for a port, it is probably available.

In practice, most people either stick with the default port 3306 or use 3307 if they need to run two MySQL instances on the same machine. Changing the port is useful for security (hiding MySQL from casual port scans) or when you need multiple database servers on one machine, but it adds complexity because every application connecting to that server must know the non-standard port.

Troubleshooting connection failures after changing the port

If you changed the MySQL port and now your application cannot connect, the most common cause is that the application is still trying to connect to the old port. Check your application's configuration file or connection settings and update the port number there. For web applications, this might be in a file called config.php, database.yml, .env, or something similar depending on what framework you are using.

Another common issue is that the MySQL service did not actually restart after you changed the configuration file. Try restarting it again using the commands above. You can verify it is running by checking the process list — on Linux, run ps aux | grep mysql and look for a mysqld process.

If MySQL is running but still on the old port, the configuration file change may not have taken effect. Double-check that you edited the correct configuration file — MySQL might be reading from a different my.cnf than the one you edited. You can find out which file MySQL is actually using by running mysql --help | grep "my.cnf" from the command line.

Frequently Asked Questions

Can I run MySQL on multiple ports at the same time?

No, a single MySQL server instance listens on only one port. However, you can run multiple separate MySQL server instances on the same machine, each listening on a different port. This requires separate configuration files and separate data directories, and is typically only done in development or testing environments.

Is it more secure to use a non-standard port like 3307 instead of 3306?

Changing the port provides minimal security benefit. An attacker scanning your server will find MySQL on any port it is listening on. Real security comes from using strong passwords, restricting network access with a firewall, and keeping MySQL updated. Changing the port is better thought of as a way to avoid accidental connections rather than a security measure.

What happens if I try to use a port that is already in use?

MySQL will fail to start and log an error saying the port is already in use or that it cannot bind to that address. Check what is using the port with netstat and either stop that service or choose a different port for MySQL.

Do I need to change the port in my application if I change the MySQL port?

Yes. Your application will not be able to find MySQL unless you update its connection settings to use the new port. The exact location of these settings depends on your application, but it is usually in a configuration file or environment variables.

Can I use port 80 or 443 for MySQL?

Technically yes if you have root or administrator access, but you should not. Port 80 is for HTTP web traffic and port 443 is for HTTPS. Using these ports for MySQL would break web services on that machine and confuse anyone trying to troubleshoot the server later. Stick to ports above 1024 that are not commonly used by other services.