Connecting project from GitHub repository to standalone MySQL Database with global variables

gradill22
TRIAL

4 months ago

I have a basic Python project that uses a standalone MySQL database. I'm trying to connect my project to the database using global variables in os.environ, but I keep getting back NoneTypes and fail to connect to the database. So far, it has only complained about the MYSQLPORT variable. Either the other parameters are working fine, or none of them are. Am I on the right track? What am I missing?

I'm trying to connect to the database via mysql.connector. It looks like this:

try:
    return mysql.connector.connect(
        host=os.environ.get("MYSQLHOST"),
        user=os.environ.get("MYSQLUSER"),
        password=os.environ.get("MYSQLPASSWORD"),
        database=os.environ.get("MYSQLDATABASE"),
        port=os.environ.get("MYSQLPORT")  # this is the environ parameter that mysql.connector complains about being NoneType
    )
except Error as e:
    print("Error connecting to MySQL:", e)
    return None
Solved

0 Replies

gradill22
TRIAL

4 months ago

acd6714d-6cfd-4ef3-9958-d02205079708


4 months ago

The MYSQL db is in the same project as the python service, correct?


gradill22
TRIAL

4 months ago

Yes, and you got here just in time. I just figured it out.


4 months ago

the python service can’t access variables that aren’t in its service variables, you can see them in your service settings


4 months ago

nice!


gradill22
TRIAL

4 months ago

They are in the same environment. I can add variable references from the database to my project and it'll appear in the environment now.


4 months ago

you got it, marking this thread as solved!


4 months ago

!s


Status changed to Solved adam 4 months ago


Connecting project from GitHub repository to standalone MySQL Database with global variables - Railway Help Station