Automate MySQL Integration Tasks from PowerShell



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 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.

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()

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.