Automatically start gcloud sql proxy when google compute engine VM starts - mysql

I'm using google compute engine and have an auto scaling instance group that spins up new VMs as needed all sitting behind a load balancer. I'm also using google's cloud SQL in the same project. The VMs need to connect to the cloud SQL instance.
Since the IPs of the VMs are dynamic I can't just plug in the IPs to the SQL access config so I followed the cloud sql proxy setup along with the notes from this very similar question:
How to connect from a pool of Google Compute Engine instances to Cloud SQL DB in the same project?
I can now log into a single test VM and run:
./cloud_sql_proxy -instances=PROJ_NAME:TIMEZONE:SQL_NAME=tcp:3306
and everything works great and that VM connects to the cloud SQL instance.
The next step is where I'm having issues. How can I setup the VM so it automatically starts up the proxy when it's either built from an instance template or just restarted. The obvious answer seem to be to shove the above in the VM's start-up script but that doesn't seem to be working. So with my single test VM I can SSH into the VM and manually run the cloud_sql_proxy command and all works. If I then include the below in my start-up script and restart the VM it doesn't connect:
#! /bin/bash
./cloud_sql_proxy -instances=PROJ_NAME:TIMEZONE:SQL_NAME=tcp:3306
Any suggestions? I seriously can't believe it's this hard to connect to the SQL cloud from a VM in the same project...

The startup script you have shown doesn’t show the download step of the cloud_sql_proxy.
You need to first download and then launch the proxy. So, your startup script should look like:
sudo wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64
sudo mv cloud_sql_proxy.linux.amd64 cloud_sql_proxy
sudo chmod +x cloud_sql_proxy
sudo ./cloud_sql_proxy -instances=PROJ_NAME:TIMEZONE:SQL_NAME=tcp:3306 &

I choose crontab to run cloud_sql_proxy automatically when vm start up.
$crontab -e
and add
#reboot cloud_sql_proxy blah blah.

Related

Stopping Bitnami WAMP Apache and MySQL server through PowerShell script in Windows

I've trying to write a script to stop Apache and MySQL services in Bitnami WAMP. If we do manually, we have to open C:\WAMP\manager-windows.exe and stop the services like this:
However, when I go into command prompt and run the command "httpd -k start", the apache server starts running and I can access my web app but if I open manager-windows.exe again it doesn't reflect the change. I've been also trying to stop the Apache service using command prompt commands "httpd -k stop" but that is somehow conflicting with Apache web server when it is started through the manager-windows.exe.
Bitnami Engineer here,
You can use the servicerun.bat script we include in the installation directory to start/stop all the services at once.
servicerun.bat START
If you only want to start/stop Apache or MySQL using the command prompt, you can use the scripts we include at installdir/apache2/scripts/servicerun.bat or installdir/mysql/scripts/servicerun.bat

ERROR: (gcloud.sql.connect) Mysql client not found

Note there is a similar post here but not fixing my issue:
I had wamp server which comes with mysql server, the path is there. I installed SDK, able to connect to it:
C:\Program Files (x86)\Google\Cloud SDK>gcloud beta sql connect welynxmysql --user=root
However in Cloud Shell, it throws out an error complaining below:
You need the [cloud_sql_proxy] component to use the sql connect
command. ERROR: (gcloud.beta.sql.connect) The cloud_sql_proxy
component(s) is unavailable through the packaging system you are
currently using. Please consider using a separate installation of the
Cloud SDK created through the default mechanism described at:
https://cloud.google.com/sdk/
What am I missing here? How can I connect to mysql instance/database in Cloud Shell?
Thank you very much.
What am I missing here? How can I connect to mysql instance/database in Cloud Shell?
If you are not able to connect to your Cloud SQL instance, you could try to either follow:
Quickstart for Cloud SQL for MySQL
Quickstart for using the proxy for local testing
You could also try to reset your Cloud Shell (note: this will delete all your files within Cloud Shell) as stated here:
Important: This will permanently delete all files in your home directory.
Then, you could try to do a fresh installation of the Google Cloud's SDK.
If none of those works for you, you could try to authorize your account again within the Cloud Shell as follows:
Before beginning, make sure that your Cloud Shell is using your project. If you are not sure you could also run this command to set your project:
gcloud config set project [PROJECT_ID]
Where PROJECT_ID is the project ID where the Cloud SQL instance is located. Then:
Go to your Cloud Shell and run this command (please notice the sudo at
the beginning):
sudo gcloud auth login
Click on the link displayed on the Cloud Shell and log in with your
Google account.
Copy the code displayed on your screen.
Switch to the Cloud Shell, paste it there and hit enter.
You can now run the command as follows:
gcloud beta sql connect [YOUR_INSTANCE_NAME]
or:
gcloud sql connect [YOUR_INSTANCE_NAME]
Where YOUR_INSTANCE_NAME is the name of your Cloud SQL instance.
I hope it helps.

