MySQL Default Port and Configuration
MySQL uses port 3306 by default. Here's how to check and change it.
The default MySQL port
MySQL listens on port 3306 by default. This has been the standard since the early days of MySQL, and it applies to every platform: Linux, macOS, and Windows.
When you install MySQL and start the server without any custom configuration, it binds to port 3306 on all available network interfaces. Every MySQL client, driver, and GUI tool assumes port 3306 unless you tell it otherwise.
MariaDB also uses port 3306 by default, since it started as a fork of MySQL. This means you cannot run both MySQL and MariaDB on the same port simultaneously. More on that later.
How to check the current MySQL port
There are several ways to find out which port your MySQL server is running on.
From inside MySQL
Connect to your server and run:
Output:
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3306 |
+---------------+-------+
You can also use the status command in the MySQL client:
This prints connection details including the TCP port.
From the command line
Use mysqladmin to check the port without opening an interactive session:
Or query the variable directly:
Check which port the process is listening on
On Linux or macOS, you can check what port the MySQL process has bound to:
Or with ss on Linux:
On Windows, use netstat:
These commands are helpful when MySQL is running but you cannot connect to it. They confirm whether the server is listening and on which port.
Changing the MySQL port
There are two ways to change the port: through the configuration file (permanent) or through a command-line flag (temporary).
Using the configuration file
The MySQL configuration file location depends on your operating system:
| OS | Config file path |
|---|---|
| Linux (Debian/Ubuntu) | /etc/mysql/mysql.conf.d/mysqld.cnf or /etc/mysql/my.cnf |
| Linux (RHEL/CentOS) | /etc/my.cnf |
| macOS (Homebrew) | /opt/homebrew/etc/my.cnf or /usr/local/etc/my.cnf |
| Windows | C:\ProgramData\MySQL\MySQL Server 8.0\my.ini |
Open the file and find the [mysqld] section. Add or modify the port setting:
You also need to update the client section so that the mysql command-line tool connects to the right port:
After saving, restart MySQL for the change to take effect.
On Linux (systemd):
On macOS with Homebrew:
On Windows, open Services (services.msc), find the MySQL service, and restart it. Or use the command line:
Using the --port flag on startup
You can override the port when starting MySQL without editing any config file:
This is useful for testing or running a temporary instance. The original configuration file remains unchanged, and MySQL will revert to the configured port on the next normal restart.
Verifying the change
After restarting, confirm the new port:
If MySQL was previously on 3306 and you changed it, trying to connect without specifying the port will fail. Make sure to update any application connection strings.
MySQL port on different platforms
Linux (Ubuntu/Debian)
MySQL is typically installed via APT:
The configuration lives in /etc/mysql/mysql.conf.d/mysqld.cnf. After installation, MySQL starts on port 3306 and listens on 127.0.0.1 by default.
To check the current bind address and port:
Linux (RHEL/CentOS/Fedora)
On Red Hat-based systems, MySQL (or its equivalent) is installed with dnf or yum:
The configuration file is /etc/my.cnf. Port settings follow the same [mysqld] format described above.
If SELinux is enabled, changing the port requires an additional step. SELinux restricts which ports MySQL can bind to. You need to allow the new port:
Without this, MySQL will fail to start on a non-standard port and the error log will reference a permission denial.
macOS with Homebrew
Install MySQL through Homebrew:
The config file is at /opt/homebrew/etc/my.cnf on Apple Silicon Macs or /usr/local/etc/my.cnf on Intel Macs. If the file does not exist, create it.
If you need to create or edit it:
Then restart:
Windows
On Windows, MySQL is usually installed through the MySQL Installer or a ZIP archive. The config file is my.ini, located in the MySQL data directory (often C:\ProgramData\MySQL\MySQL Server 8.0\).
Edit my.ini:
Restart the MySQL service:
If you installed MySQL as a different service name, replace MySQL80 with the correct name. Check the service name in services.msc.
Docker
When running MySQL in Docker, the container's internal port is still 3306, but you map it to any host port using the -p flag:
This maps host port 3307 to container port 3306. Connect from the host with:
Note the uppercase -P flag for port (lowercase -p is for password).
To run multiple MySQL containers, assign different host ports:
With Docker Compose, define the port mapping in your docker-compose.yml:
Firewall configuration
If you need remote connections to MySQL, you must open the port in your firewall.
Linux (UFW)
For a non-standard port:
To restrict access to a specific IP address:
Linux (firewalld)
Windows Firewall
Open an elevated Command Prompt:
Or configure it through Windows Defender Firewall in the Control Panel.
Cloud providers
On AWS, open the port in your Security Group's inbound rules. On Google Cloud, create a firewall rule allowing TCP traffic on your MySQL port. On Azure, update the Network Security Group.
In all cases, restrict the source IP range to known addresses. Exposing MySQL to the entire internet is a serious security risk.
MySQL bind address
Opening the firewall is not enough. MySQL also needs to accept connections from remote hosts. By default, many installations bind to 127.0.0.1, which only allows local connections.
Edit your MySQL config file:
This tells MySQL to listen on all network interfaces. For tighter security, bind to a specific IP:
Restart MySQL after making this change.
Connecting on a non-standard port
When MySQL runs on a port other than 3306, you need to specify the port in every connection.
MySQL command-line client
Use the uppercase -P flag. The lowercase -p is for the password prompt.
Connection strings
Most application frameworks use a connection string or DSN. Here are examples with a non-standard port:
PHP (PDO):
Python (mysql-connector):
Node.js (mysql2):
Java (JDBC):
Go:
GUI tools
Most GUI database clients have a port field in their connection settings. Tools like DB Pro, MySQL Workbench, and DBeaver all let you specify the port when setting up a connection.
Running multiple MySQL instances
You may need multiple MySQL instances on the same machine for testing different versions or isolating workloads. Each instance needs its own port, data directory, and socket file.
Using different config files
Create a separate config file for each instance:
Start the second instance:
Connect to the second instance:
Or by port:
Using mysqld_multi
MySQL provides mysqld_multi for managing multiple instances. Add sections to your config file:
Start all instances:
Start a specific instance:
Check the status:
MySQL port vs MariaDB port
Both MySQL and MariaDB default to port 3306. Since MariaDB was forked from MySQL, it maintained the same default to serve as a drop-in replacement.
If you need both on the same machine, change one of them to a different port. A common convention is to keep MySQL on 3306 and move MariaDB to 3307 (or vice versa).
In the MariaDB config file, the syntax is identical:
MariaDB uses the same config file format and section names as MySQL, including [mysqld] rather than [mariadbd] (though newer versions of MariaDB accept both).
The client protocol is compatible between MySQL and MariaDB for most operations, so the port is the primary way to direct connections to the right server when both are installed.
Common issues and troubleshooting
Port already in use
If MySQL fails to start with an error like "Can't start server: Bind on TCP/IP port: Address already in use," something else is occupying the port.
Find what is using the port:
Common culprits include a previous MySQL instance that did not shut down cleanly, MariaDB running on the same port, or another application that grabbed port 3306.
To fix this, stop the conflicting process or change MySQL to a different port.
Permission denied
On Linux, binding to ports below 1024 requires root privileges. Port 3306 is above this threshold, so it should not be a problem. However, if you choose a port below 1024, you will need to run MySQL as root or grant the capability:
Another source of "permission denied" errors is SELinux (on RHEL/CentOS). If you changed the port, SELinux may block the new one. Add the port to SELinux's allowed list:
On some systems, AppArmor can cause similar restrictions. Check /var/log/syslog or /var/log/mysql/error.log for details.
Cannot connect remotely
If you can connect locally but not from another machine, check these three things in order:
-
bind-address: Make sure MySQL is not bound to
127.0.0.1. Set it to0.0.0.0or the specific IP of the network interface you want. -
Firewall: Confirm the port is open. Test with
telnetorncfrom the remote machine:
If the connection is refused, the firewall is likely blocking it.
- MySQL user permissions: MySQL controls access by both username and host. A user created with
'user'@'localhost'cannot connect remotely. You need:
The % wildcard allows connections from any host. For better security, replace it with a specific IP or subnet:
Connection refused on the correct port
If you have verified the port is correct and the firewall is open, check whether MySQL is listening on TCP at all. Some configurations use Unix sockets exclusively.
Look for the skip-networking directive in your config file:
If skip-networking is enabled, MySQL will not listen on any TCP port. Remove it and restart MySQL.
MySQL starts but clients cannot connect
This sometimes happens when the client defaults to a Unix socket connection instead of TCP. Force a TCP connection by specifying the host as an IP address:
Using -h localhost may route through the Unix socket on some systems, bypassing the port entirely. Using -h 127.0.0.1 forces TCP.
Port security best practices
Keeping your MySQL port secure is straightforward:
- Do not expose MySQL to the public internet. Use a VPN, SSH tunnel, or private network for remote access.
- Restrict firewall rules to specific IP addresses rather than opening the port to all traffic.
- Use a non-standard port as an additional layer. This is not true security (it is security through obscurity), but it reduces noise from automated scanners targeting port 3306.
- Enable TLS for encrypted connections, especially over networks you do not fully control.
- Monitor connection attempts in the MySQL error log or general log to detect unauthorized access.
SSH tunneling
Instead of opening the MySQL port in your firewall, create an SSH tunnel:
This forwards your local port 3306 to the remote MySQL server. Connect your client to 127.0.0.1:3306 as if MySQL were running locally.
For a different local port:
This maps local port 3307 to the remote server's port 3306.
Quick reference
| Task | Command |
|---|---|
| Check current port | SHOW VARIABLES LIKE 'port'; |
| Check listening port (Linux/macOS) | sudo lsof -i -P -n \| grep mysqld |
| Check listening port (Windows) | netstat -an \| findstr 3306 |
| Change port permanently | Edit port = 3307 in my.cnf / my.ini |
| Change port on startup | mysqld --port=3307 |
| Connect on non-standard port | mysql -h 127.0.0.1 -P 3307 -u root -p |
| Open port in UFW | sudo ufw allow 3306/tcp |
| Open port in firewalld | sudo firewall-cmd --permanent --add-port=3306/tcp |
| Allow port in SELinux | sudo semanage port -a -t mysqld_port_t -p tcp 3307 |
| Create SSH tunnel | ssh -L 3306:127.0.0.1:3306 user@server |
Summary
MySQL's default port is 3306, and in most setups you will never need to change it. When you do need a different port (for running multiple instances, avoiding conflicts with MariaDB, or meeting organizational requirements), the process is the same across platforms: update the config file, restart the server, and update your client connections.
The most common port-related problems come down to three things: something else already using the port, the firewall blocking connections, or MySQL's bind address restricting access to localhost. Checking those three in order will resolve the majority of connection issues.