Linux SSH隧道连接 远程mysql
By admin on 2023-06-07 14:06:07
建立隧道:
[root@10-255-0-218 dc2-user]# ssh -fCPN -L 3307:127.0.0.1:3306 -p 22 root@8.218.140.46 # 在本地打开一个SSH的守护进程,该进程会监听本地的3307端口,这个端口为隧道的入口,当访问本地的3307端口时,数据包会通过ssh进程,发送到8.218.140.46的3306端口; # -C 使用压缩功能,是可选的,加快速度; # -P 用一个非特权端口进行出去的连接; # -f 一旦SSH完成认证并建立port forwarding,则转入后台运行; # -N 不执行远程命令.该参数在只打开转发端口时很有用(V2版本SSH支持); The authenticity of host '116.85.57.94 (116.85.57.94)' can't be established. ECDSA key fingerprint is SHA256:lmwBveuC78QBzZBIEDPXgoNI0J58cT8u9k4A3Yh6+wQ. ECDSA key fingerprint is MD5:aa:34:00:e4:36:d3:a3:fd:08:4d:8f:62:01:50:6f:40. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '116.85.57.94' (ECDSA) to the list of known hosts. root@8.218.140.46's password: # 输入被远程主机8.218.140.46的主机密码;
连接数据库:
[root@10-255-0-218 dc2-user]# mysql -h 127.0.0.1 -P 3307 -u root -p # 通过本地3307端口连接MySQL数据库; Enter password: # 输入MySQL密码; Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 7581 Server version: 5.7.25 MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MySQL [(none)]> # 通过SSH隧道远程连接MySQL成功。