我试图通过python中的ssh连接到mysql数据库,但是收到错误.我在python文件'forward.py'中保存了以下内容.我的代码如下:
forward.py
from sshtunnel import SSHTunnelForwarder
import MySQLdb
with SSHTunnelForwarder(
('ssh_host', 22),
ssh_password="ssh_password",
ssh_username="ssh_username",
remote_bind_address=('mysql_host', 3306)) as server:
con = MySQLdb.connect(user='mysql_username',passwd='mysql_password',db='mysql_db_name',host='mysql_host',port=server.local_bind_port)
Run Code Online (Sandbox Code Playgroud)
当我在终端中运行forward.py时,收到以下错误:
_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on
'mysql_host' (60)")
Run Code Online (Sandbox Code Playgroud)
值得注意的是,这些值是安全性的硬编码值,即'mysql_host'是一个实际的主机,当我运行时我可以ssh到很好
ssh mysql_host
Run Code Online (Sandbox Code Playgroud)
通过终端.我也可以使用相同的值使用Sequel Pro进行连接,如下:http://screencast.com/t/0niuWlMDb
谁能指出我正确的方向?提前致谢