Cant get database to work with my flask application
clueeng
FREEOP

a month ago

Hi there, this is probably redundant, but i'll shoot my shot anyway

I have deployed my flask app successfully, and i made a new mysql database service, managed to log into it and load my sql file into the database, and everything looks fine. But when I try a simple login (only SQL-related request i can make when logged out of an account) I get an internal server error, and no weird log in the console, neither the railway one or the client one (just some 500 on the client)

response of the login post request

<!doctype html>

<html lang=en>

<title>500 Internal Server Error</title>

<h1>Internal Server Error</h1>

<p>The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.</p>

I have environment variables to connect to the database in flask that includes

Host, user, password, port, database(name)

I have changed them to the ones from the database variables, and it still does not work and I do not know why, here's what i have input

DATABASE=${{MySQL.MYSQL_DATABASE}}

DB_PORT=${{MySQL.MYSQLPORT}}

HOST=${{MySQL.MYSQLHOST}}

LOGIN=${{MySQL.MYSQLUSER}}

PASSWORD=${{MySQL.MySQL.MYSQL_ROOT_PASSWORD}}

and i use them this way in my flask app

db = g._database = pymysql.connect(

host=os.environ.get("HOST"),

user=os.environ.get("LOGIN"),

password=os.environ.get("PASSWORD"),

port=os.environ.get("DB_PORT"),

database=os.environ.get("DATABASE"),

charset='utf8mb4',

cursorclass=pymysql.cursors.DictCursor

)

I tried to cast port as an integer, but it doesn't do anything, and I guess it would make sense since it worked locally without doing that.
I cannot use any other library, it is part of an assignment

Moreover logs seem to be non existent for some reason ? They showed up yesterday but today it won't show i dont know why

It used to at least send the flask logs, but today I can't see anything

$10 Bounty

9 Replies

a month ago

which db are you using?


oxtrizzz

which db are you using?

MySQL… (it’s in the thread…)


Something I’d try is to just start putting print statements everywhere in the code to help trace where went wrong.


0x5b62656e5d

MySQL… (it’s in the thread…)

a month ago

try using sqlalchemy if u using python


0x5b62656e5d

Something I’d try is to just start putting print statements everywhere in the code to help trace where went wrong.

a month ago

for this try to use the library logger, at least after the MVP


sdpowerstrok3
FREE

a month ago

A 500 normaly means that the databse connection is failling before you can log anything. I can give you some ideas on what could be causing your issue.

Make sure your port is an int and not a string.

You password is PASSWORD=${{MySQL.MYSQLUSER}}. It looks like the username not the password. MySQL usally exposes a password varible.

You can enable debuging or log the exception when you try to connect to the DB.

Make sure env varibles are actually present at runtime.

I reccommend tring to get more logs because if all its saying is erorr code 500 then all I can tell you is there is a problem with connecting.


sdpowerstrok3

A 500 normaly means that the databse connection is failling before you can log anything. I can give you some ideas on what could be causing your issue.Make sure your port is an int and not a string.You password is PASSWORD=${{MySQL.MYSQLUSER}}. It looks like the username not the password. MySQL usally exposes a password varible.You can enable debuging or log the exception when you try to connect to the DB.Make sure env varibles are actually present at runtime.I reccommend tring to get more logs because if all its saying is erorr code 500 then all I can tell you is there is a problem with connecting.

clueeng
FREEOP

a month ago

That's my bad from copy pasting in the thread, the variable is indeed set to the password. The env variables are present, I've printed them out to check if that was the isssue. I doubt the port is seen as a string instead of an int, since the same code worked locally with my own .env variables

I will try logging more things, I will be back with more logs


oxtrizzz

try using sqlalchemy if u using python

clueeng
FREEOP

a month ago

I am not allowed to use anything other than what is currently present (assignment restrictions)


0x5b62656e5d

Something I’d try is to just start putting print statements everywhere in the code to help trace where went wrong.

clueeng
FREEOP

a month ago

For some reason I cannot see any log, I don't know if it's my fault or not. Probably is, but I would expect the flask logs to show like they did before in the Deploy Logs


Loading...