Running the FAST API image is throwing the following error.
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on None")
I am using databases for async operations with 'mysqldb' driver. Even thought he DB credentials are correct the connection is not being established. Please suggest any solutions.
from databases import Database
from utils.const import DB_USER, DB_NAME, DB_PASSWORD, PROJECT_ID, REGION, INSTANCE
# Define the Cloud SQL connection details
SOCKET_PATH = f'/cloudsql/{PROJECT_ID}:{REGION}:{INSTANCE}'
# Create the connection string
DATABASE_URL = f"mysql+mysqldb://{DB_USER}:{DB_PASSWORD}@/{DB_NAME}?unix_socket={SOCKET_PATH}"
# Create the Database object
db = Database(DATABASE_URL)

There are a few things you can try to troubleshoot your connection to Cloud SQL from a FastAPI container using Unix sockets:
This will list all of the Cloud SQL instances in your project. Make sure that the instance you are trying to connect to is listed and has a status of
RUNNING.This will try to connect to the Cloud SQL instance on port 3306. If the connection is successful, you will see the following output:
If the connection is unsuccessful, you will see the following output:
If the connection is unsuccessful, you must ensure the Cloud SQL instance is accessible from the FastAPI container. You can do this by checking the firewall rules for the Cloud SQL instance and making sure that the FastAPI container can access the instance.
This will try to connect to the Cloud SQL instance using the specified database credentials. If the connection is successful, you will be prompted to enter a password. If the connection is unsuccessful, you will see the following output:
If the connection is unsuccessful, you need to make sure that the database credentials are correct. You can check this by logging into the Cloud SQL instance and running the following command:
This will list all of the users and passwords for the Cloud SQL instance. Make sure that the database credentials you are using are listed in the output of this command.
SOCKET_PATHvariable in your code. Make sure that this path is correct and that the FastAPI container has access to the Unix socket file.If you have checked all of the above and you are still unable to connect to Cloud SQL, you can try the following:
If you are still having trouble connecting to Cloud SQL, please contact Google Cloud support for assistance.