项目给了数据库的账号密码,目前用navicat通过ssh代理了才能连接,我想通过命令行也能实现连接。
直接利用ssh反向代理就可以了,参考我以前的一篇文章,里面就提到了这个需求。实际其他支持ssh的客户端也是利用这个: https://www.jianshu.com/p/edc...
ssh
到跳板机然后用 mysql
连接ssh
开启一个隧道我这里的MySQL
服务器是192.168.41.83
, 我要在192.168.41.72
连接
首先在192.168.41.72
执行命令开启隧道
[root@mysql-test-72 ~]# 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 ~]#
-f
: 完成连接后转入后台运行-N
: 不执行远程命令-o StrictHostKeyChecking=no
: 不提示是否要选择yes
3305
: 本地端口127.0.0.1:3306
: 远程机器端口检查端口状态
[root@mysql-test-72 ~]# 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
...
发现已经开启3305
端口,尝试连接
[root@mysql-test-72 ~]# 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)
连接成功
4 回答1.2k 阅读✓ 已解决
8 回答1.2k 阅读
3 回答1k 阅读✓ 已解决
2 回答1.7k 阅读
1 回答845 阅读✓ 已解决
2 回答1.2k 阅读
2 回答1k 阅读
你的意思是想在CMD里连接?下载个MySQL客户端吧