SSH ?

, /SSH MySQL/PostgreSQL/Redis

Solutions

(Recommended)Recommended

# MySQL( 3306 → 3307)
ssh -fNL 3307:localhost:3306 user@db-server
mysql -h 127.0.0.1 -P 3307 -u dbuser -p # PostgreSQL( 5432 → 5433)
ssh -fNL 5433:localhost:5432 user@db-server
psql -h 127.0.0.1 -p 5433 -U dbuser -d mydb # Redis( 6379 → 6380)
ssh -fNL 6380:localhost:6379 user@db-server
redis-cli -h 127.0.0.1 -p 6380

-f, -N shell, -L :., 127.0.0.1:. localhost SSH.

: and SSH, SSH

# db.internal:3306, bastion ssh -fNL 3307:db.internal:3306 user@bastion
mysql -h 127.0.0.1 -P 3307 -u dbuser -p # ~/.ssh/config # Host db-tunnel
# HostName bastion.example.com
# User jump-user
# LocalForward 3307 db.internal:3306
# LocalForward 5433 pg.internal:5432 # : ssh -fN db-tunnel

SSH,. SSH,.

:, /

autossh

# autossh
sudo apt install autossh # Debian/Ubuntu
brew install autossh # macOS # autossh autossh -M 0 -fNL 3307:localhost:3306 user@db-server \ -o ServerAliveInterval=30 \ -o ServerAliveCountMax=3 # systemd # /etc/systemd/system/ssh-tunnel-db.service
# [Unit]
# Description=SSH Tunnel to DB
# After=network.target
# [Service]
# ExecStart=/usr/bin/autossh -M 0 -NL 3307:localhost:3306 user@db-server
# Restart=always
# [Install]
# WantedBy=multi-user.target

autossh SSH,. -M 0 SSH. systemd and.

:,

, autossh systemd
and,
SSH TCP (AllowTcpForwarding yes)

Related Commands