Skip to content

Connect to cPanel MySQL Remotely via SSH Tunnel

Connect to cPanel MySQL Remotely via SSH Tunnel

Section titled “Connect to cPanel MySQL Remotely via SSH Tunnel”

This guide explains how to securely connect to a MySQL database hosted on a cPanel server from your local computer using an SSH tunnel.

Using an SSH tunnel means you do not need to expose MySQL port 3306 publicly.

Before starting, make sure you have:

  • SSH access enabled for the cPanel account
  • cPanel username
  • Server hostname or IP address
  • MySQL database name
  • MySQL database username
  • MySQL database password

On your local Mac or Linux computer, open Terminal and run:

Terminal window
ssh-keygen -t ed25519

Press Enter to use the default key location:

~/.ssh/id_ed25519

This creates:

Private key: ~/.ssh/id_ed25519
Public key: ~/.ssh/id_ed25519.pub

Never share or upload your private key.

Run:

Terminal window
cat ~/.ssh/id_ed25519.pub

Copy the complete output. It should start with:

ssh-ed25519

Log in to cPanel and navigate to:

Security → SSH Access → Manage SSH Keys → Import Key

Enter the following:

  • Name: id_ed25519
  • Public Key: Paste the public key copied in the previous step
  • Private Key: Leave blank
  • Passphrase: Leave blank

Click Import.

Go back to:

SSH Access → Manage SSH Keys

Find the imported key under Public Keys.

Click:

Manage → Authorize

The key must be authorized before it can be used to log in through SSH.

From your local computer, run:

Terminal window
ssh CPANEL_USERNAME@SERVER_HOSTNAME

For example:

Terminal window
ssh myuser@server.example.com

If you can successfully log in, your SSH key has been configured correctly.

On your local computer, run:

Terminal window
ssh -fNL localhost:3307:localhost:3306 CPANEL_USERNAME@SERVER_HOSTNAME

For example:

Terminal window
ssh -N -L 3307:localhost:3306 myuser@server.example.com

Keep this Terminal window open while you are using the database.

The tunnel maps:

Your Computer:3307
SSH Tunnel
cPanel Server:3306
MySQL

Configure your application or database client with:

SettingValue
Hostlocalhost
Port3307
Databasecpaneluser_database
Usernamecpaneluser_dbuser
PasswordYour MySQL database password

Use localhost rather than the cPanel server hostname for the database connection.

From another Terminal window, run:

Terminal window
mysql -h localhost -P 3307 -u cpaneluser_dbuser -p

Enter your MySQL database password when prompted.

If the connection succeeds, your application can use the same host and port.

For Prisma, your connection string would look like:

DATABASE_URL="mysql://cpaneluser_dbuser:PASSWORD@localhost:3307/cpaneluser_database"

Replace the username, password, and database name with your actual credentials.

  • Keep the SSH tunnel running while your application is connected to MySQL.
  • Do not expose MySQL port 3306 publicly just for remote development.
  • You normally do not need to whitelist your local IP under Remote MySQL in cPanel when connecting through the SSH tunnel.
  • The SSH credentials and MySQL database credentials are separate.
  • If local port 3307 is already being used, choose another unused port such as 3308.
Local Application
localhost:3307
SSH Tunnel
cPanel Server
localhost:3306
MySQL