有三个机器: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数据库了。