Datastream to Mysql SQL private IP not working over private connectivity

I'm using Terraform to create a DataStream that connects to a private MySQL instance in Cloud SQL. However, I'm encountering connection errors despite seemingly correct configurations.

My Terraform provisions the following resources:

  • Google Cloud Networking resources (likely VPC and subnets)
  • Cloud SQL Private MySQL instance
  • Google DataStream Private Connection
  • Compute Engine VM instance

I don't understand why it is not able  connect MySQL server:

test failed.png

 

 

Solved Solved
1 7 211
3 ACCEPTED SOLUTIONS

Here are some steps to troubleshoot the connection issues between Datastream and your private Cloud SQL MySQL instance:

1. Fundamentals: Network and Cloud SQL Setup

  • VPC Peering: Double-check the peering connection status between your Datastream and Cloud SQL VPCs. Ensure routes are in place for traffic to flow correctly.
  • Firewall Rules: Verify that firewall rules allow traffic on port 3306 from Datastream's IP ranges to the Cloud SQL instance.
  • Authorized Networks: In your Cloud SQL settings, make sure the Datastream private connection endpoint's IP address is whitelisted. If you're using Cloud SQL Auth Proxy, add the proxy's IP and ensure Datastream points to it.

2. DataStream Configuration

  • Connection Profile: Review the hostname (Cloud SQL private IP), port, username, and password. Even small typos here can prevent connections.
  • Connectivity Method: Ensure "Private Connectivity" is selected in your connection profile.

3. Hands-on Testing

  • Compute Engine VM: Create a VM in the same VPC (or a peered VPC) as Datastream.
  • MySQL Client: Install mysql-client on the VM and try connecting: mysql -u your_username -p -h [Cloud SQL private IP]
  • Telnet: Check basic port access: telnet [Cloud SQL private IP] 3306

4. Deeper Checks

  • Datastream Logs: Look for detailed error messages in the Datastream logs.
  • IAM Roles: Ensure Datastream and your VM have the correct roles (e.g., roles/cloudsql.client).

Additional Tips

  • Simplify: If things are complex, try isolating the issue by setting up a minimal test environment. This can help eliminate unrelated configuration problems.
  • Plan Ahead: Use terraform plan to get a preview of your infrastructure changes before applying them.
     

View solution in original post

The "connection refused" error from Google Cloud Datastream to your MySQL server (192.168.59.52) likely stems from one of these:

  • Incorrect hostname resolution (xxxxxx.com)
  • MySQL server not listening on the expected port/interface
  • Firewall rules blocking access
  • Misconfiguration in your Terraform setup

Troubleshooting Steps:

  1. Verify Hostname Resolution:

    • From a VM in the same network, run nslookup xxxxxx.com. The output should match your MySQL server's private IP (192.168.59.52).
    • If it doesn't, correct your DNS configuration.
  2. Check MySQL Server Accessibility:

    • Ensure MySQL is listening on the correct interface (all or the specific private IP). Check the bind-address in your MySQL config file (my.cnf or my.ini).
    • Verify firewall rules allow inbound traffic on port 3306 (or your custom MySQL port) from Datastream's internal Google services or IP ranges.
  3. Test Direct Connectivity:

    • From a VM in the same network, try mysql -h xxxxxx.com -u your_username -p to connect directly to the MySQL server using the hostname.
    • If this fails, there's likely an issue with the server itself (not Datastream).
  4. Review Terraform Configuration:

    • In your google_datastream_connection_profile resource, ensure the hostname under mysql_profile directly points to your MySQL server's hostname (not a Compute Engine instance unless it's specifically forwarding MySQL traffic).

Example:

 
resource "google_datastream_connection_profile" "source_connection_profile" {
    # ... other attributes ...

    mysql_profile {
        hostname = "your_mysql_server_hostname"  # Use the actual MySQL hostname
        # ... other attributes ...
    }
}

Further Debugging:

If the issue persists:

  • Check MySQL server and network logs for connection attempts/rejections.
  • Contact Google Cloud Support for assistance, especially if you suspect a Datastream service issue.

View solution in original post

Yes, you can use the same Cloud SQL Auth Proxy, for multiple Google Cloud Datastream instances.

View solution in original post

7 REPLIES 7

Here are some steps to troubleshoot the connection issues between Datastream and your private Cloud SQL MySQL instance:

1. Fundamentals: Network and Cloud SQL Setup

  • VPC Peering: Double-check the peering connection status between your Datastream and Cloud SQL VPCs. Ensure routes are in place for traffic to flow correctly.
  • Firewall Rules: Verify that firewall rules allow traffic on port 3306 from Datastream's IP ranges to the Cloud SQL instance.
  • Authorized Networks: In your Cloud SQL settings, make sure the Datastream private connection endpoint's IP address is whitelisted. If you're using Cloud SQL Auth Proxy, add the proxy's IP and ensure Datastream points to it.

2. DataStream Configuration

  • Connection Profile: Review the hostname (Cloud SQL private IP), port, username, and password. Even small typos here can prevent connections.
  • Connectivity Method: Ensure "Private Connectivity" is selected in your connection profile.

