Ready to get started?

Download a free trial of the AlloyDB Cmdlets to get started:

 Download Now

Learn more:

AlloyDB Icon AlloyDB Data Cmdlets

An easy-to-use set of PowerShell Cmdlets offering real-time access to AlloyDB. The Cmdlets allow users to easily read, write, update, and delete live data - just like working with SQL server.

PowerShell Scripting to Replicate AlloyDB Data to MySQL



Write a simple PowerShell script to replicate AlloyDB data to a MySQL database.

The CData Cmdlets for AlloyDB offer live access to AlloyDB data from within PowerShell. Using PowerShell scripts, you can easily automate regular tasks like data replication. This article will walk through using the CData Cmdlets for AlloyDB and the CData Cmdlets for MySQL in PowerShell to replicate AlloyDB data to a MySQL database.

After obtaining the needed connection properties, accessing AlloyDB data in PowerShell and preparing for replication consists of four basic steps.

The following connection properties are usually required in order to connect to AlloyDB.

  • Server: The host name or IP of the server hosting the AlloyDB database.
  • User: The user which will be used to authenticate with the AlloyDB server.
  • Password: The password which will be used to authenticate with the AlloyDB server.

You can also optionally set the following:

  • Database: The database to connect to when connecting to the AlloyDB Server. If this is not set, the user's default database will be used.
  • Port: The port of the server hosting the AlloyDB database. This property is set to 5432 by default.

Authenticating with Standard Authentication

Standard authentication (using the user/password combination supplied earlier) is the default form of authentication.

No further action is required to leverage Standard Authentication to connect.

Authenticating with pg_hba.conf Auth Schemes

There are additional methods of authentication available which must be enabled in the pg_hba.conf file on the AlloyDB server.

Find instructions about authentication setup on the AlloyDB Server here.

Authenticating with MD5 Authentication

This authentication method must be enabled by setting the auth-method in the pg_hba.conf file to md5.

Authenticating with SASL Authentication

This authentication method must be enabled by setting the auth-method in the pg_hba.conf file to scram-sha-256.

Authenticating with Kerberos

The authentication with Kerberos is initiated by AlloyDB Server when the ∏ is trying to connect to it. You should set up Kerberos on the AlloyDB Server to activate this authentication method. Once you have Kerberos authentication set up on the AlloyDB Server, see the Kerberos section of the help documentation for details on how to authenticate with Kerberos.

Collecting AlloyDB Data

  1. Install the module:

    Install-Module AlloyDBCmdlets
  2. Connect to AlloyDB:

    $alloydb = Connect-AlloyDB -User $User -Password $Password -Database $Database -Server $Server -Port $Port
  3. Retrieve the data from a specific resource:

    $data = Select-AlloyDB -Connection $alloydb -Table "Orders"

    You can also use the Invoke-AlloyDB cmdlet to execute pure SQL-92 statements:

    $data = Invoke-AlloyDB -Connection $alloydb -Query 'SELECT * FROM Orders WHERE ShipCountry = @ShipCountry' -Params @{'@ShipCountry'='USA'}
  4. Save a list of the column names from the returned data.

    $columns = ($data | Get-Member -MemberType NoteProperty | Select-Object -Property Name).Name

Inserting AlloyDB Data into the MySQL Database

With the data and column names collected, you are ready to replicate the data into a MySQL database.

  1. Install the module:

    Install-Module MySQLCmdlets
  2. Connect to MySQL, using the server address and port of the MySQL server, valid user credentials, and a specific database with the table in which the data will be replicated:

    $mysql = Connect-MySQL -User $User -Password $Password -Database $Database -Server $Server -Port $Port
  3. Loop through the AlloyDB data, store the values, and use the Add-MySQL cmdlet to insert the data into the MySQL database, one row at a time. In this example, the table will need to have the same name as the AlloyDB resource (Orders) and to exist in the database.

    $data | % { $row = $_ $values = @() $columns | % { $col = $_ $values += $row.$($col) } Add-MySQL -Connection $mysql -Table "Orders" -Columns $columns -Values $values }

You have now replicated your AlloyDB data to a MySQL database. This gives you freedom to work with AlloyDB data in the same way that you work with other MySQL tables, whether that is performing analytics, building reports, or other business functions.

Notes

  • Once you have connected to AlloyDB and MySQL in PowerShell, you can pipe command results to perform the replication in a single line:

    Select-AlloyDB -Connection $alloydb -Table "Orders" | % { $row = $_ $values = @() $columns | % { $col = $_ $values += $row.$($col) } Add-MySQL -Connection $mysql -Table "Orders" -Columns $columns -Values $values }
  • If you wish to replicate the AlloyDB data to another database using another PowerShell module, you will want to exclude the Columns, Connection, and Table columns from the data returned by the Select-AlloyDB cmdlet since those columns are used to help pipe data from one CData cmdlet to another:

    $columns = ($data | Get-Member -MemberType NoteProperty | Select-Object -Property Name).Name | ? {$_ -NotIn @('Columns','Connection','Table')}