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.
Requirements
Section titled “Requirements”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
1. Generate an SSH Key
Section titled “1. Generate an SSH Key”On your local Mac or Linux computer, open Terminal and run:
ssh-keygen -t ed25519Press Enter to use the default key location:
~/.ssh/id_ed25519This creates:
Private key: ~/.ssh/id_ed25519Public key: ~/.ssh/id_ed25519.pubNever share or upload your private key.
2. Copy Your Public Key
Section titled “2. Copy Your Public Key”Run:
cat ~/.ssh/id_ed25519.pubCopy the complete output. It should start with:
ssh-ed255193. Import the Public Key into cPanel
Section titled “3. Import the Public Key into cPanel”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.
4. Authorize the SSH Key
Section titled “4. Authorize the SSH Key”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.
5. Test SSH Access
Section titled “5. Test SSH Access”From your local computer, run:
ssh CPANEL_USERNAME@SERVER_HOSTNAMEFor example:
ssh myuser@server.example.comIf you can successfully log in, your SSH key has been configured correctly.
6. Create the MySQL SSH Tunnel
Section titled “6. Create the MySQL SSH Tunnel”On your local computer, run:
ssh -fNL localhost:3307:localhost:3306 CPANEL_USERNAME@SERVER_HOSTNAMEFor example:
ssh -N -L 3307:localhost:3306 myuser@server.example.comKeep this Terminal window open while you are using the database.
The tunnel maps:
Your Computer:3307 ↓ SSH Tunnel ↓cPanel Server:3306 ↓ MySQL7. Configure Your MySQL Client
Section titled “7. Configure Your MySQL Client”Configure your application or database client with:
| Setting | Value |
|---|---|
| Host | localhost |
| Port | 3307 |
| Database | cpaneluser_database |
| Username | cpaneluser_dbuser |
| Password | Your MySQL database password |
Use localhost rather than the cPanel server hostname for the database connection.
8. Test the MySQL Connection
Section titled “8. Test the MySQL Connection”From another Terminal window, run:
mysql -h localhost -P 3307 -u cpaneluser_dbuser -pEnter your MySQL database password when prompted.
If the connection succeeds, your application can use the same host and port.
Prisma Example
Section titled “Prisma Example”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.
Important Notes
Section titled “Important Notes”- Keep the SSH tunnel running while your application is connected to MySQL.
- Do not expose MySQL port
3306publicly 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
3307is already being used, choose another unused port such as3308.
Connection Flow
Section titled “Connection Flow”Local Application ↓localhost:3307 ↓ SSH Tunnel ↓ cPanel Server ↓localhost:3306 ↓ MySQL