localhost:3306

localhost:3306

Port 3306 is the default port for MySQL and MariaDB database servers. Unlike web servers, you don't access this port through a browser. Instead, database clients, applications, and tools like phpMyAdmin connect to port 3306 to communicate with your MySQL database.

What is Port 3306?

Port 3306 is the standard MySQL database communication port assigned by IANA (Internet Assigned Numbers Authority).

Services Using Port 3306

  • MySQL Server - World's most popular open-source database
  • MariaDB Server - MySQL fork and drop-in replacement
  • Percona Server - MySQL-compatible database server
  • MySQL Cluster - High-availability database cluster
  • Amazon RDS MySQL - Cloud-hosted MySQL instances
  • Google Cloud SQL - Managed MySQL service

Connect to MySQL on Port 3306

MySQL Command Line Client

# Connect to MySQL on localhost mysql -h localhost -P 3306 -u root -p # Shorthand (defaults to localhost:3306) mysql -u root -p # Connect to specific database mysql -h localhost -P 3306 -u root -p database_name # Connect with password in command (insecure, for scripts only) mysql -h localhost -P 3306 -u root -pYourPassword # After connection, enter password when prompted Enter password: ****

Check MySQL Status

# Windows (XAMPP) # Check XAMPP Control Panel - MySQL should show "Running" # Linux - Check MySQL service sudo systemctl status mysql sudo service mysql status # Check MariaDB sudo systemctl status mariadb # Mac - Check MySQL mysql.server status # Check if MySQL is listening on port 3306 # Windows netstat -ano | findstr :3306 # Linux/Mac sudo lsof -i :3306 netstat -an | grep 3306

Default MySQL Credentials

Software Username Password Host
XAMPP (Windows/Linux) root (blank/empty) localhost
WAMP (Windows) root (blank/empty) localhost
MAMP (Mac) root root localhost
Linux (Fresh Install) root (set during install) localhost
MySQL Workbench root (your password) 127.0.0.1

Database Connection Strings

PHP mysqli Connection

<?php // mysqli_connect(host, username, password, database, port) $conn = mysqli_connect("localhost", "root", "", "database_name", 3306); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "Connected successfully"; // Close connection mysqli_close($conn); ?>

PHP PDO Connection

<?php try { $dsn = "mysql:host=localhost;port=3306;dbname=database_name"; $username = "root"; $password = ""; $pdo = new PDO($dsn, $username, $password); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "Connected successfully"; } catch(PDOException $e) { echo "Connection failed: " . $e->getMessage(); } ?>

Node.js mysql2 Connection

// npm install mysql2 const mysql = require('mysql2'); const connection = mysql.createConnection({ host: 'localhost', port: 3306, user: 'root', password: '', database: 'database_name' }); connection.connect((err) => { if (err) { console.error('Error connecting:', err); return; } console.log('Connected to MySQL on port 3306'); }); // Execute query connection.query('SELECT * FROM users', (err, results) => { if (err) throw err; console.log(results); }); connection.end();

Python MySQL Connection

# pip install mysql-connector-python import mysql.connector try: connection = mysql.connector.connect( host='localhost', port=3306, user='root', password='', database='database_name' ) if connection.is_connected(): print('Connected to MySQL on port 3306') cursor = connection.cursor() cursor.execute('SELECT * FROM users') results = cursor.fetchall() for row in results: print(row) cursor.close() except mysql.connector.Error as e: print(f'Error: {e}') finally: if connection.is_connected(): connection.close()

Java JDBC Connection

// Add MySQL Connector/J to classpath import java.sql.*; public class MySQLConnection { public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/database_name"; String username = "root"; String password = ""; try { Connection conn = DriverManager.getConnection(url, username, password); System.out.println("Connected to MySQL on port 3306"); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM users"); while (rs.next()) { System.out.println(rs.getString("name")); } conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }

Fix "Can't connect to MySQL server on localhost:3306"

Error: Connection refused

MySQL server is not running.

  • Windows XAMPP: Start MySQL in XAMPP Control Panel
  • Linux: sudo systemctl start mysql
  • Mac: mysql.server start
  • Check if MySQL service is enabled at startup
  • Review MySQL error logs for startup failures
