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 ADO.NET Provider for MySQL is a standard ADO.NET Provider that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to MySQL.

ADO.NET Provider

The ADO.NET Provider provides a SQL interface for MySQL; this tutorial shows how to use the Provider to create, retrieve, update, and delete MySQL data.

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


$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


$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


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