sqlalchemygoogle-cloud-runaio-mysql

Unix_socket mysql+aiomysql cloud run google failed


I'm trying to connect to a database within the same project using unix_socket and the aiomysql library, below is an example of how I'm assembling the string, I'm using SQLALchemy but when I try to deploy it on cloud rum it just doesn't connect and the connection error, I am in doubt if I am assembling the string correctly below the example I am assembling

    connect_url = URL.create(
    'mysql+aiomysql',
    username=<USER>,
    password=<PASSWORD>,
    host=<HOST>,
    port=3306,
    database=<BANCO DE DADOS>,
    unix_socket='/cloudsql/project:region:instance',
)

In the aiomysql documentation it informs that it has the unix_socket option but it does not have a completed example


Solution

  • I managed to solve the problem following the example of this link here on the google https://cloud.google.com/sql/docs/mysql/connect-run?hl=pt-br

    I had to use the URL.Create from the SQLAlchemy library as follows

        unix_socket_path = '/cloudsql/<Projeto>:<Regiao>:<Instancia do Cloud mysql>'
    
      
    connect_url = URL.create(
        'mysql+aiomysql',
        username='<User>',
        password='<Password>',
        host='<Host>',
        port=3306,
        database='<bd name>',
        query={"unix_socket": unix_socket_path}
    )
    

    Then I deployed it on Cloud Run and it worked perfectly, this configuration is necessary for accessing the Cloud Mysql database which is restricted, allowing access to applications that are within the same project. This worked perfectly for me.