使用SSH.NET库从.NET连接到MySQL
我正在开发一个网页(ASP.NET / C#),它通过SSH在远程服务器上查询(MySQL)数据库。 我正在使用这两个库(mysql-connector-net-6.9.7)和(Renci.SshNet.dll)。
我可以通过SSH连接在远程服务器上使用MySQL Workbench访问MySQL数据库:
“ portal.RemoteServer.edu:22 ”使用“ RemoteServerUsername ”和
“ RemoteServerPassword ”。
这是我的C#代码,它不会从远程服务器上的Clients表返回任何数据:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Configuration; using MySql.Data.MySqlClient; using Renci.SshNet; using Renci.SshNet.Common; namespace WebApplication1 { public partial class About : Page { protected void Page_Load(object sender, EventArgs e) { MySqlConnection conn = null; SshClient client = null; ForwardedPortLocal port = null; MySqlConnectionStringBuilder connBuilder = new MySqlConnectionStringBuilder(); connBuilder.AllowBatch = true; connBuilder.Server = "127.0.0.1"; connBuilder.Port = 3306; connBuilder.UserID = "LocalHostUserID"; connBuilder.Password = "LocalHostPassword"; connBuilder.Database = "DatabaseName"; ConnectionInfo conInfo = new ConnectionInfo("portal.RemoteServer.edu", "RemoteServerUsername", new PasswordAuthenticationMethod("RemoteServerUsername", "RemoteServerPassword")); using (client = new SshClient(conInfo)) { try { port = new ForwardedPortLocal("127.0.0.1", 0, "127.0.0.1", 22); client.Connect(); client.AddForwardedPort(port); port.Start(); conn = new MySqlConnection(connBuilder.ConnectionString); conn.Open(); conn.ChangeDatabase(connBuilder.Database); using (MySqlCommand cmd = new MySqlCommand("SELECT * FROM DatabaseName.Clients LIMIT 10")) { using (MySqlDataAdapter sda = new MySqlDataAdapter()) { cmd.Connection = conn; sda.SelectCommand = cmd; using (DataTable dt = new DataTable()) { sda.Fill(dt); GridView1.DataSource = dt; GridView1.DataBind(); } } } } catch (Exception ex) {} } } } }
在应用了@Martin Prikryl的答案之后的代码,但它仍然无效。
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Configuration; using MySql.Data.MySqlClient; using Renci.SshNet; using Renci.SshNet.Common; namespace WebApplication1 { public partial class About : Page { protected void Page_Load(object sender, EventArgs e) { MySqlConnection conn = null; SshClient client = null; ForwardedPortLocal port = null; MySqlConnectionStringBuilder connBuilder = new MySqlConnectionStringBuilder(); connBuilder.AllowBatch = true; connBuilder.Server = "127.0.0.1"; connBuilder.Port = 3306; connBuilder.UserID = "remoteUsername"; connBuilder.Password = "remotePassword"; connBuilder.Database = "databasename"; ConnectionInfo conInfo = new ConnectionInfo("portal.remoteserver.edu", 22, "remoteUsername", new PasswordAuthenticationMethod("remoteUsername", "remotePassword")); using (client = new SshClient(conInfo)) { try { port = new ForwardedPortLocal(0, "127.0.0.1", 3306); client.Connect(); client.AddForwardedPort(port); port.Start(); connBuilder.Port = port.BoundPort; conn = new MySqlConnection(connBuilder.ConnectionString); conn.Open(); conn.ChangeDatabase(connBuilder.Database); using (MySqlCommand cmd = new MySqlCommand("SELECT * FROM DatabaseName.Clients LIMIT 10")) { using (MySqlDataAdapter sda = new MySqlDataAdapter()) { cmd.Connection = conn; sda.SelectCommand = cmd; using (DataTable dt = new DataTable()) { sda.Fill(dt); GridView1.DataSource = dt; GridView1.DataBind(); } } } } catch (Exception ex) {} } } } }
以下大多数代码都是自我解释的。 我仍然提出了必要的评论。 我能够使用下面的代码连接到MySql数据库。 我从这里使用了SSH库,而在 .NET中使用了MySql连接器。
using(var client = new SshClient("ssh server id", "sshuser", "sshpassword")) // establishing ssh connection to server where MySql is hosted { client.Connect(); if (client.IsConnected) { var portForwarded = new ForwardedPortLocal("127.0.0.1", 3306, "127.0.0.1", 3306); client.AddForwardedPort(portForwarded); portForwarded.Start(); using (MySqlConnection con = new MySqlConnection("SERVER=127.0.0.1;PORT=3306;UID=someuser;PASSWORD=somepass;DATABASE=Dbname")) { using (MySqlCommand com = new MySqlCommand("SELECT * FROM cities", con)) { com.CommandType = CommandType.CommandText; DataSet ds = new DataSet(); MySqlDataAdapter da = new MySqlDataAdapter(com); da.Fill(ds); foreach (DataRow drow in ds.Tables[0].Rows) { Console.WriteLine("From MySql: " + drow[1].ToString()); } } } client.Disconnect(); } else { Console.WriteLine("Client cannot be reached..."); } }
-
您必须将本地端口转发到远程MySQL端口(
3306
),而不是SSH端口22
(这将创建一个循环)。 -
您将
0
传递给ForwardedPortLocal
的boundPort
参数。 这意味着操作系统会自动选择端口号。 然而,您正试图通过固定端口3306
连接到MySQL。-
将固定端口
3306
传递给ForwardedPortLocal
port = new ForwardedPortLocal("127.0.0.1", 3306, "127.0.0.1", 3306);
如果本地MySQL数据库已使用本地3306端口,则此方法无效。 您当然可以使用任何其他本地端口(远程端口必须是3306)。
-
或者使用
port.BoundPort
值(在调用port.Start()
)。port.Start(); connBuilder.Port = port.BoundPort; conn = new MySqlConnection(connBuilder.ConnectionString);
确保您拥有最新版本的SSH.NET库。 旧版本中未更新
.BoundPort
。
请注意,第一个
127.0.0.1
指的是本地计算机(相对于本地计算机的IP地址),而第二个指的是服务器(IP地址相对于服务器计算机)。 您通常可以省略第一个参数。 -