Setting Up Cloud SQL Instances

This page describes how to set up a new Google Cloud SQL instance using Google Cloud Tools for PowerShell. Read the Google Cloud PowerShell cmdlet reference to learn more about Google Cloud cmdlets.

For most cmdlets you can specify a project ID, but if you omit it, PowerShell defaults to whatever is specified in the active Cloud SDK configuration.

Setting up a new instance

A Google Cloud SQL instance is a MySQL database running in the cloud. You can use Cloud SQL instances to store, replicate, and protect your MySQL databases. You can configure the instance's behavior, such as when and where the data will be replicated, or when it is acceptable to perform database maintenance.

To create and start a Cloud SQL database, you first must specify its configuration and settings. To do that using Cloud Tools for PowerShell, use the New-GcSqlSettingConfig and New-GcSqlInstanceConfig cmdlets.

SQL settings, such as logging, maintenance windows, and so on, are parameters of the New-GcSqlSettingConfig cmdlet. Instance-level settings, such as the hardware characteristics of the database machine, are set via the SqlInstanceConfig object, or flags on the New-GcSqlInstanceConfig cmdlet.

Specifying no parameters creates the most basic Cloud SQL instance, using the "db-n1-standard-1" tier:

$setting = New-GcSqlSettingConfig 
$instance = New-GcSqlInstanceConfig `
    "mynewinstance" -SettingConfig $setting

For information on tiers and pricing, see the Cloud SQL Pricing topic.

For information on configuration options, use Get-Help New-GcSqlSettingConfig or see the DatabaseInstance object documentation.

Creating a new instance

Once you have performed the configuration and setup described in the previous section, you can now create the Cloud SQL database using the Add-GcSqlInstance cmdlet:

Add-GcSqlInstance $instance

Creating a new instance may take a minute or so. Once the process finishes, the cmdlet returns a DatabaseInstance object describing the instance that has just been created. You can go to the Cloud Platform Console to see the instance.

From this point on, you can manage the database as described in the Cloud SQL documentation.

Creating a read replica instance

Read replica instances provide replication functionality for the data in a master instance. After creation, they can be made into a failover or into their own standalone instance. Data can be read from read replica instances but it may be semi-out of date, in accordance to semisynchronous replication. By backing up data in multiple regions through read replica instances, data can still be read in the case of an outage.

The following code snippet creates a read replica "mynewreplica" for the already existing master instance "gootoso". The snippet uses the cmdlet New-GcSqlInstanceReplicaConfig. This configures settings specific to read replica instances, like how fast they replicate data, or if they replicate from an external instance:

$setting = New-GcSqlSettingConfig "db-n1-standard-1" 
$replicaConfig = New-GcSqlInstanceReplicaConfig
$instance = New-GcSqlInstanceConfig "mynewreplica" `
    -SettingConfig $setting `
    -ReplicaConfig $replicaConfig `
    -MasterInstanceName "gootoso"
Add-GcSqlInstance $instance

For more information on read replica requirements, see Requirements and Tips for Configuring Replication.

For a demonstration of how to promote a read replica to be a failover or standalone instance, see Managing Instance Replications.

Creating a failover replica instance

Cloud SQL provides high availability configuration for instances through the use of failover replicas. Failover replicas use semisynchronous replication in order to replicate all changes in data in the master instance. If the zone in a master instance experiences an outage, the failover replica is activated and takes control. For this reason, the replica must be in a different zone than the master.

The following code snippet creates a new failover replica "myfailover" for the master instance "gootoso". The replica instance is created the same way as above, using the config cmdlets. However, note the addition of the -FailoverTarget flag:

$setting = New-GcSqlSettingConfig "db-n1-standard-1" 
$replicaConfig = New-GcSqlInstanceReplicaConfig -FailoverTarget
$instance = New-GcSqlInstanceConfig "myfailover" `
    -SettingConfig $setting `
    -ReplicaConfig $replicaConfig `
    -MasterInstanceName "gootoso"
Add-GcSqlInstance $instance

Note that replica instances impact billing the same way that normal instances do.

Send feedback about...

Cloud Tools for PowerShell