Connecting to MySQL via SSH Tunnel (Firstserv Guide)

Connecting directly to MySQL over port 3306 is not encrypted, which can pose a security risk. To ensure your database connection is secure, Firstserv recommends using an SSH tunnel.


What is an SSH Tunnel?

An SSH tunnel securely routes your database connection through your hosting account using encryption.

✅ Benefits

  • Encrypts all database traffic
  • Protects login credentials
  • Avoids exposing your database to the public internet

How It Works

  • You create a secure SSH connection to your server
  • This connection forwards your local port to the MySQL server
  • You connect locally (e.g. localhost), but traffic is securely routed to your hosting account

Connection Overview

Type Value
SSH Port 722
MySQL Port 3306
MySQL Host (local) localhost or 127.0.0.1

Requirements

Before you begin:

  • ✅ SSH access enabled on your Firstserv account
  • ✅ Your cPanel username and password
  • ✅ Your database username and password

Mac / Linux Setup (Terminal)


Step 1: Open Terminal

Use Spotlight or Applications → Utilities → Terminal


Step 2: Create the Tunnel

Run the following command:

 
 
 
 
 
 
Shell
 
 
ssh -p 722 -N -L 3306:localhost:3306 username@yourdomain.com
 
 

Replace Values

  • username → your cPanel username
  • yourdomain.com → your domain or server hostname

Example

 
 
 
 
 
 
Shell
 
 
ssh -p 722 -N -L 3306:localhost:3306 firstserv@server.example.com
 
 

Step 3: Enter Password

  • Enter your cPanel password when prompted
  • The terminal will remain open (this is normal)

✅ Your tunnel is now active


Optional: Run in Background

To run the tunnel in the background:

 
 
 
 
 
 
Shell
 
 
ssh -p 722 -f -N -L 3306:localhost:3306 username@yourdomain.com
 
 

⚠️ Port Conflict Note

If you already have MySQL running locally:

 
 
 
 
 
 
Shell
 
 
ssh -p 722 -N -L 3333:localhost:3306 username@yourdomain.com
 
 

👉 Then connect using:

localhost:3333

Windows Setup (PuTTY)


Step 1: Open PuTTY

  • Enter your:
    • Host Name (domain or server hostname)
    • Port: 722

Step 2: Configure Tunnel

  1. Go to:
    Connection → SSH → Tunnels
    
  2. Enter:
    • Source Port: 3306
    • Destination: localhost:3306
  3. Click Add

Step 3: Save (Optional)

  • Go back to Session
  • Enter a name under Saved Sessions
  • Click Save

Step 4: Connect

  • Click Open
  • Enter your cPanel password

✅ Keep the window open to maintain the tunnel


Connecting to MySQL

Once the tunnel is active, connect using your database tool (e.g. MySQL Workbench):

Host: localhost (or 127.0.0.1)
Port: 3306 (or your custom port)
Username: your MySQL user
Password: your MySQL password

✅ All traffic is now securely routed via SSH


Summary

  • Direct MySQL connections are not secure
  • SSH tunnels encrypt your database traffic
  • Use:
    SSH Port: 722
    MySQL Port: 3306
    
  • Connect locally while securely accessing your remote database

Need Help?

If you need help setting up an SSH tunnel or connecting your database securely, the Firstserv support team is always happy to assist.

 

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

Enable Redis on a Premium PCI-DSS compliant plan

Using Redis on Firstserv Hosting What is Redis? Redis (Remote Dictionary Server) is an...

Shell access to your server using SSH (PuTTY for Windows)

Accessing Your Server via SSH (Firstserv Guide) All Firstserv...

Enable or disable SSH (Secure Shell) access

Enabling SSH Access (Firstserv Guide) Firstserv provides secure,...

Connect to MySQL using MySQL Workbench

Connecting to MySQL via SSH Tunnel (Firstserv Guide – MySQL...

Block bad spiders to save bandwidth

Blocking Bad Bots & Search Crawlers (Firstserv Guide) Search...