-1

I am trying to use Invoke-Sqlcmd command in PowerShell (version 7):

$Server = 'localhost'
$Database = 'db'
$Query = "PRINT 'This is output'"
$Username = 'root'
$Password = 'pass'
Invoke-SQLCmd -ServerInstance $Server -Database $Database -ConnectionTimeout 2 -QueryTimeout 5 -Query $Query

But getting error:

Invoke-Sqlcmd: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) Invoke-Sqlcmd:

I can connect to DB using Workbench and using mysql command from powershell, but Invoke-Sqlcmd returns this error every time, can anyone point me where the problem is?

Shadow
  • 33,525
  • 10
  • 51
  • 64
  • 4
    Are you trying to use `Invoke-Sqlcmd` against a MySQL instance? That isn't going to work... As the error tells you, it's for connecting to **SQL Server**, *not* MySQL. – Thom A Jul 22 '22 at 08:08
  • Thank you very much, that was my bad, installed MySQL Connector and now can connect to MySQL Server. Thanks again for pointing that! – Роман Рослый Jul 22 '22 at 10:30

1 Answers1

0

As Larnu pointed - I tried to use Invoke-Sqlcmd to connect to MySQL Server which will not work, so I installed MySQL Connector and now able to connect using

$Mysqlhost= "127.0.0.1"
$Mysqluser= "user"
$Mysqlpass= "pass"
$Mysqldatabase= "db"
​
$Connection = [MySql.Data.MySqlClient.MySqlConnection]@{ConnectionString="server=$Mysqlhost;uid=$Mysqluser;pwd=$Mysqlpass;database=$Mysqldatabase"}
$Connection.Open()
$sql = New-Object MySql.Data.MySqlClient.MySqlCommand
$sql.Connection = $Connection

Maybe will be useful for someone. Thanks to Larnu for pointing to my error.