简体   繁体   English

使用SSH.NET库与MySQL建立SSH连接

[英]SSH connection to MySQL using SSH.NET library

I was given a link to use this library for connection through SSH connection to a MySQL database with C#. 通过使用C#与MySQL数据库的SSH连接,我获得了使用此库进行连接的链接。

Here is the LINK to the library as a lot of you will find this interesting as you don't need to open a SSH channel manually. 这是图书馆的链接 ,因为你不需要手动打开SSH频道,很多人会觉得这很有趣。

This is a very detailed library and has a lot of features but what I want to do is pretty simple. 这是一个非常详细的库,有很多功能,但我想要做的很简单。 I just want to open a SSH channel each time I want to write to the database. 我只想在每次要写入数据库时​​打开SSH通道。

While I was searching through the source that is provided I came up to this part of the code which can be found in TestSshCommand.cs and I think I can use this part for my connections but I need your help to get it work as I have my connection string I don't know how to connect all of it. 当我在搜索提供的源代码时,我找到了可以在TestSshCommand.cs中找到的代码的这一部分,我想我可以将这部分用于我的连接但是我需要你的帮助才能使它工作,因为我有我的连接字符串我不知道如何连接所有它。

Code: 码:

public void Test_Execute_SingleCommand()
        {
            using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
            {
                client.Connect();
                var result = ExecuteTestCommand(client);
                client.Disconnect();

                Assert.IsTrue(result);
            }

My code for connectiong to non SSH channel is: 我连接到非SSH通道的代码是:

con.ConnectionString = ConfigurationManager.ConnectionStrings["Con2"].ConnectionString;
            con.Open();
            cmd = new MySqlCommand("SELECT COUNT(*) FROM " + ConfigSettings.ReadSetting("main"), con);

     //...... more of the code

So how can I combine this code to the above one so it can first open a SSH channel and then execute my query? 那么如何将此代码与上面的代码结合起来,以便它可以先打开一个SSH通道然后执行我的查询?

Do I need to put my code inside connect and disconnect functions? 我需要将我的代码放入连接断开连接功能吗?

One more think I have add .dll file inside references and I must say that none of the imports are working. 还有一个想法我在引用中添加.dll文件,我必须说没有任何导入工作。 So can someone please download the library and try this simple code to see if references are working on your project and so that everyone ( which are a lot ) can have a working solution for their future projects? 那么有人可以下载库并尝试这个简单的代码来查看引用是否适用于您的项目,以便每个人(很多)可以为他们未来的项目提供有效的解决方案?

EDIT: 编辑:

This is my code that I was trying to do later on. 这是我以后尝试做的代码。 My SSH connection works now only need to combine those two to get it work. 我的SSH连接现在只需将两者结合起来就可以了。

Problem is that I need to have one database locally on my pc and need to connect to the server. 问题是我需要在我的电脑上本地拥有一个数据库,并且需要连接到服务器。 So port forwarding won't work as somone says on the forum that I can't port forward while mysql already have port for running 因此,端口转发将不起作用,因为somone在论坛上说我无法移植,而mysql已经有端口用于运行

using (var client = new SshClient("cpanel****", "******", "******"))
          {
            client.Connect();
            con = new MySqlConnection(GetConnectionString());
            con.Open();
            cmd = new MySqlCommand(String.Format("insert into {0} values (null, ?Parname , ?Parname2, ?Parname3, ?Parname4, ?Parname5, ?Parname6, ?Parname7);", ConfigSettings.ReadSetting("main")), con);
            cmd.Parameters.Add("?Parname", MySqlDbType.Double).Value = Math.Round(deciLat, 5);
            // ... more parameters
            cmd.ExecuteNonQuery();
            client.Disconnect();
           }

You simply need to use a different local port for the tunnel than 3306 which is being used by your MySQL instance. 您只需要为隧道使用不同的本地端口,而不是由MySQL实例使用的3306。

Example below with port 8001 being used instead: 改为使用端口8001的示例如下:

using (var client = new SshClient("ssh_hostname", "ssh_username", "ssh_password"))
{
  client.Connect();
  var tunnel = new ForwardedPortLocal("127.0.0.1", 8001, "127.0.0.1", 3306);
  client.AddForwardedPort(tunnel);
  tunnel.Start();
  using (var DB = new DBContext())
  {
    //MySQL interaction here!
    scores = DB.Table
      .Where(x => !String.IsNullOrEmpty(x.SomeField))
      .ToList();
  }
  tunnel.Stop();
}

And then make sure in your connection string you have the server 127.0.0.1 and the port 8001 然后在您的连接字符串中确保您拥有服务器127.0.0.1和端口8001

server=127.0.0.1;port=8001

This will connect anything going to 127.0.0.1:8001 to ssh_hostname:3306. 这将连接到127.0.0.1:8001的任何内容连接到ssh_hostname:3306。

You will need to setup port forwarding with your SSH connection... 您需要使用SSH连接设置端口转发...

Basically make a SSH connection that listens on port 3306 on localhost and forwards that to your MySQL DB server... 基本上建立一个SSH连接,侦听localhost上的端口3306并将其转发到MySQL数据库服务器......

In the connections string to your MySQL DB you need to change the part server=... to server=localhost while your SSH connection needs to go to your MySQL DB server's IP. 在MySQL数据库的连接字符串中,您需要将部分server=...更改为server=localhost而SSH连接需要转到MySQL数据库服务器的IP。

IF you can't forward port 3306 you can use any port you want BUT you need change the additionally the port= part of the connection string... see here for examples on connection strings with a different port... the SSH connection still targets 3306 but listens on a different port on localhost in this case... 如果您不能转发端口3306,您可以使用任何您想要的端口但是您需要更改另外的port=连接字符串的一部分...请参阅此处获取有关具有不同端口的连接字符串的示例...仍然是SSH连接目标3306但在这种情况下侦听localhost上的不同端口...

Here are some working examples with source code (although not with SSH.NET since I don't use SSH.NET but this should nonetheless get you started IMO): 下面是一些带有源代码的工作示例(虽然不使用SSH.NET,因为我不使用SSH.NET但是这应该让你开始使用IMO):

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM