C#创建SSH隧道以连接到远程MySQL
因此,我一直在尝试使用SSH隧道连接到远程服务器,以到达另一端的MySQL数据库。看起来SSH部分终于完成了我的任务,但是尝试连接到MySQL会抛出“从流读取失败。-->System.IO.EndOfStreamException” 全部信息:C#创建SSH隧道以连接到远程MySQL,c#,mysql,ssh,tcpclient,tunnel,C#,Mysql,Ssh,Tcpclient,Tunnel,因此,我一直在尝试使用SSH隧道连接到远程服务器,以到达另一端的MySQL数据库。看起来SSH部分终于完成了我的任务,但是尝试连接到MySQL会抛出“从流读取失败。-->System.IO.EndOfStreamException” 全部信息: MySql.Data.MySqlClient.MySqlException (0x80004005): Reading from the stream has failed. ---> 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()
最后,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上一页又一页地浏览,没有找到解决这个问题的答案
经过一些研究,并得到了普林克窗口显示;它似乎拒绝连接,原因不明
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]
今天,我遇到了与MySql相同的问题,错误是1042(无法连接到任何指定的MySql主机)。我可以通过SSH隧道和MySql连接中设置的A5SQL连接。但在C#中做不到:()。
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]