3. Hands-on Testing

  • Compute Engine VM: Create a VM in the same VPC (or a peered VPC) as Datastream.
  • MySQL Client: Install mysql-client on the VM and try connecting: mysql -u your_username -p -h [Cloud SQL private IP]
  • Telnet: Check basic port access: telnet [Cloud SQL private IP] 3306

4. Deeper Checks

  • Datastream Logs: Look for detailed error messages in the Datastream logs.
  • IAM Roles: Ensure Datastream and your VM have the correct roles (e.g., roles/cloudsql.client).

Additional Tips

  • Simplify: If things are complex, try isolating the issue by setting up a minimal test environment. This can help eliminate unrelated configuration problems.
  • Plan Ahead: Use terraform plan to get a preview of your infrastructure changes before applying them.
     

Got it! Thank you.

 

I have created resources using Terraform for DataStream to MySQL private connectivity. Previously, I created a source connection profile and used an IP address instead of a hostname with same configuration. Here is an example:

connection_profile2.png

But now I want to create it using a hostname instead of an IP address. I have written Terraform code for that; however, I'm facing issues while creating it using a hostname.

Could you please help me with this issue?

Error:

 

 

 Error: Error waiting for Updating ConnectionProfile: {"@type":"type.googleapis.com/google.rpc.ErrorInfo","domain":"datastream.googleapis.com","metadata":{"message":"We can't connect to the data source using the hostname that you provided. Make sure that the hostname is correct.","originalMessage":"(2003, \"Can't connect to MySQL server on '192.168.59.52' ([Errno 111] Connection refused)\")","time":"2024-04-11T12:42:25.686357Z","uuid":"0ff7fc6f-cd38-42f0-b76f-498e0e3bbfa1"},"reason":"WRONG_HOSTNAME"}
│ {"code":"VALIDATE_CONNECTIVITY","description":"Validates that Datastream can connect to the source database.","message":[{"code":"WRONG_HOSTNAME","level":"ERROR","message":"We can't connect to the data source using the hostname that you provided. Make sure that the hostname is correct.","metadata":{"original_error":"(2003, \"Can't connect to MySQL server on '192.168.59.52' ([Errno 111] Connection refused)\")"}}],"state":"FAILED"}

│   with google_datastream_connection_profile.source_connection_profile,
│   on main.tf line 143, in resource "google_datastream_connection_profile" "source_connection_profile":
│  143: resource "google_datastream_connection_profile" "source_connection_profile" {

 

Terraform :

 

resource "google_compute_instance" "custom_hostname_instance" {
  name         = "custom-hostname-instance-name"
  project      = var.project
  machine_type = var.proxy_machine_type
  zone         = var.zone

  # Set a custom hostname below
  hostname = "xxxxxx.com"

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-11"
    }
  }

  network_interface {
    network    = google_compute_network.network.name
    subnetwork = google_compute_subnetwork.private-1.self_link
    access_config {
      // Ephemeral public IP
    }
  }

resource "google_datastream_connection_profile" "source_connection_profile" {
  display_name          = "Source connection profile"
  location              = var.region
  connection_profile_id = "source-profile"
  project               = var.project

  mysql_profile {
    hostname = google_compute_instance.custom_hostname_instance.hostname
    username = var.user_name
    password = var.user_password
  }

  private_connectivity {
    private_connection = google_datastream_private_connection.private.id
  }

  # depends_on = [google_datastream_private_connection.private]
}

 

 

 

The "connection refused" error from Google Cloud Datastream to your MySQL server (192.168.59.52) likely stems from one of these:

  • Incorrect hostname resolution (xxxxxx.com)
  • MySQL server not listening on the expected port/interface
  • Firewall rules blocking access
  • Misconfiguration in your Terraform setup

Troubleshooting Steps:

  1. Verify Hostname Resolution:

    • From a VM in the same network, run nslookup xxxxxx.com. The output should match your MySQL server's private IP (192.168.59.52).
    • If it doesn't, correct your DNS configuration.
  2. Check MySQL Server Accessibility:

    • Ensure MySQL is listening on the correct interface (all or the specific private IP). Check the bind-address in your MySQL config file (my.cnf or my.ini).
    • Verify firewall rules allow inbound traffic on port 3306 (or your custom MySQL port) from Datastream's internal Google services or IP ranges.
  3. Test Direct Connectivity:

    • From a VM in the same network, try mysql -h xxxxxx.com -u your_username -p to connect directly to the MySQL server using the hostname.
    • If this fails, there's likely an issue with the server itself (not Datastream).
  4. Review Terraform Configuration:

    • In your google_datastream_connection_profile resource, ensure the hostname under mysql_profile directly points to your MySQL server's hostname (not a Compute Engine instance unless it's specifically forwarding MySQL traffic).

Example:

 
resource "google_datastream_connection_profile" "source_connection_profile" {
    # ... other attributes ...

    mysql_profile {
        hostname = "your_mysql_server_hostname"  # Use the actual MySQL hostname
        # ... other attributes ...
    }
}

Further Debugging:

If the issue persists:

  • Check MySQL server and network logs for connection attempts/rejections.
  • Contact Google Cloud Support for assistance, especially if you suspect a Datastream service issue.

Thank you for the guidance! I have another question: Can we use the same proxy for multiple DataStream's?

Yes, you can use the same Cloud SQL Auth Proxy, for multiple Google Cloud Datastream instances.

That's Great! Thank you.