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:

SQL
SHOW VARIABLES LIKE 'port';

Output:

+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |
+---------------+-------+

You can also use the status command in the MySQL client:

SQL
\s

This prints connection details including the TCP port.

From the command line

Use mysqladmin to check the port without opening an interactive session:

BASH
mysqladmin -u root -p variables | grep port

Or query the variable directly:

BASH
mysql -u root -p -e "SHOW VARIABLES LIKE 'port';"

Check which port the process is listening on

On Linux or macOS, you can check what port the MySQL process has bound to:

BASH
sudo lsof -i -P -n | grep mysqld

Or with ss on Linux:

BASH
sudo ss -tlnp | grep mysql

On Windows, use netstat:

CMD
netstat -an | findstr 3306

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:

INI
[mysqld]
port = 3307

You also need to update the client section so that the mysql command-line tool connects to the right port:

INI
[client]
port = 3307

After saving, restart MySQL for the change to take effect.

On Linux (systemd):

BASH
sudo systemctl restart mysql

On macOS with Homebrew:

BASH
brew services restart mysql

On Windows, open Services (services.msc), find the MySQL service, and restart it. Or use the command line:

CMD
net stop MySQL80
net start MySQL80

Using the --port flag on startup

You can override the port when starting MySQL without editing any config file:

BASH
mysqld --port=3307

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:

BASH
mysql -u root -p --port=3307 -e "SHOW VARIABLES LIKE '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:

BASH
sudo apt install mysql-server

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:

BASH
grep -E "port|bind-address" /etc/mysql/mysql.conf.d/mysqld.cnf

Linux (RHEL/CentOS/Fedora)

On Red Hat-based systems, MySQL (or its equivalent) is installed with dnf or yum:

BASH
sudo dnf install mysql-server
sudo systemctl start mysqld

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:

BASH
sudo semanage port -a -t mysqld_port_t -p tcp 3307

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:

BASH
brew install mysql
brew services start mysql

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.

BASH
# Check if the config file exists
ls /opt/homebrew/etc/my.cnf

If you need to create or edit it:

INI
[mysqld]
port = 3307

Then restart:

BASH
brew services restart mysql

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:

INI
[mysqld]
port=3307

[client]
port=3307

Restart the MySQL service:

CMD
net stop MySQL80
net start MySQL80

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:

BASH
docker run -d \
  --name mysql-server \
  -e MYSQL_ROOT_PASSWORD=my-secret-pw \
  -p 3307:3306 \
  mysql:8.0

This maps host port 3307 to container port 3306. Connect from the host with:

BASH
mysql -h 127.0.0.1 -P 3307 -u root -p

Note the uppercase -P flag for port (lowercase -p is for password).

To run multiple MySQL containers, assign different host ports:

BASH
docker run -d --name mysql-1 -e MYSQL_ROOT_PASSWORD=pw1 -p 3306:3306 mysql:8.0
docker run -d --name mysql-2 -e MYSQL_ROOT_PASSWORD=pw2 -p 3307:3306 mysql:8.0
docker run -d --name mysql-3 -e MYSQL_ROOT_PASSWORD=pw3 -p 3308:3306 mysql:8.0

With Docker Compose, define the port mapping in your docker-compose.yml:

YAML
services:
  db:
    image: mysql:8.0
    ports:
      - "3307:3306"
    environment:
      MYSQL_ROOT_PASSWORD: my-secret-pw

Firewall configuration

If you need remote connections to MySQL, you must open the port in your firewall.

Linux (UFW)

BASH
sudo ufw allow 3306/tcp

For a non-standard port:

BASH
sudo ufw allow 3307/tcp

To restrict access to a specific IP address:

BASH
sudo ufw allow from 192.168.1.100 to any port 3306

Linux (firewalld)

BASH
sudo firewall-cmd --permanent --add-port=3306/tcp
sudo firewall-cmd --reload

Windows Firewall

Open an elevated Command Prompt:

