Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Cloud SQL Proxy connect to server sql from local MacOs

Hi,

I'm new to GCP, I have created a project and database Server SQL to learn about API. by connect my MacOs writing .Net to connect to the Cloud Server SQL.

I am able to connect by adding my public IP but when using Proxy autheticaticate that I have an error connecting from both .Net code and also Azure Studio

error:"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)"

The Proxy is running normal and I telnet that localhost IP it show connect but I don't know why the error still happen. I have check all the requirement API enable... please help to assisst

HuyPhan_0-1716094958448.png

Telnet 

HuyPhan_1-1716094989030.png

 

Solved Solved
0 2 3,085
1 ACCEPTED SOLUTION

The error message "A network-related or instance-specific error occurred while establishing a connection to SQL Server" indicates a problem reaching your Cloud SQL instance, despite the proxy seemingly running correctly. Here are some troubleshooting steps:

  1. Connection String Mismatch:

    • Double-Check: Ensure your connection string in both your .NET code and Azure Data Studio is accurate. When using the proxy, it should look something like this:
    Server=127.0.0.1,1433;Database=<YOUR_DATABASE_NAME>;User Id=<YOUR_USERNAME>;Password=<YOUR_PASSWORD>;
    
    • Important: Replace <YOUR_DATABASE_NAME>, <YOUR_USERNAME>, and <YOUR_PASSWORD> with your actual Cloud SQL credentials. The Server is 127.0.0.1 (localhost) because you're connecting through the proxy, and the Port is 1433, as per your proxy logs.
  2. Firewall Rules:

    • Google Cloud Console: Go to your Cloud SQL instance in the Google Cloud Console. Under the "Connections" tab, verify that your machine's public IP is allowed in the "Authorized networks" section.
    • Proxy Authentication: If you're using IAM database authentication with the proxy, double-check that the service account or user you're running the proxy as has the necessary permissions (Cloud SQL Client role at minimum).
  3. Proxy Configuration:

    • Command: Review the command you used to start the proxy. It should include the correct instance connection name and authentication method. Example:
    ./cloud_sql_proxy -instances=<PROJECT_ID>:<REGION>:<INSTANCE_NAME>=tcp:1433
    
    • Service Account: If using a service account, make sure the credentials file is correctly specified (-credentials_file flag) and has the required permissions.
  4. Network Issues:

    • Telnet Test: While your telnet test suggests the proxy is reachable, try connecting to the actual Cloud SQL instance's public IP (if allowed by firewall rules) using telnet or a similar tool to rule out network issues between your machine and Google Cloud.

Additional Tips:

  • Proxy Logs: Look for more detailed error messages in the Cloud SQL Proxy logs. They might provide clues about the specific issue.
  • Restart Proxy: Try restarting the Cloud SQL Proxy. Sometimes, a simple restart can resolve transient problems.
  • Cloud SQL Auth Proxy: If you haven't already, consider using the Cloud SQL Auth Proxy, which handles authentication for you and simplifies the connection process.

Example:

 
using System.Data.SqlClient;

// ...

string connectionString = "Server=127.0.0.1,1433;Database=<YOUR_DATABASE_NAME>;User Id=<YOUR_USERNAME>;Password=<YOUR_PASSWORD>;";

using (SqlConnection connection = new SqlConnection(connectionString))
{
    connection.Open();
    // Your SQL commands here
}

View solution in original post

2 REPLIES 2

The error message "A network-related or instance-specific error occurred while establishing a connection to SQL Server" indicates a problem reaching your Cloud SQL instance, despite the proxy seemingly running correctly. Here are some troubleshooting steps:

  1. Connection String Mismatch:

    • Double-Check: Ensure your connection string in both your .NET code and Azure Data Studio is accurate. When using the proxy, it should look something like this:
    Server=127.0.0.1,1433;Database=<YOUR_DATABASE_NAME>;User Id=<YOUR_USERNAME>;Password=<YOUR_PASSWORD>;
    
    • Important: Replace <YOUR_DATABASE_NAME>, <YOUR_USERNAME>, and <YOUR_PASSWORD> with your actual Cloud SQL credentials. The Server is 127.0.0.1 (localhost) because you're connecting through the proxy, and the Port is 1433, as per your proxy logs.
  2. Firewall Rules:

    • Google Cloud Console: Go to your Cloud SQL instance in the Google Cloud Console. Under the "Connections" tab, verify that your machine's public IP is allowed in the "Authorized networks" section.
    • Proxy Authentication: If you're using IAM database authentication with the proxy, double-check that the service account or user you're running the proxy as has the necessary permissions (Cloud SQL Client role at minimum).
  3. Proxy Configuration:

    • Command: Review the command you used to start the proxy. It should include the correct instance connection name and authentication method. Example:
    ./cloud_sql_proxy -instances=<PROJECT_ID>:<REGION>:<INSTANCE_NAME>=tcp:1433
    
    • Service Account: If using a service account, make sure the credentials file is correctly specified (-credentials_file flag) and has the required permissions.
  4. Network Issues:

    • Telnet Test: While your telnet test suggests the proxy is reachable, try connecting to the actual Cloud SQL instance's public IP (if allowed by firewall rules) using telnet or a similar tool to rule out network issues between your machine and Google Cloud.

Additional Tips:

  • Proxy Logs: Look for more detailed error messages in the Cloud SQL Proxy logs. They might provide clues about the specific issue.
  • Restart Proxy: Try restarting the Cloud SQL Proxy. Sometimes, a simple restart can resolve transient problems.
  • Cloud SQL Auth Proxy: If you haven't already, consider using the Cloud SQL Auth Proxy, which handles authentication for you and simplifies the connection process.

Example:

 
using System.Data.SqlClient;

// ...

string connectionString = "Server=127.0.0.1,1433;Database=<YOUR_DATABASE_NAME>;User Id=<YOUR_USERNAME>;Password=<YOUR_PASSWORD>;";

using (SqlConnection connection = new SqlConnection(connectionString))
{
    connection.Open();
    // Your SQL commands here
}

Thank, I recognize that I my configuration string is using the wrong syntax for port when changing colon to comma