Google Compute Engine is not responding

My GCP server is down. It was working last day. I can see the server in VM Instances but can not connect using SSH. All the client websites are down.
Can any one help ?
There is several reasons this could happen:
If your disk is full
sshd deamon isn't configured properly
If OS login is enabled on your instance
A firewall rule block port 20
Sometimes, you see some connection errors in the console, that worth to take a look.
EDIT:
I will need additional information if that still not working;
Take a look to your serial console logs and tell me if you have any relevant logs that can help like a kernel panic, issue with networking, permission denied, etc
Use Cloud Shell and try to connect to your VM instance with these commands:
gcloud compute firewall-rules create --network=default default-allow-ssh --allow tcp:22
gcloud compute ssh YOUR_INSTANCE_NAME --zone YOUR_ZONE -- -vvv
If you can't connect from cloud shell, try to ping your VM instance (internal IP & external IP)
I highly recommend to delete your screenshots showing information about your VM instance (Firewall rules, Project name, nmap scans, etc).

How to keep persistent SQL Proxy Unix Socket with Google Compute Engine

I have a GCE instance that uses older mysql connection type and in order to connect to a Google Cloud SQL instance, I open an SSH connection through the browser on the GCE instance and run the following:
> cd /
> ./cloud_sql_proxy -dir=/cloudsql -instances=my-gce-instance-name:us-east1:my-sql-instance-name &./cloud_sql_proxy -dir=/cloudsql -
And then it is open for connections. The problem is, when I close the browser window (or even sign into Google with a different user), the connection is lost. Is there any way to persistently run this Unix socket with Google Cloud Engine?
Generally, you can run a command in the background by appending a & to the end of the command. This will start a background process for the proxy. You can stop the proxy with killall cloud_sql_proxy.
Another solution would be to run the proxy as a service. How to do this wildly varies by distro and version. Ubuntu 16 is typically bundled with systemd.

Cant Disable Google Compute Engine

I am trying to stop google compute engine from
https://console.developers.google.com/project/myapp/apiui/api
As soon as I click the off button next to google compute engine, I see the message "Disabling Google Compute Engine...".
The message never goes away and Google Compute Engine is still on.
I'm using Chrome on Windows.
I'm trying to restart the GCE service because I keep getting:
Error: API rate limit exceeded when I try to run gcutil listinstances after setting up my instance for the first time.
Can someone help with either the service restart issue or the API rate limit exceeded issue?
If you'd like to restart your instance, there are two ways to do this. For the first one, go to your VM Instances page (https://console.developers.google.com/project/{project-id}/compute/instances) Be sure to fill in your project-id, or simply go to the page from the console.
Then click on the instance you'd like to restart, and on that page, there's a simple "reboot" button. If this still isn't working, try the next option.
The next option is to login to your instance and do sudo reboot. My guess is this will succeed and not fix your problem, but it will either succeed of tell you why it didn't. Alternatively you can use sudo poweroff and use the console to restart your instance.
You have 4 options to stop your instance.
If you would like to stop your instance using Google Cloud Platform Console, you may use this guide:
Go to the VM instances page in the GCP Console.
Select one or more instances that you want to stop.
At the top of the VM instances page, click Stop.
To stop your instance using gcloud tool, please follow this procedure.
Open your Google Cloud Shell in GCP.
Use the instances stop command and specify one or more instances that you want to stop.
$ gcloud compute instances stop your-instance1 your-instance-2
In the API, construct a POST request to stop an instance.
POST https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-f/instances/example-instance/stop
To stop your instance through the OS for Linux and Windows.
In the GCP Console, go to the VM Instances page.
In the list of virtual machine instances, click SSH in the row of the instance that you want to connect to.
Use the sudo shutdown -h now or sudo poweroff commands. Execute one of these commands while you are logged into the virtual machine:
$ sudo shutdown -h now
$ sudo poweroff
You can reboot a Windows instance, similar to sudo reboot above, using the Start menu. In the Start menu, click on the arrow next to Log off and click Restart.