Я использовал Putty, чтобы создать соединение SSH (rlogin) на моем сервере удаленной базы данных (MySQL). Похоже, он работает (без исключения на dbconnection.open ()) < /p>
позже в моем приложении я выполняю следующий код: < /p>
MySqlCommand getCustomers = new MySqlCommand("SELECT * FROM Customer WHERE Active = 1 ORDER BY CustomerName", dbConnection);
MySqlDataReader customersReader = getCustomers.ExecuteReader();
< /code>
getCustomers.execuTerReader (). Вызывает исключение: < /p>
socketException: установленное соединение было прервано программным обеспечением в вашем хост -машине < /p>
< /blockquote>
Я положил на Statement reecter statemied at atemired at atemired. Это открыто в этот момент, поэтому может показаться, что оператор приводит к закрытию соединения. против ключей) < /p>
Вот класс, который я использую для создания соединения: < /p>
using MySqlConnector;
using Renci.SshNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Bella
{
public class DBConnection
{
public static MySqlConnection getConnection()
{
string sshServer = "mySSHServer";
string sshUserName = "ainswort";
string sshPassword = "mySSHPassword";
string databaseServer = "mySSHServer";
string databaseUserName = "ainswort_Anita";
string databasePassword = "myDBPassword";
var (sshClient, localPort) = ConnectSsh(sshServer, sshUserName, sshPassword, databaseServer: databaseServer);
using (sshClient)
{
MySqlConnectionStringBuilder csb = new MySqlConnectionStringBuilder
{
Server = "127.0.0.1",
Port = localPort,
UserID = databaseUserName,
Password = databasePassword,
};
MySqlConnection connection = new MySqlConnection(csb.ConnectionString);
connection.Open();
return connection;
}
}
public static (SshClient SshClient, uint Port) ConnectSsh(string sshHostName, string sshUserName, string sshPassword = null,
string sshKeyFile = null, string sshPassPhrase = null, int sshPort = 7822, string databaseServer = "localhost", int databasePort = 3306)
{
// check arguments
if (string.IsNullOrEmpty(sshHostName))
throw new ArgumentException($"{nameof(sshHostName)} must be specified.", nameof(sshHostName));
if (string.IsNullOrEmpty(sshHostName))
throw new ArgumentException($"{nameof(sshUserName)} must be specified.", nameof(sshUserName));
if (string.IsNullOrEmpty(sshPassword) && string.IsNullOrEmpty(sshKeyFile))
throw new ArgumentException($"One of {nameof(sshPassword)} and {nameof(sshKeyFile)} must be specified.");
if (string.IsNullOrEmpty(databaseServer))
throw new ArgumentException($"{nameof(databaseServer)} must be specified.", nameof(databaseServer));
// define the authentication methods to use (in order)
var authenticationMethods = new List();
if (!string.IsNullOrEmpty(sshKeyFile))
{
authenticationMethods.Add(new PrivateKeyAuthenticationMethod(sshUserName,
new PrivateKeyFile(sshKeyFile, string.IsNullOrEmpty(sshPassPhrase) ? null : sshPassPhrase)));
}
if (!string.IsNullOrEmpty(sshPassword))
{
authenticationMethods.Add(new PasswordAuthenticationMethod(sshUserName, sshPassword));
}
// connect to the SSH server
var sshClient = new SshClient(new ConnectionInfo(sshHostName, sshPort, sshUserName, authenticationMethods.ToArray()));
sshClient.Connect();
// forward a local port to the database server and port, using the SSH server
var forwardedPort = new ForwardedPortLocal("127.0.0.1", databaseServer, (uint)databasePort);
sshClient.AddForwardedPort(forwardedPort);
forwardedPort.Start();
return (sshClient, forwardedPort.BoundPort);
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/794 ... ting-query