gcloud config set project mysql-remote-access-267116
gcloud compute instances create my-client --zone us-central1-f --image-project ubuntu-os-cloud --image-family ubuntu-1804-lts --scopes https://www.googleapis.com/auth/cloud-platform
gcloud compute instances create my-server --zone us-central1-f --image-project ubuntu-os-cloud --image-family ubuntu-1804-lts --scopes https://www.googleapis.com/auth/cloud-platform
sudo apt-get update sudo apt-get -y install mysql-client-5.7
sudo apt-get update sudo apt-get -y install mysql-server-5.7 sudo mysql_secure_installation -press any key to skip the setting up the VALIDATE PASSWORD plugin -set up the root password(to be used for the root user access) -choose y for all the following questions to finish up.
configure the database to listen on its internal IP address by updating the mysqld.cnf configuration file
LOCAL_IP=$(curl http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/0/ip -H "Metadata-Flavor: Google") sudo sed -i "s|bind-address.*|bind-address = $LOCAL_IP|" /etc/mysql/mysql.conf.d/mysqld.cnf
sudo service mysql restart sudo mysql --user=root -p=[root user password you set] -e "show databases"
CLIENT_IP=$(gcloud compute instances describe my-client
--zone=us-central1-f
--format='value(networkInterfaces[0].networkIP)')
sudo mysql -uroot -p=[root user password you set]
-e "CREATE USER 'username'@'${CLIENT_IP}' IDENTIFIED BY '[MY_PASSWORD]';"
sudo mysql -uroot -p=[root user password you set] -e "use mysql; select user from user;"
Grant the new MySQL user permission to log on to the server from the internal IP address of my-client.
sudo mysql -uroot -p=[ROOT_PASSWORD] -e
"GRANT ALL PRIVILEGES ON . TO 'username'@'${CLIENT_IP}'
IDENTIFIED BY '[MY_PASSWORD]';"
sudo mysql --host=my-server --user=username
--password=your_password -e "SHOW DATABASES;"
gcloud compute instances add-tags my-client --tags mysql-client --zone=us-central1-f gcloud compute instances add-tags my-server --tags mysql-server --zone=us-central1-f
gcloud compute firewall-rules create "mysql-remote-access"
--allow tcp:3306 --source-tags "mysql-client"
--target-tags "mysql-server"