Create an HA Cloud SQL (MySQL) instance
GRANT USAGE ON *.* TO andrew@'%' IDENTIFIED BY 'password';Create a GCE instance (VM)
Write few bash scripts to test connection, and repeat it every second
#!/bin/bash
USER=andrew
PASS=password
REMOTEHOST=10.74.32.3
MYSQL_CONN="-h${REMOTEHOST} -u${USER} -p${PASS}"
mysqladmin ping ${MYSQL_CONN} 2>/dev/null 1>/dev/null
if [ $? -eq 0 ]
then
echo $(date) "DB connection is AVAILABLE"
else
echo $(date) "DB connection is NOT available"
fi
while sleep 1; do sh checkMySQLConnection.sh; doneWrite Cloud Function code to update the instance type upon receiving a message from cloud pub/sub
constraints/cloudfunctions.allowedIngressSettingsRun the demo
while sleep 1; do sh checkProxySQLConnection.sh; donepython3 main.pyClean up & prep for the next demo
python3 reset.pymysql -u admin -padmin -h 127.0.0.1 -P6032 --prompt='proxysql> '
update mysql_servers set hostname='10.74.32.31' where hostname='10.74.32.25';
LOAD MYSQL SERVERS TO RUNTIME;
SAVE MYSQL SERVERS TO DISK;
REFERENCES
from googleapiclient import discovery
from oauth2client.client import GoogleCredentials
from pprint import pprint
credentials = GoogleCredentials.get_application_default()
service = discovery.build('sqladmin', 'v1', credentials=credentials, cache_discovery=False)
project = 'addo-demo-cloudsql'
instance_name = 'ha-demo'
scaledup_tier = 'db-n1-standard-2'
scaleddown_tier = 'db-n1-standard-1'
def scaledb(event, context):
print(event)
request = service.instances().get(project=project, instance=instance_name)
response = request.execute()
updated_dbinstancebody = {
"settings": {
"settingsVersion": response["settings"]["settingsVersion"],
"tier": scaledup_tier,
"activationPolicy": "ALWAYS"
}
}
patch(instance_name, updated_dbinstancebody)
def patch(instance, dbinstancebody):
request = service.instances().get(project=project, instance=instance)
response = request.execute()
request = service.instances().patch(
project=project,
instance=instance,
body=dbinstancebody)
response = request.execute()
pprint(response)
Requirements.txt
google-api-python-client==1.10.0
google-auth-httplib2==0.0.4
google-auth==1.19.2
oauth2client==4.1.3
Tue Apr 12 17:42:20 UTC 2022 DB connection is AVAILABLE
Tue Apr 12 17:42:21 UTC 2022 DB connection is NOT available
Tue Apr 12 17:42:22 UTC 2022 DB connection is NOT available
Tue Apr 12 17:42:23 UTC 2022 DB connection is NOT available
Tue Apr 12 17:42:24 UTC 2022 DB connection is NOT available
Tue Apr 12 17:42:25 UTC 2022 DB connection is NOT available
Tue Apr 12 17:42:26 UTC 2022 DB connection is NOT available
Tue Apr 12 17:42:27 UTC 2022 DB connection is NOT available
Tue Apr 12 17:42:28 UTC 2022 DB connection is NOT available
Tue Apr 12 17:42:29 UTC 2022 DB connection is NOT available
Tue Apr 12 17:42:30 UTC 2022 DB connection is NOT available
Tue Apr 12 17:42:31 UTC 2022 DB connection is NOT available
Tue Apr 12 17:42:32 UTC 2022 DB connection is NOT available
Tue Apr 12 17:42:33 UTC 2022 DB connection is NOT available
Tue Apr 12 17:44:41 UTC 2022 DB connection is NOT available
Tue Apr 12 17:45:13 UTC 2022 DB connection is AVAILABLE
Wed Apr 13 01:54:20 UTC 2022 DB connection is AVAILABLE
Wed Apr 13 01:54:21 UTC 2022 DB connection is NOT available
Wed Apr 13 01:54:22 UTC 2022 DB connection is NOT available
Wed Apr 13 01:54:23 UTC 2022 DB connection is NOT available
Wed Apr 13 01:54:24 UTC 2022 DB connection is NOT available
Wed Apr 13 01:54:25 UTC 2022 DB connection is NOT available
Wed Apr 13 01:54:26 UTC 2022 DB connection is NOT available
Wed Apr 13 01:54:27 UTC 2022 DB connection is NOT available
Wed Apr 13 01:54:28 UTC 2022 DB connection is NOT available
Wed Apr 13 01:54:44 UTC 2022 DB connection is AVAILABLE