# Start MySQL service # Windows (XAMPP) # Use XAMPP Control Panel GUI # Linux - MySQL sudo systemctl start mysql sudo service mysql start # Linux - MariaDB sudo systemctl start mariadb # Mac - MySQL sudo mysql.server start brew services start mysql # Check if started successfully sudo systemctl status mysql

Error: Access denied for user 'root'@'localhost'

Wrong username or password.

  • Verify username is correct (usually "root")
  • Check password - XAMPP default is blank
  • MAMP default password is "root"
  • Password may have been changed previously
  • User may not have permission to access database
# Reset MySQL root password # Stop MySQL first sudo systemctl stop mysql # Start MySQL in safe mode (skip grant tables) sudo mysqld_safe --skip-grant-tables & # Connect without password mysql -u root # Change root password USE mysql; UPDATE user SET authentication_string=PASSWORD('newpassword') WHERE User='root'; FLUSH PRIVILEGES; EXIT; # Or use ALTER USER (MySQL 5.7+) ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword'; FLUSH PRIVILEGES; # Restart MySQL normally sudo systemctl stop mysql sudo systemctl start mysql # Test new password mysql -u root -p

Error: Port 3306 already in use

Another MySQL instance or application is using port 3306.

  • Check for multiple MySQL installations
  • Look for MySQL Windows Service running
  • Another database server on port 3306
  • Previous MySQL instance not shut down properly
# Find what's using port 3306 # Windows netstat -ano | findstr :3306 tasklist | findstr [PID] # Stop MySQL Windows Service net stop MySQL sc config MySQL start= disabled # Linux/Mac sudo lsof -i :3306 sudo netstat -tulpn | grep 3306 # Kill process using port 3306 sudo kill -9 [PID] # Stop MySQL service sudo systemctl stop mysql

Access MySQL via GUI Tools

phpMyAdmin

  • URL: http://localhost/phpmyadmin
  • Web-based MySQL management interface
  • Included with XAMPP, WAMP, MAMP
  • Connects to MySQL on port 3306 automatically

MySQL Workbench

# Download from mysql.com # Configure connection: # Connection Name: Local MySQL # Hostname: localhost or 127.0.0.1 # Port: 3306 # Username: root # Password: (your password or blank) # Test Connection button to verify

Other MySQL GUI Tools

  • HeidiSQL - Lightweight Windows MySQL client
  • DBeaver - Universal database tool (Windows/Linux/Mac)
  • TablePlus - Modern database client (Mac/Windows)
  • Sequel Pro - MySQL client for Mac
  • Adminer - Single-file phpMyAdmin alternative
  • DataGrip - JetBrains database IDE

Change MySQL Port from 3306

If port 3306 is occupied, change MySQL to different port:

Edit MySQL Configuration File

# Find my.cnf or my.ini file # Windows XAMPP: C:\xampp\mysql\bin\my.ini # Linux: /etc/mysql/my.cnf or /etc/my.cnf # Mac: /usr/local/etc/my.cnf # Edit configuration file sudo nano /etc/mysql/my.cnf # Find [mysqld] section and change port [mysqld] port = 3307 # Save and restart MySQL sudo systemctl restart mysql # Connect to new port mysql -h localhost -P 3307 -u root -p # Update application connection strings to use port 3307

Configure Firewall for Port 3306

Windows Firewall

# Allow MySQL port 3306 netsh advfirewall firewall add rule name="MySQL Port 3306" dir=in action=allow protocol=TCP localport=3306 # Remove firewall rule netsh advfirewall firewall delete rule name="MySQL Port 3306"

Linux Firewall (UFW)

# Allow MySQL port 3306 sudo ufw allow 3306/tcp # Allow from specific IP only sudo ufw allow from 192.168.1.0/24 to any port 3306 # Check firewall status sudo ufw status numbered # Remove rule sudo ufw delete allow 3306/tcp

Linux Firewall (firewalld)

# Allow MySQL service sudo firewall-cmd --permanent --add-service=mysql sudo firewall-cmd --reload # Or allow port directly sudo firewall-cmd --permanent --add-port=3306/tcp sudo firewall-cmd --reload # Check open ports sudo firewall-cmd --list-all

Test MySQL Port 3306 Connection

