Tuesday, September 20, 2016

Re: [google-cloud-sql-discuss] Automating Google SQL queries in Cloud Shell

It's slightly more complicated than what you might expect, since from the Cloud Shell you're actually connecting from a different machine than the one your Cloud SQL instance is running on. 

Instead of '127.0.0.1', you need to pass the instance's real external IP address (which you can get from the Cloud Console under 'SQL'). You also need to authorize the Cloud Shell machine's external IP address (which you set under 'SQL' -> <instance> -> 'Access Control'). You can get the external IP using some web service eg.

$ wget http://ipinfo.io/ip -qO -
130.211.142.199

The IP can potentially change every time you start a new Cloud Shell. None of this would be necessary if if 'gcloud beta sql connect' allowed you to pass extra flags to the 'mysql' client, but alas it currently does not. Hopefully this feature will exist when the command leaves beta, but if it doesn't it would be a perfect candidate for a feature request.

On Tuesday, September 20, 2016 at 9:19:09 AM UTC-4, Mike Dorfman wrote:
Thanks for the help!  I tried connecting through the mysql client (using the command mysql -uroot -p then entering the password and hitting "enter" when prompted) however I always get an "ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)", even if I specify the host "-h 127.0.0.1".  I've also tried non-root access to no avail.  Any thoughts on how to continue troubleshooting this?

Thanks,
Mike

On Sunday, September 18, 2016 at 7:31:36 PM UTC-4, Vadim Berezniker wrote:
gcloud sql connect launches the stock 'mysql' client. 
You can read the guidance here on ways of passing a password to the mysql client: http://dev.mysql.com/doc/refman/5.6/en/password-security-user.html


On Sun, Sep 18, 2016 at 8:11 AM Mike Dorfman <mdor...@mountwashington.org> wrote:
Hi!

I have a project which requires me to automate a select statement on a Google SQL database and FTP the result to a client.  It seems like the only way to connect through Cloud Shell is via "gcloud beta sql connect my-instance --user=readOnlyUser".  This then prompts for a password and I can perform queries through the command line.  What's the best way to safely (and securely) automate this process so I can run a query once an hour through Cron?

Thanks,
Mike

--
You received this message because you are subscribed to the Google Groups "Google Cloud SQL discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-cloud-sql-discuss+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-cloud-sql-discuss/25467919-bd6f-415c-ba0d-b9fb48099e0d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Google Cloud SQL discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-cloud-sql-discuss+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-cloud-sql-discuss/2226d22c-fecd-44c9-b505-baf174bb6bf6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment