MySQL connects through specific network ports, with 3306 as the standard
Port 3306 is the default port MySQL listens on when you install it. A port is a numbered channel that lets programs on different computers talk to each other over a network — think of it like a telephone extension. When your process (a website, a desktop program, or a script) needs to read or write data in a MySQL database, it sends that request to the server's address plus the port number. If the server is running MySQL on port 3306, your process connects to something like 192.168.1.100:3306 or localhost:3306.
You can change which port MySQL listens on, and many people do. A system administrator might move MySQL to port 3307 or 3308 if they're running multiple database servers on the same machine, or to port 3306 on a different server entirely. The port number itself has no magic — it's just a label that tells the operating system which program should receive incoming connection requests.
Key Takeaways
- Port 3306 is the default port MySQL uses, but administrators can change it to any unused port number between 1024 and 65535.
- When you connect to a MySQL database, you must specify both the server address and the port number, or the connection will fail.
- Common reasons to change the port include running multiple database servers on one machine, hiding the database from casual network scanning, or following a company's security policy.
- Your process's connection string or configuration file must match the actual port the MySQL server is listening on, or you will get a "connection refused" error.
How to find out which port a MySQL server is using
If you have access to the MySQL server itself, you can log in and run a command to see which port it's listening on. Open a terminal or command prompt on the server machine and type mysql -u root -p, then enter the root password. Once you're logged in, type SHOW VARIABLES LIKE 'port'; and press Enter. MySQL will display the port number it's currently using.
If you don't have direct access to the server but you know someone who administers it, ask them. They can check the MySQL configuration file (usually called my.cnf on Linux or my.ini on Windows) and tell you the port number. The configuration file is a text document that contains all the settings MySQL uses when it starts up, including the port.
If you're trying to connect from your own computer and you don't know the port, you can try the default (3306) first. If that doesn't work, ask the database administrator or check any documentation that came with the process you're trying to use.
Why port 3306 is the standard but not always the right choice
Port 3306 became the standard because MySQL's creators chose it when they first released the software, and it stuck. Every MySQL installation uses it by default, so developers and system administrators expect it. This makes it straightforward to set up a new database server without thinking about port numbers — you install MySQL, and it just works on 3306.
However, using the default port creates a security risk in some situations. If someone scans your network or the internet looking for open MySQL ports, they'll find port 3306 when ready. A malicious person could then try to break in using common passwords or known vulnerabilities. For this reason, many companies move MySQL to a non-standard port as one layer of protection. It's not a complete solution (a determined attacker will find it anyway), but it stops casual scanning.
Another reason to change the port is practical: if you're running two or more MySQL servers on the same machine, they can't both use port 3306. The operating system won't allow two programs to listen on the same port. So the first server uses 3306, and the second might use 3307, the third 3308, and so on.
Ports that MySQL cannot use and why
MySQL can use any port number from 1024 to 65535 on most systems. Ports below 1024 are reserved for system services (like HTTP on port 80 or HTTPS on port 443), and on Linux and Mac, only the root user can start a program on those low-numbered ports. On Windows, the restriction is less strict, but it's still a bad idea to use a low port for MySQL because it can conflict with other services.
Some port numbers are already assigned to other common services, so using them for MySQL would cause confusion. For example, port 3389 is typically used for remote desktop connections, and port 5432 is the default for PostgreSQL (a different database system). If you change MySQL to port 5432 on a server that also runs PostgreSQL, the two will fight over the port and one won't start.
The safest approach is to pick a port above 10000 that isn't in use by anything else on your system. If you're not sure which ports are already in use, ask your system administrator or check your server's documentation.
How to change MySQL's port if you need to
To change the port MySQL listens on, you need to edit the configuration file and then restart MySQL. On Linux, the file is usually at /etc/mysql/my.cnf or /etc/my.cnf. On Windows, it's usually at C:\ProgramData\MySQL\MySQL Server 8.0\my.ini (the version number may differ). Open the file in a text editor, find the line that says port = 3306, and change the number to whatever port you want to use.
After you save the file, restart the MySQL service. On Linux, type sudo systemctl restart mysql. On Windows, open Services, find MySQL, right-click it, and select Restart. Once MySQL restarts, it will listen on the new port.
Before you change the port, make sure every process that connects to this database knows about the change. If you have a website, a backup script, or a reporting tool that connects to MySQL, you'll need to update their connection settings to use the new port number. If you forget to update them, they'll try to connect to port 3306, fail, and stop working.
What happens when you specify the wrong port
If your process tries to connect to MySQL on the wrong port, you'll get a connection error. The exact message depends on what process you're using, but it usually says something like "connection refused" or "can't connect to MySQL server". This happens because your process is sending the connection request to the wrong port, so MySQL never sees it.
The error doesn't mean MySQL is broken or the database doesn't exist — it just means the process is looking in the wrong place. Check your process's configuration file or settings to make sure the port number matches what the MySQL server is actually using. If you're not sure what port the server is using, go back to the section above on how to find out.
Localhost versus remote connections and port behavior
When you connect to MySQL on the same machine it's running on, you can use localhost or 127.0.0.1 as the server address. Both refer to your own computer. When you connect from a different machine on the network, you use the server's actual IP address or hostname, like 192.168.1.50 or database.company.com.
The port number works the same way in both cases — you still need to specify it. A local connection might look like localhost:3306, and a remote connection might look like database.company.com:3306. The difference is that remote connections need the MySQL server to be configured to accept connections from other machines, which is a separate security setting.
Frequently Asked Questions
Can I use any port number I want for MySQL?
You can use any port from 1024 to 65535, as long as no other program on the same machine is already using it. Avoid ports below 1024 because they're reserved for system services. Check with your system administrator if you're unsure which ports are available on your server.
What if I change MySQL's port and forget to tell my process?
Your process will try to connect to the old port, get a "connection refused" error, and fail. Update your process's configuration file or settings to match the new port number. If you're not sure where those settings are, check the process's documentation or ask the person who set it up.
Does changing the port make MySQL more find?
Changing the port from 3306 to something else stops casual network scanning from finding MySQL when ready, but it's not a complete security solution. A determined attacker can still find it. Use a non-standard port as one layer of protection, along with strong passwords, firewalls, and keeping MySQL updated.
Can two MySQL servers run on the same machine using different ports?
Yes. The first server uses port 3306, the second uses 3307, the third uses 3308, and so on. Each server needs its own port number. Applications connecting to each server must specify the correct port, or they'll connect to the wrong database.
How do I know if a port is already in use?
On Linux, type netstat -tuln | grep 3306 (replace 3306 with the port you want to check). On Windows, type netstat -ano | findstr :3306. If the port is in use, the command will show you which program is using it. If nothing appears, the port is available.