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
0 Replies
4 months ago
The MYSQL db is in the same project as the python service, correct?
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!
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