Search code examples
mysqlpowershellxampp

Access XAMPP mysql from powershell


I try to access the XAMPP mysql on my local WINDOWS10 from powershell. My powershell script works on the mysql db on an external hosting: (Connection names hidden)

$pass = ConvertTo-SecureString -String "*****" -AsPlainText -Force
$user = "*****"
$cred = New-Object -TypeName 'System.Management.Automation.PSCredential' -ArgumentList $user, $pass
$server = "*****"
Connect-MySqlServer -Credential $cred -ComputerName $server -Database "*****" -Verbose -Debug

User, password, database are correct I have a 1-to-1 copy of webpage and mysql on XAMPP with same tables and users which works fine. I set bind-address="0.0.0.0" and #skip-networking is commented out

Which server name do I use for the XAMPP connection? I tried "localhost" and the local IP Error message:

Connect-MySqlServer : Ausnahme beim Aufrufen von "Open" mit 0 Argument(en):  "SSL Connection error."
In Zeile:1 Zeichen:7
+ $C2 = Connect-MySqlServer -Credential $cred -ComputerName "127.0.0.1" ...
+       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Connect-MySqlServer

Solution

  • I found this script working for me after installing the MySQL Connector NET 8.0:

    [void][System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")
    $SQLServer = "localhost"
    $SQLDBName = "****"
    $uid       = "****"
    $pwd       = "****"
    
    
    $Command             = New-Object MySql.Data.MySqlClient.MySqlCommand
    $conn                = New-Object MySql.Data.MySqlClient.MySqlConnection("server=$SQLServer;user id=$uid;password=$pwd;database=$SQLDBName;SslMode=None")
    $Command.CommandText = "Your SQL query"
    $Command.Connection  = $conn
    
    $Command.Connection.Open()
    [int]$i = $Command.ExecuteNonQuery()
    $Command.Connection.Close()