1
votes

I try to connect to a remote MySQL server using PowerShell 5.1 on a Windows 10 64 Bit system. I used Powershell x86 and the 64 Bit version.

When I use the Open() method of System.Data.Common.DbConnection I get an error that a file called Renci.SshNet could not be found while calling the Open() method. I wonder why the Renci.SshNet.dll is even needed here, because I don't try to build an SSH connection.

I installed the MySQL dot net connector.

I installed the files for the MySQL module into: C:\Program Files (x86)\WindowsPowerShell\Modules\MySQL MySql.psd1 MySQL.psm1

I imported the module MySQL

I imported the MySQL Assembly (whatever this exactly may be): Result: GAC Version Location
--- ------- --------
True v4.0.30319 C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\MySql.Data...

I downloaded the Renci.SshNet.dll file and used it like seen in the source code:

[void][Reflection.Assembly]::LoadFrom( (Resolve-Path "$PSScriptRoot\SSH.NET.Fork.2018.8.25.2\lib\netstandard2.0\Renci.SshNet.dll") )  
# Import MySQL Assembly
Import-Module MySQL

[System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")

# Connection string
$strServer = "db.server.test"
$strDB = "mydb"
$strUser = "dbuser"
$strPw = "mypassword"

$cs = "Server=$($strServer);Port=3306;Database=$($strDB);Uid=$($strUser);Pwd=$($strPw);"

$conn = New-Object MySql.Data.MySqlClient.MySqlConnection($cs)
$conn.ConnectionString = $cs
$conn.Open()

The expected result is getting a connection to the MySQL database, but I always get the error message below:

Ausnahme beim Aufrufen von "Open" mit 0 Argument(en): "Die Datei oder Assembly "Renci.SshNet, Version=2016.1.0.0, Culture=neutral, PublicKeyToken=1cee9f8bde3db106" oder eine Abhängigkeit davon wurde nicht gefunden. Das System kann die angegebene Datei nicht finden."

Exception calling "Open" with 0 arguments: "The file oder Assembly "Renci.SshNet, Version=2016.1.0.0, Culture=neutral, PublicKeyToken=1cee9f8bde3db106" or a dependency could not be found. The system can't find the file.

3

3 Answers

1
votes

It seems like the Assembly reflection is deprecated, which is not the main problem but nevertheless.

Deprecated:

[void][system.reflection.Assembly]::LoadFrom("C:\Program Files (x86)\MySQL\MySQL Connector Net 8.0.17\Assemblies\v4.5.2\MySQL.Data.dll")

Use this instead:

Add-Type -Path "C:\Program Files (x86)\MySQL\MySQL Connector Net 8.0.17\Assemblies\v4.5.2\MySQL.Data.dll"`

The main problem is the wrong version of the Renci.SShNet.dll file which I didn't know where to get. It can be found in a folder within the MySQL Connector: `Add-Type -Path 'C:\Program Files (x86)\MySQL\MySQL Connector Net 8.0.17\Assemblies\v4.5.2\Renci.SshNet.dll'

The complete code to connect to the database is:

Add-Type -Path "C:\Program Files (x86)\MySQL\MySQL Connector Net 8.0.17\Assemblies\v4.5.2\MySQL.Data.dll"

# This is essential to solve the missing Renci.SshNet.dll error
Add-Type -Path 'C:\Program Files (x86)\MySQL\MySQL Connector Net 8.0.17\Assemblies\v4.5.2\Renci.SshNet.dll'

# Connection string
$strServer = "db.server.test"
$strDB = "mydb"
$strUser = "dbuser"
$strPw = "mypassword"

$cs = "Server=$($strServer);Port=3306;Database=$($strDB);Uid=$($strUser);Pwd=$($strPw);"

$conn = New-Object MySql.Data.MySqlClient.MySqlConnection($cs)
$conn.ConnectionString = $cs
$conn.Open()
0
votes

I got the same problem with Connector/NET v8.0.17. Reverting to v8.0.16 fixed it. You can install previous versions using the MySql Installer. Make sure to set the filter to "Other Releases".

enter image description here

0
votes

Just Download MySqlConnector.msi https://dev.mysql.com/downloads/file/?id=489908 with this link you can download mySQLconnector.exe just install that completely. I personally face this error during database connectivity but with this link you can solve this error easily.