使用SSH.NET库从.NET连接到MySQL

时间:2015-09-16 08:58:14

标签: c# mysql asp.net ssh mysql-workbench

enter image description here

我正在开发一个网页(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) {}
            }
        }
    }
}

1 个答案:

答案 0 :(得分:3)

  1. 您必须将本地端口转发到远程MySQL端口(3306),而不是SSH端口22(这将创建一个循环)。

  2. 您正在将0传递给boundPort的{​​{1}}参数。这意味着操作系统会自动选择端口号。然而,您正尝试通过固定端口ForwardedPortLocal连接到MySQL。

    • 将固定端口3306传递给3306

      ForwardedPortLocal

      如果本地MySQL数据库已使用本地3306端口,则无法工作。您当然可以使用任何其他本地端口(远程端口必须是3306)。

    • port = new ForwardedPortLocal("127.0.0.1", 3306, "127.0.0.1", 3306); 使用port.BoundPort值(在致电port.Start()后){/ 1}}。

      connBuilder.Port

      确保您拥有最新版本的SSH.NET库。 .BoundPort was not updated in the old versions

    请注意,第一个port.Start(); connBuilder.Port = port.BoundPort; conn = new MySqlConnection(connBuilder.ConnectionString); 指的是本地计算机(相对于本地计算机的IP地址),而第二个指的是服务器(IP地址相对于服务器计算机)。您通常可以省略第一个参数。