Google Cloud - Cant connect to MySQL cloud sql instance from GKE using internal IP

1.6k Views Asked by At

I am trying to migrate to use private IP:s for all our Cloud SQL instances. I have gotten it working for postgres, and am now trying to get access for our wordpress instances using MySQL.

The problem is I cant get the connection working from the running pods in our GKE cluster.

root@******:/var/www/app# mysql --host=10.**.**.* -u *_se -p
Enter password:
ERROR 2003 (HY000): Can't connect to MySQL server on '10.*.*.*' (110 "Connection timed out") 

I have activated the private IP on the Cloud SQL instance, and added a private service connection to the VPC-network that is the same network that our cluster uses, but still cant connect in the same way as I did with postgres.

Does anyone know anything I could try to get this working?

1

There are 1 best solutions below

0
On
  • There are specific network requirements Cloud SQL instances must adhere to when communicating via a private connection. One of which is that your Cloud SQL and GKE instances are located in the same region and VPC network. Check this to configure private IP for MySQL.
  • The GKE cluster must be VPC-native and peered with the same VPC network as the Cloud SQL instance. To connect MySQL from GKE, refer to this Google documentation.

Note: If you are using shared VPC networks, you cannot assign a private IP address in a shared VPC network to an existing Cloud SQL instance. Also, VPC Network Peering uses private services access. However, you do not create the VPC Network Peering explicitly, because the peering is internal to Google Cloud.

  • Some possible causes of mentioned error are as follows:
  1. Network failure especially if MySQL database server is running on a remote host.
  2. No MYSQL server is running on the mentioned host.
  3. Firewall blocking TCP-IP connection or other related reasons.

I would suggest you check the firewall setting and check if your MySQL server is listening on default port 3306. Also try to connect MySQL server on IP for which MySQL server is bound in 'my.cnf’. If it not so, run the following command to bind address if you are using Ubuntu:

               1. Run the command  vim /etc/mysql/my.cnf or vim /etc/mysql/mysqld.conf/mysqld.cnf
               2. Comment bind-address = <Server IP> using the # symbol
               3. Restart your MYSQL server once.