How does the mysql command line client connect to the database through the ssh server?

The

project gives the account password of the database. At present, you can only connect through the ssh proxy using navicat. I think the connection can also be achieved through the command line.

Feb.14,2022

you mean you want to connect in CMD? Download a MySQL client


just take advantage of the ssh reverse proxy. Refer to my previous article, which mentioned this requirement. In fact, other clients that support ssh also take advantage of this: https://www.jianshu.com/p/edc.


  1. ssh to the springboard machine and then use mysql to connect
  2. Open a tunnel with ssh

my MySQL server is 192.168.41.83 . I want to connect

at 192.168.41.72 .

first open the tunnel in 192.168.41.72 execute the command

[root@mysql-test-72 ~]-sharp ssh -NPf -o StrictHostKeyChecking=no root@192.168.41.83 -L 3305:127.0.0.1:3306
root@192.168.41.83's password: 
[root@mysql-test-72 ~]-sharp 
  • -f : run in the background after completing the connection
  • -N : do not execute remote commands
  • -o StrictHostKeyChecking=no : do not prompt whether to select yes
  • 3305 : local port
  • 127.0.0.1 li 3306 : remote machine port < / machine >

check port status

[root@mysql-test-72 ~]-sharp netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 192.168.41.72:9991          0.0.0.0:*                   LISTEN      20409/mmm_agentd-te 
tcp        0      0 127.0.0.1:3305              0.0.0.0:*                   LISTEN      28339/ssh
...

found that the 3305 port is open, try to connect

[root@mysql-test-72 ~]-sharp mysql -umytest -p -P3305 -h127.0.0.1
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 698103
Server version: 5.7.21-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show variables like 'hostname';
+---------------+---------------+
| Variable_name | Value         |
+---------------+---------------+
| hostname      | mysql-test-83 |
+---------------+---------------+
1 row in set (0.01 sec)

connection succeeded

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b43823-2bbe5.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b43823-2bbe5.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?