Heim  >  Artikel  >  Datenbank  >  MySQLWorkbench使用SSH Tunnel连接MySQL数据库

MySQLWorkbench使用SSH Tunnel连接MySQL数据库

WBOY
WBOYOriginal
2016-06-07 16:41:404058Durchsuche

在2010年用Amazon AWS时,就用过ssh命令来建立一个SSH Tunnel,然后本地配置为代理就可以无障碍访问互联网了。 现在连接一个IDC的MySQL服务器时,由于网络的隔离性,需要ssh到一台跳板机上才能直接访问MySQL,而我想在本地使用MySQLWorkbench这样的客户端直

在2010年用Amazon AWS时,就用过ssh命令来建立一个SSH Tunnel,然后本地配置为代理就可以无障碍访问互联网了。
现在连接一个IDC的MySQL服务器时,由于网络的隔离性,需要ssh到一台跳板机上才能直接访问MySQL,而我想在本地使用MySQLWorkbench这样的客户端直接连接到MySQL中。其解决方案是,先用ssh命令建立一个SSH Tunnel,然后MySQLWorkbench只需要连接本地的一个端口,即可与对应的远程MySQL建立连接。

假设跳板机为:192.168.1.1 (它可以与MySQL连通)
MySQL IP为:10.1.0.58 端口为:3306
建立SSH Tunnel的命令如下:

<span style="color: #666666; font-style: italic;"># 建立SSH Tunnel</span>
jay<span style="color: #000000; font-weight: bold;">@</span>Jay-Air:~ <span style="color: #007800;">$ssh</span> <span style="color: #660033;">-f</span> jay<span style="color: #000000; font-weight: bold;">@</span>192.168.1.1 <span style="color: #660033;">-L</span> <span style="color: #000000;">2001</span>:10.1.0.58:<span style="color: #000000;">3306</span> <span style="color: #660033;">-N</span>
Warning: Permanently added <span style="color: #ff0000;">'192.168.1.1'</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>RSA<span style="color: #7a0874; font-weight: bold;">&#41;</span> to the list of known hosts.
jay<span style="color: #000000; font-weight: bold;">@</span>192.168.1.1<span style="color: #ff0000;">'s password: 
 
# 查一下在后台运行的ssh进程
jay@Jay-Air:~ $ps -ef | grep '</span><span style="color: #c20cb9; font-weight: bold;">ssh</span> <span style="color: #660033;">-f</span><span style="color: #ff0000;">' | grep -v grep
  501  9729 1   0  6:10PM ??  0:00.19 ssh -f jay@192.168.1.1 -L 2001:10.1.0.58:3306 -N</span>

ssh命令中,-f 表示让ssh在执行命令(这里没有配置执行任何命令)之前进入后台运行状态; -L 指定本地的一个端口(如这里的2001),它将会被转发到远程IP的一个端口(如:10.1.0.58:3306);-N 表示不执行远程命令,对于只做端口转发的场景非常有用。

在MySQLWorkbench中,将原来连接MySQL的IP和端口,配置为设置的本地SSH Tunnel即可(这里设置的是:localhost:2001)

补充说一下,在MySQLWorkbench中一直使用是正常的,而在用mysql命令连接时遇到了问题,如下:

jay<span style="color: #000000; font-weight: bold;">@</span>jay-linux:~$ mysql  <span style="color: #660033;">-P</span> <span style="color: #000000;">2001</span> <span style="color: #660033;">-u</span> qa <span style="color: #660033;">-p</span>
Enter password: 
ERROR <span style="color: #000000;">1045</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">28000</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>: Access denied <span style="color: #000000; font-weight: bold;">for</span> user <span style="color: #ff0000;">'qa'</span><span style="color: #000000; font-weight: bold;">@</span><span style="color: #ff0000;">'localhost'</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>using password: YES<span style="color: #7a0874; font-weight: bold;">&#41;</span>

出现这个原因是,这样的命令默认连接localhost,会默认使用Unix socket方式连接;而使用SSH Tunnel这种情况应该使用TCP协议连接。
(mysql官网中的原文:For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. )
所以,解决方法有如下两种:
1. 在mysql命令中添加“–protocol=TCP”参数指定连接协议,命令为:mysql -P 2001 -u qa -p –protocol=TCP
2. 将用127.0.0.1 IP来代替默认的localhost,命令为:mysql -h 127.0.0.1 -P 2001 -u qa -p

另外,参考资料中的第二个,讲了如何在Windows下用putty设置建立SSH Tunnel。

参考资料:

http://www.revsys.com/writings/quicktips/ssh-tunnel.html

http://davidngos.blogspot.com/2013/06/how-to-use-mysql-workbench-behind.html

http://dev.mysql.com/doc/refman/5.6/en/connecting.html

Original article: MySQLWorkbench使用SSH Tunnel连接MySQL数据库

©2014 笑遍世界. All Rights Reserved.

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn