In some cases, you will have the ability to use and store data from one Cloud SQL instance to another. In this hands-on lab, we will learn how to export data into Cloud Storage from one Cloud SQL instance and import date from Cloud Storage to another Cloud SQL instance.
Learning Objectives
Successfully complete this lab by achieving the following learning objectives:
- Create 2 Cloud SQL Instances
- From the main console navigation, head over to SQL.
- Click on Create Instance.
- Choose MySQL.
- Name the instance instance-1
Root pass: 12345
Database Version: MySQL 5.7- Scroll down to Zonal Availability and set to Single zone
- Under Customize your instance expand the options and within Machine type choose the Shared core type.
- Click Create.
For Instance-2:
- From the main console navigation, head over to SQL.
- Click on Create Instance.
- Choose MySQL.
- Name the instance instance-2
Root pass: 12345
Database Version: MySQL 5.7
Scroll down to Zonal Availability and set to Single zone
Under Customize your instance expand the options and within Machine type choose the Shared core type. - Click Create.
- Enable the Cloud SQL Admin API and Connect to Instance-1 using Cloud Shell
- From the main console navigation, head over to APIs and Services.
- Click on Enable APIs and Services.
- Then type in Cloud SQL Admin API.
- Click Enable.
Connect to the Cloud SQL instance
- From the SQL console, click on the patient-records-instance instance.
- Head to the Connect to the instance section.
- Click on Connect using the cloud shell
- Use the command
pwd
and copy your directory for further use. - Enable local file upload to Cloud SQL by editing the mysql my.cnf file – run this one-liner to add the required setting: l
sudo sh -c 'echo -e "[mysql]nlocal-infile=1" >> /etc/mysql/my.cnf'
- Now use the
gcloud sql connect instance-1 --user=root --quiet
command. Press Enter - Put in the root password which is 12345 and press enter
- You are now logged into the MySQL instance.
- Create a Table and Database on Instance-1
- Use the command
CREATE DATABASE sneakerinfo;
. - Use the command
USE sneakerinfo;
to use the database to create a table. - Use the command
CREATE TABLE sneakerrelease (name VARCHAR(15), model VARCHAR(30), datereleased VARCHAR(30));
- Verify the table has been created by using command
SHOW TABLES;
- Use the 3 dotted horizontal lined button on the top right of the cloud shell and click on More, then click on Upload file and upload the CSV file downloaded from the instructions.
- Use command
LOAD DATA LOCAL INFILE '/home/your directory/SneakersandDates.csv' INTO TABLE sneakerrelease FIELDS TERMINATED BY ',';
- Use the command
- Create a Storage bucket
- From the main console navigation, head over to Storage.
- Click on Create Bucket.
- Name your bucket sneakerinfo.
- Keep Configuration set to the defaults.
- Click Create.
- In the Cloud Storage console, click Create Folder
- Name the folder sneakerrelease.
- Export data from Instance-1 to Cloud Storage
- From the SQL console, click on Export.
- Choose the Cloud Storage folder you want to export to.
- Choose CSV as the file type and use query *SELECT FROM sneakerrelease**
- Click on Advance Options and choose sneakerinfo database.
- Click on Export.
- Connect to Instance-2
- From the SQL console, click on the Instance-2 instance.
- Head to the Connect to the instance section.
- Click on Connect using the cloud shell.
- Now use the
gcloud sql connect instance-2 --user=root --quiet
command. Press Enter - Put in the root password, which is 12345, and press Enter
- You are now logged into the MySQL instance.
- Create a Database and Table within Instance-2
- Use the command
CREATE DATABASE sneakerinfo;
. - Use the command
USE sneakerinfo;
to use the database to create a table. - Use the command
CREATE TABLE sneakerrelease (name VARCHAR(15), model VARCHAR(30), datereleased VARCHAR(30));
- Verify the table has been created by using command
SHOW TABLES;
.
- Use the command
- Import data to Instance 2
- From the SQL console, click on Import.
- Choose the Cloud Storage bucket sneakerinfo and folder sneakerrelease.
- From that folder, you will see the SQL Export file.
- Choose database sneakerinfo and type in sneakerrelease for the table and click on Import.
Verify the data has been imported:
Use commandSELECT * FROM sneakerreleaseG;