所以我一直在尝试使用SSH隧道连接到远程服务器,以便到达另一端的MySQL数据库。似乎SSH部分最终正在进行我的竞标,但尝试连接到MySQL会引发"从流中读取失败。 ---> System.IO.EndOfStreamException"
整条信息:
MySql.Data.MySqlClient.MySqlException (0x80004005): Reading from the stream has failed. ---> System.IO.EndOfStreamException: Failed to read past end of stream.
vid MySql.Data.MySqlClient.MySqlStream.ReadFully(Stream stream, Byte[] buffer, Int32 offset, Int32 count)
vid MySql.Data.MySqlClient.MySqlStream.LoadPacket()
vid MySql.Data.MySqlClient.MySqlStream.LoadPacket()
vid MySql.Data.MySqlClient.MySqlStream.ReadPacket()
vid MySql.Data.MySqlClient.NativeDriver.Open()
vid MySql.Data.MySqlClient.Driver.Open()
vid MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings)
vid MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection()
vid MySql.Data.MySqlClient.MySqlPool.GetPooledConnection()
vid MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()
vid MySql.Data.MySqlClient.MySqlPool.GetConnection()
vid MySql.Data.MySqlClient.MySqlConnection.Open()
vid Wrapper_Steleco.Databas.Database.<constructDatabase>d__11.MoveNext()
SSH.cp:
public async Task connectSSH()
{
try
{
TcpClient client = new TcpClient("localhost", 3307);
Console.WriteLine("Port is open!");
}
catch (Exception ex)
{
Console.WriteLine("Error pinging host: 'localhost:3307'");
}
try
{
await Task.Run(() =>
{
Console.WriteLine("About to execute Plink.");
ProcessStartInfo plink = new ProcessStartInfo()
{
FileName = @".\plink.exe",
Arguments = " -ssh -L 3307:127.0.0.1:3306 user@host -pw password -v",
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
CreateNoWindow = false
};
Process process = Process.Start(plink);
Console.WriteLine("Executed plink...");
process.StandardInput.WriteLine("logout");
process.Kill();
});
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
最后,MySQL:
public async void constructDatabase() // Konstruktor
{
SSH ssh = new SSH();
try
{
var task = Task.Run(() => ssh.connectSSH());
Console.WriteLine("Task-status: " + task.Status);
try
{
task.Wait();
using (task)
{
try
{
Console.WriteLine("Task-status: " + task.Status);
Console.WriteLine("Database-connection continues.");
host = "127.0.0.1";
port = "3307";
dbName = "database";
uid = "user";
password = "password";
conString = "Server=" + host + "; Port=" + port + "; Database=" + dbName + "; Uid=" + uid + "; Pwd=" + password + "";
Console.WriteLine(conString);
conn = new MySqlConnection(conString);
Console.WriteLine("MySQL-state: " + conn.State.ToString());
conn.Open();
Console.WriteLine("MySQL-state: " + conn.State.ToString());
}
catch (MySqlException e)
{
Console.WriteLine(e);
}
catch (EndOfStreamException ex)
{
Console.WriteLine("Stream exception caught: " + ex.Message.ToString());
}
finally
{
if (conn != null)
{
conn.Close();
conn.Dispose();
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
catch (MySqlException ex)
{
eventLog = new EventLog();
eventLog.getError(EventLog.Errors.DatabaseConnectionError, 0, ex.ToString());
connectionError = true;
}
}
我对C#很陌生,多年来一直没有使用它,这是我第一次尝试通过SSH隧道建立这样的远程连接。我一直在考虑是否使用了错误的方法,或者我只是使用了正确的方法。显然有一些我没有正确做的事情,我已经坚持了三天,在Google和StackOverflow上逐页浏览,但没有找到解决这一特定问题的答案。
经过一番研究并获得plink窗口显示;似乎它拒绝连接,原因不明。
Local port 3307 forwarding to 127.0.0.1:3306
Opening connection to 127.0.0.1:3306 for forwarding from 127.0.0.1:52566
Forward connection refused by server: Connection failed [Connection refused]