0

An attempt was made to access a socket in a way forbidden by its access permissions. I'm connecting to mysql in ssh. Below is my code.

     Console.WriteLine("Connecting to MySQL...");
        using (var client = new SshClient("ssh sever", "ssh username", "ssh password"))
        {
            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=root;PASSWORD=rootPassword;DATABASE=UserDb;SslMode=none"))
                {
                    using (MySqlCommand com = new MySqlCommand("SELECT * FROM fhulu", con))
                    {
                        com.CommandType = CommandType.Text;
                        DataSet ds = new DataSet();
                        MySqlDataAdapter da = new MySqlDataAdapter(com);
                        da.Fill(ds);
                        foreach (DataRow drow in ds.Tables[0].Rows)
                        {
                            Console.WriteLine(drow[0].ToString() + " -- " + drow[1].ToString() + " -- " + drow[2].ToString());
                        }
                    }
                }
                client.Disconnect();
            }
        }

2 Answers2

2

This line of code is the problem, because it forces a specific local port to be used:

var portForwarded = new ForwardedPortLocal("127.0.0.1", 3306, "127.0.0.1", 3306);

Instead, use the constructor that doesn't supply a local port.

Secondly, it doesn't make sense to forward a port to 127.0.0.1, as that is the local host and you don't need SSH to connect to it. You need to use the database server's IP address:

var databaseServerIp = "YOU NEED THIS";
var portForwarded = new ForwardedPortLocal("127.0.0.1", databaseServerIp, 3306);

Then use forwardedPort.BoundPort in your connection string.

For more details (and a code sample you can copy), see Connecting to MySQL Server with SSH from C# and https://stackoverflow.com/a/32605335/23633.

Or, if your database server is actually on 127.0.0.1:3306, you don't need any of the SshClient code at all, and it can all be deleted.

Bradley Grainger
  • 27,458
  • 4
  • 91
  • 108
-1
         Console.WriteLine("Connecting to MySQL...");
        using (var client = new SshClient("ssh sever", "ssh username", "ssh password"))
        {
            client.Connect();
            if (client.IsConnected)
            {
                var strconn = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
                MySqlConnection conn = new MySqlConnection(strconn);
                conn.Open();
                string sql = "select * from table1;";
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                MySqlDataReader rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    Console.WriteLine(rdr[0] + " -- " + rdr[1] + " -- " + rdr[2]);
                }
                rdr.Close();
                conn.Close();
                Console.WriteLine("Connection Closed. Press any key to exit...");
                Console.Read();
                client.Disconnect();
            }
            else
            {
                Console.WriteLine("Client cannot be reached...");
            }
        }