简体   繁体   中英

Migrate .NET Core app from Google App Engine to Google Cloud Run - SQL connection issues

I have a .NET Core API currently running in App Engine and everything is working fine there. I am connecting to the database by following Google provided example , defining cloud sql instance in app.yaml :

beta_settings:
  cloud_sql_instances: <sql connection name>

Along with env variables like DB_HOST: cloudsql and other credentials. Note that I am using a custom runtime and provide the dockerfile myself.

While trying to deploy my app to Cloud Run, I've tried setting up the environment variables in the same way as I am providing them in my app.yaml file, as well as providing cloud sql connection name and adding it (note that I could not see the connection name I want, out of 4 existing SQL instances I could only see 2), the same one I use in my app.yaml . Deployment fails with an error:

Unhandled exception. Microsoft.Data.SqlClient.SqlException (0x80131904): A network-related or 
instance-specific error occurred while establishing a connection to SQL Server. The server was 
not found or was not accessible. Verify that the instance name is correct and that SQL Server 
is configured to allow remote connections. (provider: TCP Provider, error: 35 - An internal 
exception was caught)

I've tried following this post, but still nothing changed. Other attempts:

  • I've configured my SQL instance to have a private IP assigned and tried using it at DB_HOST field, still got the exception
  • Tried changing SQL connection name to other instances (appropriately changing environment variables), still got the exception
  • Created a VPC Connector and added it to my revision, still got the same error.

How can I fix this? What am I doing wrong?

  1. If you are migrating an existing web application, see Migrating Your Service to Cloud Run .

  2. Check if your app meets all the criteria for Cloud Run.

  3. Checked this document about connecting to a Cloud SQL instance from a service running in Cloud Run. The steps to configure Cloud Run depend on the type of IP address you assigned to your Cloud SQL instance. You can also check about connection options .

  4. You can follow this example about Google Cloud SQL with MySQL for Cloud Run.

  5. It could be helpful to check this SO post about the correct Cloud SQL connection string syntax for do.netcore app with Cloud Run.

Alright so I managed to make it work by following this quickstart guide, it's quite confusing really to have many different sources about Cloud Run connecting to SQL. Adding the environment variables mentioned in the guide solved the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM