我有一个只能通过Jumphost访问的MySQL数据库。我们试图访问MySQL的拓扑如下
Client Host-> jumphost-> MySQL
使用以下命令通过SSL端口转发访问MySQL ssh -At -L 3306:MySQL:3306 user @ jumphost
一旦创建了隧道,就可以成功将客户端主机上运行的Workbench与MySQL连接,主机为localhost,端口为3306。
但是,如果我尝试通过mysql的pymysql模块使用相同的连接属性(主机:localhost,端口:3306)访问MySQL,则会发生以下错误 OperationalError:(2003年,“无法连接到'localhost'上的MySQL服务器([Errno 99]无法分配请求的地址)”)
代码详细信息:
import pymysql
import pandas
def getQueryResult(query):
host = 'localhost';
password = 'something';
database = 'test';
conn = pymysql.connect(host=host, port=int(3306), user="someuser", passwd=password, db=database);
df = pandas.read_sql_query(query, conn);
conn.close();
return df;
我们希望连接能够成功建立,但是跟踪却出现错误
OSErrorTraceback (most recent call last)
/opt/conda/lib/python3.6/site-packages/pymysql/connections.py in connect(self, sock)
957 (self.host, self.port), self.connect_timeout,
--> 958 **kwargs)
959 break
/opt/conda/lib/python3.6/socket.py in create_connection(address, timeout, source_address)
723 if err is not None:
--> 724 raise err
725 else:
/opt/conda/lib/python3.6/socket.py in create_connection(address, timeout, source_address)
712 sock.bind(source_address)
--> 713 sock.connect(sa)
714 # Break explicitly a reference cycle
OSError: [Errno 99] Cannot assign requested address
During handling of the above exception, another exception occurred:
我的代码调用堆栈:省略这些框架
/opt/conda/lib/python3.6/site-packages/pymysql/__init__.py in Connect(*args, **kwargs)
88 """
89 from .connections import Connection
---> 90 return Connection(*args, **kwargs)
91
92 from . import connections as _orig_conn
/opt/conda/lib/python3.6/site-packages/pymysql/connections.py in __init__(self, host, user, password, database, port, unix_socket, charset, sql_mode, read_default_file, conv, use_unicode, client_flag, cursorclass, init_command, connect_timeout, ssl, read_default_group, compress, named_pipe, no_delay, autocommit, db, passwd, local_infile, max_allowed_packet, defer_connect, auth_plugin_map, read_timeout, write_timeout, bind_address, binary_prefix)
702 self._sock = None
703 else:
--> 704 self.connect()
705
706 def _create_ssl_ctx(self, sslp):
/opt/conda/lib/python3.6/site-packages/pymysql/connections.py in connect(self, sock)
1003 exc.traceback = traceback.format_exc()
1004 if DEBUG: print(exc.traceback)
-> 1005 raise exc
1006
1007 # If e is neither DatabaseError or IOError, It's a bug.
OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 99] Cannot assign requested address)")
答案 0 :(得分:0)
也许您可以按照以下说明使用来自python的SSH隧道:https://medium.com/@amirziai/query-your-database-over-an-ssh-tunnel-with-pandas-603ce49b35a1