SSH隧道到MariaDB

时间:2018-04-06 03:06:05

标签: python ssh mariadb raspbian ssh-tunnel

我看了几个非常相似的例子,但我做错了什么......可能是因为我混淆了本地或远程绑定地址,或者不太确定。我还没有找到一份文件,可以描述每个人对我这样的新人的看法。

我在装有MariaDB的机器人中有一个覆盆子pi。我可以使用SQL Workbench从我的PC连接到服务器。

我有第二个需要python脚本的Pi,它可以将数据发送到第一个pi ...

IP地址,名称和密码已被更改,以保护无辜,但整个事情是一个封闭的网络无论如何。

import mysql.connector
import sshtunnel

_host = Robot's IP Address
_ssh_port = 22
_username = Robot user login
_password = Robot Password
_remote_bind_address = Robot's IP Address
_remote_mysql_port = 3308
_local_bind_address = Second Pi's IP Address
_local_mysql_port = 3308
_db_user = Database User Name
_db_password = Database Password
_db_name = "joycap"

with sshtunnel.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 = mysql.connector.connect(
                user=_db_user,
                passwd=_db_password,
                host=_local_bind_address,
                database=_db_name,
                port=_local_mysql_port)

这是我目前得到的错误......

pi@raspberrypi:~ $ python dbtest1.py
2018-04-05 06:11:42,262| ERROR   | Secsh channel 0 open FAILED: Connection refused: Connect failed
2018-04-05 06:11:42,277| ERROR   | Could not establish connection from ('Second Pi's IP Address', 3308) to remote side of the tunnel
Traceback (most recent call last):
  File "dbtest1.py", line 29, in <module>
    port=_local_mysql_port)
  File "/usr/local/lib/python2.7/dist-packages/mysql/connector/__init__.py", line 184, in connect
    return MySQLConnection(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/mysql/connector/connection.py", line 100, in __init__
    self.connect(**kwargs)
  File "/usr/local/lib/python2.7/dist-packages/mysql/connector/abstracts.py", line 733, in connect
    self._open_connection()
  File "/usr/local/lib/python2.7/dist-packages/mysql/connector/connection.py", line 241, in _open_connection
    self._do_handshake()
  File "/usr/local/lib/python2.7/dist-packages/mysql/connector/connection.py", line 108, in _do_handshake
    packet = self._socket.recv()
  File "/usr/local/lib/python2.7/dist-packages/mysql/connector/network.py", line 248, in recv_plain
    raise errors.InterfaceError(errno=2013)
mysql.connector.errors.InterfaceError: 2013: Lost connection to MySQL server during query

非常感谢任何建议!会认为有人会制作一个关于这样做的视频:)

此致 马特

1 个答案:

答案 0 :(得分:0)

最后通过反复试验解决了问题。

_host = "192.168.8.1"   #Server where database is hosted
_ssh_port = 22   #ssh port
_username = "pi"  #user name to host server
_password = "raspberry" #password to host server
_remote_bind_address = "127.0.0.1"  #local connection IP to db on host(should probably not change)
_remote_mysql_port = 3306
_local_bind_address = "0.0.0.0"  #local bind address(should probably not change)
_local_mysql_port = 3306
_db_user = "databaseuser"
_db_password = "databasepass"
_db_name = "databasename"