0

So I followed the given code here:

import mysql.connector
import sshtunnel

sshtunnel.SSH_TIMEOUT = 5.0
sshtunnel.TUNNEL_TIMEOUT = 5.0

with sshtunnel.SSHTunnelForwarder(
    ('your SSH hostname'),
    ssh_username='your PythonAnywhere username', ssh_password='the password you use to log in to the PythonAnywhere website',
    remote_bind_address=('your PythonAnywhere database hostname, eg. yourusername.mysql.pythonanywhere-services.com', 3306)
) as tunnel:
    connection = mysql.connector.connect(
        user='your PythonAnywhere username', password='your PythonAnywhere database password',
        host='127.0.0.1', port=tunnel.local_bind_port,
        database='your database name, eg yourusername$mydatabase',
    )
    # Do stuff
    connection.close()

I basically just want to do some very simple things now that I am connected to the database. I would like to just print out the contents of a table. When I try to do this, my code looks like (I filled in the needed data, it is not the template anymore):

import mysql.connector
import sshtunnel

sshtunnel.SSH_TIMEOUT = 5.0
sshtunnel.TUNNEL_TIMEOUT = 5.0

with sshtunnel.SSHTunnelForwarder(
    ('your SSH hostname'),
    ssh_username='your PythonAnywhere username', ssh_password='the password you use to log in to the PythonAnywhere website',
    remote_bind_address=('your PythonAnywhere database hostname, eg. yourusername.mysql.pythonanywhere-services.com', 3306)
) as tunnel:
    connection = mysql.connector.connect(
        user='your PythonAnywhere username', password='your PythonAnywhere database password',
        host='127.0.0.1', port=tunnel.local_bind_port,
        database='your database name, eg yourusername$mydatabase',
    )
    # Do stuff
    stuff = connection.cursor()
    stuff.execute("select * from Semesters")

    for i in stuff:
        print(i)


    connection.close()

My code compiles fine without my code under "do stuff" comment. When I added that code, I get the error:

Traceback (most recent call last):
  File "C:\Users\Bobby\python_datebase\database_connect.py", line 17, in <module>
    stuff = connection.cursor()
  File "E:\Python 3.9\lib\site-packages\mysql\connector\connection.py", line 809, in cursor
    raise errors.OperationalError("MySQL Connection not available.")
mysql.connector.errors.OperationalError: MySQL Connection not available.

Any help on what I am doing wrong or an alternative way to do this would be awesome. Thanks!

  • Were you checking if you are able to open just ssh connection to your PythonAnywhere account with the details you use in the tunnel? – Filip Feb 20 '21 at 12:15
  • I believe that I was able to connect. I now just want to run a simple function using the mysql.connector library. I would like to just show the data in a table from my python script but I'm unsure how. – BoobooSparky Feb 20 '21 at 22:18

1 Answers1

0

You need to give it more parameters, such as port number and db name. this code is relevant if you have mysql installed and configured and you need to configure it on the code itself. example: port=3306 database="the db name you configured on mysql. for more info visit this site https://dev.mysql.com/doc/connector-python/en/connector-python-example-connecting.html

shv
  • 47
  • 1
  • 9
  • I did this, I just did not put my own inputs in so my data was not on the internet. – BoobooSparky Feb 19 '21 at 06:20
  • I would consider to make mysql to listen to any connection for testing it. for example listen to 0.0.0.0/0 – shv Feb 19 '21 at 06:53
  • try following this link. https://stackoverflow.com/questions/27537892/cursor-raise-errors-operationalerrormysql-connection-not-available-operat/44577753 I would write down more info, but I have a little kid shouting on me while writing it down – shv Feb 19 '21 at 06:55