![]() |
VOOZH | about |
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.
You can also optionally set the following:
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.
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.
This authentication method must be enabled by setting the auth-method in the pg_hba.conf file to md5.
This authentication method must be enabled by setting the auth-method in the pg_hba.conf file to scram-sha-256.
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.
Install the module:
Install-Module AlloyDBCmdlets
Connect to AlloyDB:
$alloydb = Connect-AlloyDB -User $User -Password $Password -Database $Database -Server $Server -Port $Port
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'}
Save a list of the column names from the returned data.
$columns = ($data | Get-Member -MemberType NoteProperty | Select-Object -Property Name).Name
With the data and column names collected, you are ready to replicate the data into a MySQL database.
Install the module:
Install-Module MySQLCmdlets
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
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.
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')}
Download a free trial of the AlloyDB Cmdlets to get started:
Download NowLearn more:
👁 AlloyDB IconAn 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.