Tutorial
Connect to MySQL through an SSH tunnel
Forward a local port to MySQL with ssh -L and connect to 127.0.0.1 on that port. Two things catch almost everyone: the mysql client treats localhost as a socket file, and MySQL accounts are tied to the host the server sees.
- Level
- Intermediate
- Reading time
- 9 min
- Applies to
- Windows, macOS, Linux, OpenSSH command line
- Transputty steps checked against
- Transputty 0.1 (development build, not released)
Before you start
- An SSH login on the database host or on a host that can reach it, for example deploy@bastion.example.com
- The MySQL address as that SSH host sees it, for example 127.0.0.1:3306 or db.internal:3306
- A MySQL account whose host part matches where the SSH host connects from (explained below)
- The mysql command-line client or another MySQL client on your computer
Short answer
To connect to MySQL through an SSH tunnel, run ssh -N -L 3307:127.0.0.1:3306 deploy@db.example.com and keep it running, then connect with mysql -h 127.0.0.1 -P 3307 --protocol=TCP -u app -p. Use 127.0.0.1, not localhost: on Linux and macOS the client treats localhost as a socket file and never reaches the tunnel. The same steps apply to MariaDB.
The last section shows this tunnel in Transputty, an SSH tunnel manager for Windows 11 that is in development with no public build yet (see release status). Everything before it uses plain ssh and works today. Never made a tunnel before? Do your first SSH tunnel on Windows first.
Why does MySQL need a tunnel?
MySQL listens on TCP port 3306, and most servers do not expose it. The bind-address setting decides which interfaces accept connections, and many distributions ship it as 127.0.0.1, which means only programs on the database machine itself can connect. Where it is wider, a firewall usually limits 3306 to application servers.
An SSH tunnel fits that setup without changing it. Your client connects to a port on your own computer; the SSH server opens the real connection from its side. This is local port forwarding. Everything on this page is also true for MariaDB, which shares the protocol, the port and the client options.
What does the path look like?
your computer db.example.com (sshd and mysqld on one host)
mysql -> 127.0.0.1:3307 ==== SSH (encrypted, port 22) ====> sshd ---> 127.0.0.1:3306 mysqld
(ssh listens here)
With a separate bastion (a jump host: the one server you can SSH into, which can reach the private machines) the last arrow leaves the SSH host and crosses the private network to db.internal:3306. That hop is not covered by SSH.
The command
MySQL on the same machine as the SSH server, the most common case:
ssh -N -L 3307:127.0.0.1:3306 deploy@db.example.com
MySQL on another machine, reached through a bastion:
ssh -N -L 3307:db.internal:3306 deploy@bastion.example.com
The middle part of -L is resolved and dialled by the SSH server. In the first command 127.0.0.1 is therefore the database host's own loopback address (the one a computer uses to reach itself), which is exactly where a server with bind-address = 127.0.0.1 listens. In the second, mysqld on db.internal must be listening on an address the bastion can reach, so bind-address = 127.0.0.1 there will not work.
-N starts no shell: the command sits silently while the tunnel is up. Local port 3307 avoids a clash with a MySQL or MariaDB server on your own computer. The local port forwarding tutorial covers the syntax in detail.
Connect with the mysql client
mysql -h 127.0.0.1 -P 3307 --protocol=TCP -u app -p
Once connected, the status command confirms the route. Look for these two lines:
Connection: 127.0.0.1 via TCP/IP
TCP port: 3307
The localhost trap
On Unix-like systems the MySQL client gives the name localhost a special meaning: connect through the local Unix socket file. The port option is ignored. So this command does not use your tunnel at all:
mysql -h localhost -P 3307 -u app -p
It either reaches a MySQL server installed on your own machine or fails with error 2002 naming a socket path. Write -h 127.0.0.1, or add --protocol=TCP, or both. On Windows the client uses TCP for localhost, so the trap does not appear there, but 127.0.0.1 is correct everywhere and worth the habit. Connection libraries built on the MySQL C client inherit the same rule.
Which account will MySQL match?
A MySQL account is a user name plus a host, and the server compares the host part with the address the connection arrives from. Through a tunnel that is the SSH server, never your computer.
| Layout | Address mysqld sees | Account that matches |
|---|---|---|
Forward to 127.0.0.1:3306 on the database host |
127.0.0.1, resolved to localhost |
'app'@'localhost' |
Same, with skip_name_resolve = ON |
127.0.0.1, not resolved |
'app'@'127.0.0.1' |
Forward from a bastion to db.internal:3306 |
The bastion's private address | 'app'@'10.0.1.5' or 'app'@'10.0.1.%' |
The middle row deserves attention because many managed and tuned servers enable skip_name_resolve. A server that only has 'app'@'localhost' then rejects the tunnelled TCP connection, even though the same account works from a shell on the server (where the client uses the socket). The message is:
ERROR 1130 (HY000): Host '127.0.0.1' is not allowed to connect to this MySQL server
The fix is an account for the address in the message, for example CREATE USER 'app'@'127.0.0.1' IDENTIFIED BY '…'; with the same grants. To see which account you actually got, run SELECT CURRENT_USER();.
TLS through the tunnel
MySQL 8 clients default to --ssl-mode=PREFERRED: encrypt if the server offers it, verify nothing. That works through a tunnel as is, but neither PREFERRED nor REQUIRED proves that the server is the one you meant. --ssl-mode=VERIFY_IDENTITY compares the certificate with the host you connected to, which is 127.0.0.1, so it fails through a tunnel unless the certificate lists that address. --ssl-mode=VERIFY_CA --ssl-ca=ca.pem checks the certificate chain without the name and is the practical strict setting here. Keep encryption on when a bastion sits between you and the database, because the hop from the bastion to MySQL travels outside SSH.
GUI clients and MySQL Workbench
Any client works with Host 127.0.0.1, Port 3307, your user and password, and its own SSH option switched off.
MySQL Workbench can also build the tunnel itself: choose the connection method Standard TCP/IP over SSH and fill in the SSH host and user plus the MySQL host as seen from the SSH server. Other clients, including SQLPro Studio, DBeaver and DataGrip, have a similar option. A separate tunnel remains the better tool when several programs share one forwarded port, when it must survive the client being closed, or when the route needs a chain of jump hosts.
The same tunnel in Transputty
Transputty stores the forward as a connection and keeps the local port bound while it reconnects. These steps follow version 0.1, a development build.
- Press Ctrl+N (or click +). In the "New connection" dialog, under Start from a preset, pick MySQL. The connection is named
mysql-tunneland gets one "Local (-L)" rule from127.0.0.1:3306tolocalhost:3306. - If 3306 is already used on your PC, Transputty picks the next free port and shows a note such as "3306 is in use by mysqld.exe (PID 5120), so Transputty picked 3307."
- Under SSH server fill in Host, Port and User, and choose a method under Authentication.
- Under Forwarding rules, set the Port next to Listen address to
3307to match this page. Leave Destination host aslocalhostwhen MySQL runs on the SSH server, or typedb.internal. - Click Save & connect and, on the first connection, compare the host key fingerprint and choose Trust and connect. If you chose Password, Transputty then asks for it.
- Connect your client to
127.0.0.1and port3307.
The preset fills in ports and nothing else; there is no MySQL-specific behaviour. One difference from the commands above: the preset's destination is localhost, which the SSH server resolves itself. If MySQL is bound to 127.0.0.1 only and the connection is refused, type 127.0.0.1 as the Destination host.
Troubleshooting
| Error | What it means | What to do |
|---|---|---|
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) |
The client used the Unix socket because the host was localhost or missing. It never tried the tunnel |
Use -h 127.0.0.1 --protocol=TCP |
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1:3307' (10061) |
Nothing listens on the local port (10061 on Windows, 111 on Linux, 61 on macOS). The tunnel is not running or the port is wrong | SSH tunnel connection refused |
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0 |
The tunnel accepted the connection but the SSH server could not reach MySQL: wrong destination, bind-address on another interface, or forwarding disabled on the server |
Check the ssh window for channel N: open failed, then connection refused |
ERROR 1045 (28000): Access denied for user 'app'@'localhost' (using password: YES) |
The tunnel works and MySQL answered. Wrong password, or the account matched is not the one you expected. The host in the message is what the server saw | Check the password and the account's host part |
ERROR 1130 (HY000): Host '…' is not allowed to connect to this MySQL server |
No account exists for the address the SSH server connects from | Create the account for that host |
ERROR 2013 in the middle of a session |
The SSH connection dropped and took the MySQL connection with it | SSH tunnel keeps disconnecting |
Common questions
Why does mysql -h localhost ignore my SSH tunnel?
On Linux and macOS the mysql client treats the host name localhost as a request for the local Unix socket file and ignores the port, so it never reaches the tunnel. Use -h 127.0.0.1, or add --protocol=TCP, to force a TCP connection to the forwarded port. On Windows, localhost uses TCP.
Which MySQL user host do I need for an SSH tunnel?
MySQL matches the account against the address the server sees, which is the SSH host. When the forward targets 127.0.0.1 on the database host itself, an account such as 'app'@'localhost' normally matches; with skip_name_resolve enabled it must be 'app'@'127.0.0.1'. When the database is on another machine, the host part must match the bastion's address.
Does an SSH tunnel work with MariaDB?
Yes. MariaDB uses the same wire protocol, the same default port 3306 and the same client options for host, port and protocol, so the ssh command and the localhost caveat are identical. The mariadb client's error texts differ slightly from MySQL's, and the steps on this page were run against MySQL 8.4, not MariaDB.
What local port should I use for a MySQL tunnel?
Any free port. 3307 is a common choice because a MySQL or MariaDB server installed on your own computer already uses 3306. The remote end of the forward stays 3306. Tell your client to connect to 127.0.0.1 on the local port you picked.
Does MySQL Workbench need a separate SSH tunnel?
No. MySQL Workbench has a connection method called Standard TCP/IP over SSH that opens the tunnel itself. A separate tunnel is useful when other tools need the same connection, when it should stay up while Workbench restarts, or when the route goes through more than one SSH host.
How this tutorial was checked
2026-11-26: The ssh and mysql steps were run on Windows 11 with the built-in client (OpenSSH_for_Windows 9.5p2) against a real OpenSSH server (OpenSSH for Windows 10.4p1) and a throwaway MySQL Community Server 8.4.7 with bind-address=127.0.0.1, all on one PC. Run through the tunnel with the mysql 8.4.7 client: a login as 'app'@'localhost' with current_user() and the status command, --ssl-mode=VERIFY_CA succeeding and VERIFY_IDENTITY failing, and errors 2003, 1045, 2013 and 1130, quoted as printed. Not run: MariaDB, the Unix socket behaviour of -h localhost and error 2002 (the test PC ran Windows; these follow the MySQL manual), a separate bastion, MySQL Workbench and a Linux SSH server. The Transputty steps were written from the 0.1 source code and were not clicked through in the app.
Sources and further reading
- ssh(1) manual page, OpenBSD (the -L and -N options)
- MySQL 8.4 Reference Manual: Connecting to the MySQL Server Using Command Options (localhost and sockets)
- MySQL 8.4 Reference Manual: Connection Transport Protocols
- MySQL 8.4 Reference Manual: Specifying Account Names
- MySQL 8.4 Reference Manual: Troubleshooting Problems Connecting to MySQL
- MySQL Workbench Manual: Standard TCP/IP over SSH Connection Method
Transputty keeps SSH tunnels running on Windows
Local, remote and SOCKS5 forwards in one window, automatic reconnection with ports that stay bound, and a clear reason when something fails. In development; no public build yet.