s3rius / FastAPI-template

Feature rich robust FastAPI template.
MIT License
1.94k stars 171 forks source link

MySQL connection refused when pytest executed in github action #120

Closed gwanryo closed 2 years ago

gwanryo commented 2 years ago

I created new FastAPI project with below command, and push it to my own repository to check github action working properly.

python3 -m fastapi_template --name test --description test --api-type rest --db mysql --orm sqlalchemy --ci github --redis --migrations --dummy --routers --swagger --sentry

But, github action keep throws same error in pytest stage.

E pymysql.err.OperationalError: (1044, "Access denied for user 'test'@'%' to database 'mysql'")

I think it's related with 'localhost' problem, but not sure.

gwanryo commented 2 years ago

You can see more details in my repository.

s3rius commented 2 years ago

Hi, and thanks for creating an issue.

Oh, I'll try to investigate it later. Would you mind if I use your repo to validate my fixes?

gwanryo commented 2 years ago

@s3rius Not at all. First commit in my repo will be useful.

gwanryo commented 2 years ago

This issue is related with connection problem from local machine to internal docker. https://stackoverflow.com/a/24326540

s3rius commented 2 years ago

Ok. I found the root cause.

Sorry, why it took so long to find it out.

image

I fixed it by changing some variables in docker container. This problem was caused because before running tests it creates database. But since in github actions I used variable MYSQL_USER and MYSQL_PASSWORD, instead of MYSQL_ROOT_USER and MYSQL_ROOT_PASSWORD it couldn't create test database.

Also I forgot to add MYSQL_AUTHENTICATION_PLUGIN: "mysql_native_password". These steps will fix your repo's builds.

s3rius commented 2 years ago

Also I've created a pull request to fix your problem.

gwanryo commented 2 years ago

Thanks for your help! I'll close this issue.