ubuntu uninx 命令行通过跳板机连接远程mysql

有三个机器:ubuntu本机A,跳板机B,mysql所在机器C。
A当前无法直接访问mysql所在机器。A可以连接B。B可以连接mysql数据库C。
希望A可以通过B访问C的数据库。

B的跳板机地址是117.78.4.212,用户名是userB。
在A上可以通过ssh userB@117.78.4.212连接上远程服务器B。
在服务器B上,执行ifconfig命令

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.34  netmask
        ...

即远程服务器B的ip是192.168.1.34

第三台机器C安装了数据库,此机器ip为192.168.200.216,数据库端口为3306,数据库用户名是userC_mysql,数据库密码是123456。

在ubuntu本机A上,执行以下命令

## 注释:1233是A上当前没有使用的端口。可以自定义。
$ local_port=1233 
$ board_machine_username=userB
$ board_machine_ip=117.78.4.212
$ remote_mysql_ip=192.168.200.216
$ remote_mysql_port=3306
$ remote_mysql_username="userC_mysql"
$ remote_mysql_password="123456"
$ ssh -p 22 -fNL ${local_port}:${remote_mysql_ip}:${remote_mysql_port} ${board_machine_username}@${board_machine_ip}
userB@117.78.4.212's password: 
$ mysql -h127.0.0.1 -P${local_port} -u${remote_mysql_username} -p${remote_mysql_password}
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 1404781028
Server version: 5.7.27-5-log 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)]> 

这样就已经连接上了C机器192.168.200.216的mysql数据库了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值