ssh 登陆mysql数据库_mysql命令行客户端如何通过ssh服务器连接数据库啊?

本文介绍了如何使用SSH隧道从192.168.41.72连接到192.168.41.83上的MySQL服务器。通过执行特定的SSH命令,设置本地端口3305来转发到远程MySQL的3306端口,确保数据传输的安全。确认隧道开启后,成功使用`mysql`命令行工具连接到数据库并验证了连接。
摘要由CSDN通过智能技术生成

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)

连接成功

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值