Site icon The Introverted Engineer

Cloud SQL Proxy: can’t connect to remote MySQL server

I struggled for a while trying to get Google Cloud SQL to proxy properly on Compute Engine. Google has a guide to do this, but I continuously received an error saying mysql_connect(): Can't connect to remote MySQL server or #2003 Cannot log in to the MySQL server.

After a ton of time wasted thinking that I had done something wrong to cause my apps like WordPress, PHPMyAdmin, etc. not to be able to connect via my proxy, I realized it wasn’t me at all! You can see the details of the issue on the Google Cloud SQL issue board, but to sum it up, I had to run the following command in my Compute Engine instance.

sudo setsebool httpd_can_network_connect_db=1

What this command does, is it allows Apache to connect to MySQL via the proxy you set up to proxy localhost to Cloud SQL. This is a missing component on the guide Google provides, but hopefully they’ll update it soon to inform people as I imagine this is a common issue.

I hope this helps others avoid wasting too much time debugging something rather simple!