CMD
netsh advfirewall firewall add rule name="MySQL" dir=in action=allow protocol=TCP localport=3306

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:

INI
[mysqld]
bind-address = 0.0.0.0

This tells MySQL to listen on all network interfaces. For tighter security, bind to a specific IP:

INI
[mysqld]
bind-address = 192.168.1.50

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

BASH
mysql -h 127.0.0.1 -P 3307 -u root -p

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):

PHP
$dsn = 'mysql:host=127.0.0.1;port=3307;dbname=my_app';

Python (mysql-connector):

PYTHON
import mysql.connector

conn = mysql.connector.connect(
    host="127.0.0.1",
    port=3307,
    user="root",
    password="my-secret-pw",
    database="my_app"
)

Node.js (mysql2):

JAVASCRIPT
const mysql = require('mysql2');

const connection = mysql.createConnection({
  host: '127.0.0.1',
  port: 3307,
  user: 'root',
  password: 'my-secret-pw',
  database: 'my_app'
});

Java (JDBC):

JAVA
String url = "jdbc:mysql://127.0.0.1:3307/my_app";

Go:

GO
dsn := "root:my-secret-pw@tcp(127.0.0.1:3307)/my_app"

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:

INI
# /etc/mysql/instance2.cnf
[mysqld]
port = 3307
datadir = /var/lib/mysql2
socket = /var/run/mysqld/mysqld2.sock
pid-file = /var/run/mysqld/mysqld2.pid

Start the second instance:

BASH
mysqld --defaults-file=/etc/mysql/instance2.cnf &

Connect to the second instance:

BASH
mysql --socket=/var/run/mysqld/mysqld2.sock -u root -p

Or by port:

BASH
mysql -h 127.0.0.1 -P 3307 -u root -p

Using mysqld_multi

MySQL provides mysqld_multi for managing multiple instances. Add sections to your config file:

INI
[mysqld_multi]
mysqld = /usr/bin/mysqld_safe
mysqladmin = /usr/bin/mysqladmin

[mysqld1]
port = 3306
datadir = /var/lib/mysql
socket = /var/run/mysqld/mysqld.sock

[mysqld2]
port = 3307
datadir = /var/lib/mysql2
socket = /var/run/mysqld/mysqld2.sock

Start all instances:

BASH
mysqld_multi start

Start a specific instance:

BASH
mysqld_multi start 2

Check the status:

BASH
mysqld_multi report

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:

INI
[mysqld]
port = 3307

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:

BASH
# Linux/macOS
sudo lsof -i :3306

# Windows
netstat -ano | findstr 3306

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:

BASH
sudo setcap 'cap_net_bind_service=+ep' /usr/sbin/mysqld

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:

BASH
sudo semanage port -a -t mysqld_port_t -p tcp 3307

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:

  1. bind-address: Make sure MySQL is not bound to 127.0.0.1. Set it to 0.0.0.0 or the specific IP of the network interface you want.

  2. Firewall: Confirm the port is open. Test with telnet or nc from the remote machine:

BASH
nc -zv your-server-ip 3306

If the connection is refused, the firewall is likely blocking it.

  1. MySQL user permissions: MySQL controls access by both username and host. A user created with 'user'@'localhost' cannot connect remotely. You need:
SQL
CREATE USER 'app_user'@'%' IDENTIFIED BY 'secure_password';
GRANT ALL PRIVILEGES ON my_app.* TO 'app_user'@'%';
FLUSH PRIVILEGES;

The % wildcard allows connections from any host. For better security, replace it with a specific IP or subnet:

SQL
CREATE USER 'app_user'@'192.168.1.%' IDENTIFIED BY 'secure_password';

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:

INI
[mysqld]
# Remove or comment out this line
# skip-networking

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:

BASH
mysql -h 127.0.0.1 -P 3306 -u root -p

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:

BASH
ssh -L 3306:127.0.0.1:3306 user@your-server

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:

BASH
ssh -L 3307:127.0.0.1:3306 user@your-server

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.