FastAPI-MySql-SqlAlchemy Intermittent Broken Pipe Error
yavuzakyazici
HOBBYOP

a year ago

Hi everyone

I keep getting BrokenPipeError: [Errno 32] Broken pipe error from MySQL using SqlAlchemy. It usually happens right when logging in or refreshing tokens. Meaning.. right at the first use after not using for a while. I am the only user plus a few test users so the API service is not continuously used. Did anybody see this? What could be the reason. Could it be a time out issue with MySql?

0 Replies

brody
EMPLOYEE

a year ago

are you getting this error from the mysql service or your fastapi service?


yavuzakyazici
HOBBYOP

a year ago

Sorry.. Project Id: dba6cd6e-5ea1-494c-b77f-81377e24fbde


yavuzakyazici
HOBBYOP

a year ago

I think from SqlAlchemy


yavuzakyazici
HOBBYOP

a year ago

Copying one by one takes long so I took a screenshot

1250695933990273000


yavuzakyazici
HOBBYOP

a year ago

Sql Alchemy says:…


yavuzakyazici
HOBBYOP

a year ago

OperationalError
Exception raised for errors that are related to the database’s operation and not necessarily under the control of the programmer, e.g. an unexpected disconnect occurs, the data source name is not found, a transaction could not be processed, a memory allocation error occurred during processing, etc.

This error is a DBAPI Error and originates from the database driver (DBAPI), not SQLAlchemy itself.


yavuzakyazici
HOBBYOP

a year ago

1250696412421951500


yavuzakyazici
HOBBYOP

a year ago

The link at the end of the screenshot


yavuzakyazici
HOBBYOP

a year ago


yavuzakyazici
HOBBYOP

a year ago

It usually happens, after first login if I have not used for a while. Or… again after not using right at the time of refreshing JWT tokens


brody
EMPLOYEE

a year ago

are you connecting to the database via the private network?


yavuzakyazici
HOBBYOP

a year ago

No.


yavuzakyazici
HOBBYOP

a year ago

Should I?


yavuzakyazici
HOBBYOP

a year ago

I guess I should


brody
EMPLOYEE

a year ago

yes absolutely


yavuzakyazici
HOBBYOP

a year ago

ok. Will try that. I would have to change the connection string for that.


brody
EMPLOYEE

a year ago

yep!


yavuzakyazici
HOBBYOP

a year ago

I am sure there is an example but If I cannot find it, I'll check out the docs


brody
EMPLOYEE

a year ago

what is the variable you use for the database url?


yavuzakyazici
HOBBYOP

a year ago

SQLALCHEMYDATABASEURL


brody
EMPLOYEE

a year ago

what is the name of your mysql service


brody
EMPLOYEE

a year ago

and is the mysql service in the same project as the fastapi service?


yavuzakyazici
HOBBYOP

a year ago

MySQL-9_Bm


yavuzakyazici
HOBBYOP

a year ago

Yes, they are in the same project


brody
EMPLOYEE

a year ago

SQLALCHEMY_DATABASE_URL=${{MySQL-9_Bm.MYSQL_PRIVATE_URL}}


brody
EMPLOYEE

a year ago

as easy as that


yavuzakyazici
HOBBYOP

a year ago

Great.. Will do.. Thanks a lot for this


yavuzakyazici
HOBBYOP

a year ago

OK. I kept everything the same on the SqlAlchemy connection string, except the URL. For the URL I just used the MySQL service name MySQL-9_Bm and not even the port number since it was suggested in the network settins of MySQL… Everything worked.


brody
EMPLOYEE

a year ago

so what is SQLALCHEMY_DATABASE_URL set to now


brody
EMPLOYEE

a year ago

sans the password


yavuzakyazici
HOBBYOP

a year ago

So, If I don't get the broken pipe error again, this means it was because I was using the Newtork URL


yavuzakyazici
HOBBYOP

a year ago

mysql+pymysql://username:mypassword@mysql-9bm/database_Name


yavuzakyazici
HOBBYOP

a year ago

That is an example SqlAlchemy URL for pymysql driver


brody
EMPLOYEE

a year ago

that works too


yavuzakyazici
HOBBYOP

a year ago

Yeah, I don't know why I did not setup the link internally. Network URL does not make sense when I have the chance to use internal. Thanks again Brody


brody
EMPLOYEE

a year ago

no problem!


yavuzakyazici
HOBBYOP

a year ago

Yes, it did not happen. No broken pipe errors all day. I would have already gotten a few. So this fixed it 🙂 Thanks


brody
EMPLOYEE

a year ago

happy to hear that!


Loading...