VOOZH about

URL: https://www.cdata.com/kb/tech/mysql-ado-powershell.rst

⇱ Automate MySQL Integration Tasks from PowerShell


Automate MySQL Integration Tasks from PowerShell

👁 Jerod Johnson
Jerod Johnson
Director, Technology Evangelism
Are you in search of a quick and easy way to access MySQL data from PowerShell? This article demonstrates how to utilize the MySQL Cmdlets for tasks like connecting to MySQL data, automating operations, downloading data, and more.

The CData Cmdlets for MySQL are standard PowerShell cmdlets that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to MySQL.

PowerShell Cmdlets or ADO.NET Provider?

The Cmdlets are not only a PowerShell interface to MySQL, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete MySQL data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for MySQL. To access MySQL data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for MySQL.

Once you have acquired the necessary connection properties, accessing MySQL data in PowerShell can be enabled in three steps.

The CData Provider supports connecting to on-premises and cloud-hosted versions of MySQL such as Amazon RDS for MySQL, Google Cloud SQL for MySQL, Azure Database for MySQL, or Oracle MySQL HeatWave. The Server and Port properties must be set to a MySQL server. If IntegratedSecurity is set to false, then User and Password must be set to valid user credentials. Optionally, Database can be set to connect to a specific database. If not set, tables from all databases will be returned.

SSH Connectivity for MySQL

You can use SSH (Secure Shell) to authenticate with MySQL, whether the instance is hosted on-premises or in supported cloud environments. SSH authentication ensures that access is encrypted (as compared to direct network connections).

SSH Connections to MySQL in Password Auth Mode

To connect to MySQL via SSH in Password Auth mode, set the following connection properties:

  • User: MySQL User name
  • Password: MySQL Password
  • Database: MySQL database name
  • Server: MySQL Server name
  • Port: MySQL port number like 3306
  • UserSSH: "true"
  • SSHAuthMode: "Password"
  • SSHPort: SSH Port number
  • SSHServer: SSH Server name
  • SSHUser: SSH User name
  • SSHPassword: SSH Password

SSH Connections to MySQL in Public Key Auth Mode

To connect to MySQL via SSH in Password Auth mode, set the following connection properties:

  • User: MySQL User name
  • Password: MySQL Password
  • Database: MySQL database name
  • Server: MySQL Server name
  • Port: MySQL port number like 3306
  • UserSSH: "true"
  • SSHAuthMode: "Public_Key"
  • SSHPort: SSH Port number
  • SSHServer: SSH Server name
  • SSHUser: SSH User name
  • SSHClientCret: the path for the public key certificate file

PowerShell

  1. Install the module:

    Install-Module MySQLCmdlets
  2. Connect:

    $mysql = Connect-MySQL -User "$User" -Password "$Password" -Database "$Database" -Server "$Server" -Port "$Port"
    
  3. Search for and retrieve data:

    $shipcountry = "USA"
    $orders = Select-MySQL -Connection $mysql -Table "Orders" -Where "ShipCountry = `'$ShipCountry`'"
    $orders
    

    You can also use the Invoke-MySQL cmdlet to execute SQL commands:

    $orders = Invoke-MySQL -Connection $mysql -Query 'SELECT * FROM Orders WHERE ShipCountry = @ShipCountry' -Params @{'@ShipCountry'='USA'}
    

ADO.NET

  1. Load the provider's assembly:

    [Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for MySQL\lib\System.Data.CData.MySQL.dll")
     
  2. Connect to MySQL:

     
    $conn= New-Object System.Data.CData.MySQL.MySQLConnection("User=myUser;Password=myPassword;Database=NorthWind;Server=myServer;Port=3306;")
    $conn.Open()
    
  3. Instantiate the MySQLDataAdapter, execute an SQL query, and output the results:

    $sql="SELECT ShipName, Freight from Orders"
    
    $da= New-Object System.Data.CData.MySQL.MySQLDataAdapter($sql, $conn)
    $dt= New-Object System.Data.DataTable
    $da.Fill($dt)
    
    $dt.Rows | foreach {
    	Write-Host $_.shipname $_.freight
    }
     

Update MySQL Data

PowerShell

Update-MySQL -Connection $MySQL -Columns @('ShipName','Freight') -Values @('MyShipName', 'MyFreight') -Table Orders -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.MySQL.MySQLCommand("UPDATE Orders SET ShipCountry='USA' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.MySQL.MySQLParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()

Insert MySQL Data

PowerShell

Add-MySQL -Connection $MySQL -Table Orders -Columns @("ShipName", "Freight") -Values @("MyShipName", "MyFreight") 

ADO.NET

$cmd = New-Object System.Data.CData.MySQL.MySQLCommand("INSERT INTO Orders (ShipCountry) VALUES (@myShipCountry)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.MySQL.MySQLParameter("@myShipCountry","USA")))
$cmd.ExecuteNonQuery()

Delete MySQL Data

PowerShell

Remove-MySQL -Connection $MySQL -Table "Orders" -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.MySQL.MySQLCommand("DELETE FROM Orders WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.MySQL.MySQLParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject

Ready to get started?

Download a free trial of the MySQL Data Provider to get started:

 Download Now

Learn more:

👁 MySQL Icon
MySQL ADO.NET Provider

Rapidly create and deploy powerful .NET applications that integrate with MySQL-compatible database engines.