SSH.net Port Forward Mysql

Jan 2, 2013 at 8:55 PM

I have a button in an app that when pressed will establish a connection with a SSH Server and portforward to a mysql server. The issue is, I can't seem to correctly establish a tunnel connection with the mysql server via SSH.net library. I can however use the mysql .net library + putty & portforward to establish a mysql connection when the button is pressed.

 

I believe what the issue is that my connection string for mysql states the Server as localhost, which should be needed to establish a local connection, right? However using SSH.net library PortFowardLocal I believe the boundhost is not needed (which would be localhost). Of course when I remove the bound host I get a a runtime error.

According to the mysql net documentation a Server needs to be stated in order to connect to a mysql DB.

http://dev.mysql.com/doc/refman/5.0/en/connector-net-programming-connecting-connection-string.html

 

Sorry code is in VB:

 

           Dim client As New SshClient("IP, 22, "User", "password")
            Dim port1 = New ForwardedPortRemote("", 3306, "local IP", 3306)

            'Check for Textbox errors
            If textBoxEmail.Text.Length = 0 Then
                MessageBox.Show("Enter a Email Address", MessageBoxButton.OK)
                textBoxEmail.Focus()
            ElseIf Not Regex.IsMatch(textBoxEmail.Text, "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$") Then
                MessageBox.Show("Enter a vaild Email Address", MessageBoxButton.OK)
                textBoxEmail.[Select](0, textBoxEmail.Text.Length)
                textBoxEmail.Focus()


                'No Textbox errors? Carry on..
            Else

              
                'Connect to SSH Server
                Try
                    client.Connect()

                    'Portforward
                    client.AddForwardedPort(port1)
                    port1.Start()

                
                Catch ex As Exception
                    loginmsg.Text = "Error! Click (?) for help."
                End Try

                Dim email As String = textBoxEmail.Text
                Dim password As String = passwordBox1.Password
                Dim connStr As String = "Server=localhost;" & _
                                            "Database=;" & _
                                             "User Id=;Password=;"
                Dim connection As New MySqlConnection(connStr)
                Try
                    connection.Open()
                Catch ex As Exception
                    loginmsg.Text = "Login Error. Click (?) for help."
                End Try

                Dim cmd As New MySqlCommand("Select * from reg_user where Email='" + email + "'  and password='" + password + "'", connection)
                cmd.CommandType = CommandType.Text
                Dim adapter As New MySqlDataAdapter()
                adapter.SelectCommand = cmd
                Dim dataSet As New DataSet()
                adapter.Fill(dataSet)
                If dataSet.Tables(0).Rows.Count > 0 Then

                    mainwindow.Show()
                    Close()
                Else
                    MessageBox.Show("Please enter existing username/password.", MessageBoxButton.OK)

                End If
                connection.Close()

                port1.Stop()


                client.Disconnect()

 

Thanks for any help!

Jan 3, 2013 at 6:44 PM
Edited Jan 3, 2013 at 7:24 PM

Here is a link of a screenshot of what is going on when i run the app and it cannot connect to the mysql server when using the ssh.net library (WITH CODE ABOVE)

 

http://simpletechss.com/App_mysql_Error.jpg

 

HOWEVER IF I  DO NOT USE SSH.NET LIbrary and use putty as my SSH CLIENT with Mysql library I CAN CONNECT.  What am I doing wrong?!?  THANKS IN ADVANCE FOR ANY HELP! My putty/plink settings:

http://simpletechss.com/Putty_mysql_tunnel.png