2

I have tried solutions found on other SO questions but none of them have worked for me. I am attempting to pull data from a mysql db running on a remote server by setting up an ssh tunnel. My code is as follows:

server = sshtunnel.SSHTunnelForwarder(
            ('10.6.41.10', 22),
            ssh_username= 'serveruser',
            ssh_password= 'serverpw',
            remote_bind_address=('127.0.0.1', 3306))

server.start()
print(server.local_bind_port)

cnx = mysql.connector.connect(user='root', password='mysqlpw',
                              host='127.0.0.1',
                              database='mydb',
                              charset='utf8',
                              use_unicode='FALSE',
                              port = 3306)

However, when I run this code I receive:

1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

I have also tried adding

local_bind_address = ('0.0.0.0', 3306)

to the sshtunnel setup and instead recieved

Problem setting SSH Forwarder up: Couldn't open tunnel 0.0.0.0:3306 <> 127.0.0.1:3306 might be in use or destination not reachable

I don't fully understand the remote_bind_address and local_bind_address, so my guess is that must be doing something wrong there. I know my username/pw/server info is correct, I am able to ssh into my server via terminal and then use

mysql -h 127.0.0.1 -u root -p

to successfully log into my mysql server. So what do I need to fix to get it running in python? Thanks.

1 Answer 1

5

If you don't specify local_bind_address in sshtunnel.SSHTunnelForwarder, the local port is allocated randomly. In that case set port=server.local_bind_port in mysql.connector.connect().

Instead, you can also set local_bind_address=('0.0.0.0', [some port which is not in use]) in sshtunnel.SSHTunnelForwarder. The sshtunnel.HandlerSSHTunnelForwarderError ("Problem setting...") tells you that you can't use local_bind_address=('0.0.0.0', 3306).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.