sshtunnel connect failed towards mysql container on NAS drive

25 Views Asked by At

I am trying to access remotely from my raspberry pi weather station towards my mySQL DB on a NAS drive. For this I am using SSHTunnelForwarder and I have found several small python code examples in this forum but it seems that I miss something because I am for now getting:

pi@raspberrypi:~/enviroplus-python/examples $ python3 sshtunnel_try.py
2024-03-18 20:34:51,390| ERROR   | Secsh channel 0 open FAILED: Connection refused: Connect failed
2024-03-18 20:34:51,396| ERROR   | Could not establish connection from local ('127.0.0.1', 34955) to remote ('hostname-mysql.at.remote.it', 3306) side of the tunnel: open new channel ssh error: ChannelException(2, 'Connect failed')
{('0.0.0.0', 34955): False}
34955
2024-03-18 20:34:51,474| ERROR   | Secsh channel 1 open FAILED: Connection refused: Connect failed
2024-03-18 20:34:51,480| ERROR   | Could not establish connection from local ('127.0.0.1', 34955) to remote ('hostname-mysql.at.remote.it', 3306) side of the tunnel: open new channel ssh error: ChannelException(2, 'Connect failed')
Keine Verbindung zum Server...(2013, "Lost connection to MySQL server at 'handshake: reading initial communication packet', system error: 11")

my python code for now is:

import MySQLdb

from datetime import datetime
from sshtunnel import SSHTunnelForwarder

server = SSHTunnelForwarder(
    ('<hostname-ssh.at.remote.it>'),
    ssh_username="pi",
    ssh_password="raspberry",
    remote_bind_address=("<hostname-mysql.at.remote.it>", 3306), # or localhost?
    threaded=False)

if __name__ == '__main__':
    server.start()
    server.check_tunnels()
    print(server.tunnel_is_up, flush=True)
    print(server.local_bind_port)  # show assigned local port

    try:
        db = MySQLdb.connect(
            host="127.0.0.1", # or localhost?
            port=server.local_bind_port,
            user="root",
            password="password",
            database="weatherDB")
        print("Connected to:", db.get_server_info())
        db_cursor = db.cursor(dictionary=True)
        print("Do nothing")
    except Exception as e:
        print(f"No connection to Server...{str(e)}")
    finally:
        if "cursor" in locals() and db_cursor:
            db_cursor.close()
        if "db" in locals() and db:
            db.close()
        if "tunnel" in locals() and server:
            server.close()

When the raspberry pi is connected to my local network (via WiFi) another python script can access the mySQL DB for inserting and querying information. When I move the raspberry pi to another WiFi hotspot outside my local network then I can't even make a connection to the mySQL DB.

Now on my NAS drive I have docker with mySQL container and other like Grafana...and I can access the the NAS drive via remote.it on ssh, example:

$  ssh -v -L 33002:127.0.0.1:33002 <hostname>-ssh.at.remote.it

On remote.it I have setup two services one for ssh towards NAS drive and one towards mySQL:

<hostname>-ssh.at.remote.it:33002
<hostname>-mysql.at.remote.it:33006

But for the moment I have not managed to get it working.

0

There are 0 best solutions below