Connection to MySQL from .NET using SSH.NET Library

前端 未结 2 1973
梦谈多话
梦谈多话 2020-12-04 00:06

I am developing a web page (ASP.NET/ C#) that queries (MySQL) database on a remote server over SSH. I am using those two libraries (mysql-connector-net-6.9.7) and (

2条回答
  •  醉梦人生
    2020-12-04 00:34

    1. You have to forward a local port to the remote MySQL port (3306), not the SSH port 22 (that would create a loop).

    2. You are passing 0 to the boundPort argument of the ForwardedPortLocal. That means a port number is automatically selected by the OS. Yet you are trying to connect to the MySQL via the fixed port 3306.

      • Either pass the fixed port 3306 to the ForwardedPortLocal

        port = new ForwardedPortLocal("127.0.0.1", 3306, "127.0.0.1", 3306);
        

        This won't work if the local 3306 port is already used by a local MySQL database. You can of course use any other local port (the remote port must be 3306 though).

      • Or use the port.BoundPort value (after calling the port.Start()) for the connBuilder.Port.

        port.Start();
        connBuilder.Port = port.BoundPort;
        conn = new MySqlConnection(connBuilder.ConnectionString);
        

        Make sure you have the latest version of the SSH.NET library. The .BoundPort was not updated in the old versions.

      Note that the first 127.0.0.1 refers to your local machine (IP address relative to your local machine), while the second refers to the server (IP address is relative to the server machine). You can typically omit the first argument.

提交回复
热议问题