# Test with telnet telnet localhost 3306 # Test with netcat nc -zv localhost 3306 # Test with mysql client mysql -h localhost -P 3306 -u root -p # Windows PowerShell Test-NetConnection -ComputerName localhost -Port 3306 # Check if MySQL is listening # Linux sudo netstat -tulpn | grep :3306 sudo ss -tulpn | grep :3306 # Show MySQL process ps aux | grep mysql

MySQL Configuration File Locations

System Configuration File
XAMPP Windows C:\xampp\mysql\bin\my.ini
XAMPP Linux /opt/lampp/etc/my.cnf
Ubuntu/Debian /etc/mysql/my.cnf
CentOS/RHEL /etc/my.cnf
Mac (Homebrew) /usr/local/etc/my.cnf
MAMP Mac /Applications/MAMP/conf/my.cnf

MySQL Error Log Locations

# Find error log location mysql -u root -p -e "SHOW VARIABLES LIKE 'log_error';" # Common log locations: # XAMPP Windows C:\xampp\mysql\data\mysql_error.log # Linux /var/log/mysql/error.log /var/log/mysqld.log # Mac /usr/local/var/mysql/[hostname].err # View recent errors tail -f /var/log/mysql/error.log

Allow Remote Connections to Port 3306

By default, MySQL only accepts connections from localhost. To allow remote access:

Edit MySQL Configuration

# Edit my.cnf or my.ini sudo nano /etc/mysql/my.cnf # Find and comment out or change bind-address # bind-address = 127.0.0.1 bind-address = 0.0.0.0 # Save and restart MySQL sudo systemctl restart mysql

Grant Remote Access to User

# Connect to MySQL mysql -u root -p # Create user for remote access CREATE USER 'username'@'%' IDENTIFIED BY 'password'; # Grant privileges GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'%'; # Or grant from specific IP CREATE USER 'username'@'192.168.1.100' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'192.168.1.100'; # Apply changes FLUSH PRIVILEGES; EXIT; # Test remote connection mysql -h 192.168.1.100 -P 3306 -u username -p

Common MySQL Commands

# Show all databases SHOW DATABASES; # Create database CREATE DATABASE mydb; # Select database USE mydb; # Show tables in database SHOW TABLES; # Create table CREATE TABLE users ( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100), email VARCHAR(100) ); # Show table structure DESCRIBE users; # Insert data INSERT INTO users (name, email) VALUES ('John', 'john@example.com'); # Query data SELECT * FROM users; # Show MySQL version SELECT VERSION(); # Show current database SELECT DATABASE(); # Show current user SELECT USER(); # Show all users SELECT User, Host FROM mysql.user; # Exit MySQL EXIT;
Security Warning: Never expose MySQL port 3306 directly to the internet. Always use VPN, SSH tunneling, or restrict access by IP address. Change default root password immediately after installation.

MySQL vs MariaDB on Port 3306

Feature MySQL MariaDB
Default Port 3306 3306
Compatibility Oracle MySQL MySQL drop-in replacement
License GPL (community) / Commercial GPL (fully open source)
Performance Excellent Slightly better
Storage Engines InnoDB, MyISAM InnoDB, MyISAM, Aria, more
Default in Most hosting providers Debian, SUSE, Arch Linux

Frequently Asked Questions

Can I access MySQL port 3306 in browser?

No. Port 3306 is for MySQL protocol, not HTTP. Use phpMyAdmin (http://localhost/phpmyadmin) or MySQL Workbench to manage databases through GUI.

Why can't I connect to MySQL on port 3306?

Common reasons: MySQL not running, wrong credentials, firewall blocking port, MySQL bound to 127.0.0.1 only, or port 3306 used by another service.

Is it safe to expose port 3306 to internet?

No. Never expose MySQL directly to internet. Use SSH tunneling, VPN, or restrict access by IP. Use strong passwords and disable root remote access.

Can I run MySQL on different port?

Yes. Edit my.cnf configuration file and change port number. Remember to update all application connection strings to use new port.

What's the difference between localhost and 127.0.0.1 for MySQL?

On MySQL, localhost uses Unix socket connection (faster), while 127.0.0.1 uses TCP connection on port 3306. Both work but localhost is preferred for same-machine connections.

Related Ports and Resources