2017-05-23 4 views
0

Ich versuche sshtunnel und MySQLdb in python2.7 zu Tunnel verwenden, um meine Verbindung und folgende Problem:Python verwendet sshtunnel nicht mysql verbinden

habe ich Sequel Pro mysql war in Ordnung, aber die Python-Code zu verbinden funktioniert nicht!

Sequel Pro ist wie folgt:

connect info

und Code ist wie folgt:

`from sshtunnel import SSHTunnelForwarder 
import MySQLdb 
with SSHTunnelForwarder(
     ('2.2.2.2', 22), 
     ssh_username='name2', 
     ssh_password='mypassword', 
     remote_bind_address=('127.0.0.1', 3306) 
) as tunnel: 
    connection = MySQLdb.connect(
    user='name1', 
    password='mypassword', 
    host='1.1.1.1', 
    database='mydata', 
    port=3306) 

ich einige Beispiel-Code wie gesucht:

from sshtunnel import SSHTunnelForwarder 
import MySQLdb 

with SSHTunnelForwarder(
     (_host, _ssh_port), 
     ssh_username=_username, 
     ssh_password=_password, 
     remote_bind_address=(_remote_bind_address, _remote_mysql_port), 
     local_bind_address=(_local_bind_address, _local_mysql_port) 
) as tunnel: 
    connection = MySQLdb.connect(
    user=_db_user, 
    password=_db_password, 
    host=_local_bind_address, 
    database=_db_name, 
    port=_local_mysql_port) 

Ich möchte wissen, das habe ich richtig gemacht, um ssh zu bauen oder mit mysql zu verbinden? Danke für die Hilfe!

Update: die Fehlermeldung:

2017-06-15 17:52:58,415| ERROR | Could not connect to gateway 1.1.1.1:22 : 110 
Traceback (most recent call last): 
    File "<stdin>", line 6, in <module> 
    File "build/bdist.linux-x86_64/egg/sshtunnel.py", line 1483, in __enter__ 
    File "build/bdist.linux-x86_64/egg/sshtunnel.py", line 1225, in start 
    File "build/bdist.linux-x86_64/egg/sshtunnel.py", line 1037, in _raise 
sshtunnel.BaseSSHTunnelForwarderError: Could not establish session to SSH gateway 

Antwort

1

Aktualisieren Sie Ihren Code, um die local_bind_address hinzuzufügen:

from sshtunnel import SSHTunnelForwarder 
import MySQLdb 
with SSHTunnelForwarder(
     ('2.2.2.2', 22), 
     ssh_username='name2', 
     ssh_password='mypassword', 
     remote_bind_address=('127.0.0.1', 3306), 
     local_bind_address = ('1.1.1.1', 3306) 
) as tunnel: 
    connection = MySQLdb.connect(
    user='name1', 
    password='mypassword', 
    host='1.1.1.1', 
    database='mydata', 
    port=3306) 

Sofern Sie Ihre Localhost angeben (als '1.1.1.1' in Ihrem Fall) & Hafen; Der MYSQL-Dienst ist für den lokalen Host nicht verfügbar. Ich hoffe, das hilft.

+0

Danke für Ihre Hilfe! Aber es funktioniert immer noch nicht. – chilun

+0

irgendwelche Fehler? Mitteilungen ? –

+0

Ich habe die Fehlermeldung im Artikel hinzugefügt. Danke. – chilun