Mysql – Unable to connect to cloud sql from a gcp vm instance

google-cloud-platformgoogle-compute-engineMySQL

enter image description here

I have created a very simple cloud sql database. It is a second generation mysql.

Form my other vm instance within the same project, I can ping this sql server with no issue

> ping 104.154.107.11                                                                     
PING 104.154.107.11 (104.154.107.11) 56(84) bytes of data.                                                                               
64 bytes from 104.154.107.11: icmp_seq=1 ttl=48 time=128 ms                                                                               
64 bytes from 104.154.107.11: icmp_seq=2 ttl=48 time=126 ms                                                                               
64 bytes from 104.154.107.11: icmp_seq=3 ttl=48 time=126 ms                                                                               
^C                                                                                                                                         
--- 104.154.106.111 ping statistics ---                                                                                                    
3 packets transmitted, 3 received, 0% packet loss, time 2002ms                                                                             
rtt min/avg/max/mdev = 126.789/127.307/128.309/0.708 ms    

The ping returns OK.

However if I try to connect using mysql client, I had a timeout

$ mysql --host=104.154.107.11                                                           
ERROR 2003 (HY000): Can't connect to MySQL server on '104.154.107.11' (110) 

Apparently private IPs are not allowed to add to authorised network. What do I need to do to fix the connectivity?

Best Answer

To connect from a VM to a Cloud SQL instance, you will need to authorize the public IP address of the VM instance.

As your screenshot shows no network is listed in Authorized network section. Click on Add network button, enter external IP address of your VM instance in Network field, click on Done, then Save it. Now you should be able to use mysql client from within your VM to connect to your Cloud SQL instance.