Aaron
Aaron

Reputation: 31

Vb Connecting to an online mysql server database

I'm making a small desktop application in Visual Basic 2010, and I need to establish a connection to an online mysql server database that has been created on the hosting for a website I have made.

For the moment I am just trying to get the connection to succeed, so I have made the following code to attempt this, however I keep getting an error message when I debug:

Imports MySql.Data.MySqlClient

Public Class Form2

Dim serverstring As String = "Server=188.121.42.33;Database=MyDatabase;User Id=MyUser;Password=password"


Private Sub Form2_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    Dim sqlConnection As MySqlConnection = New MySqlConnection
    sqlConnection.ConnectionString = serverstring

    Try
        If sqlConnection.State = ConnectionState.Closed Then
            sqlConnection.Open()
            MsgBox("Sucessfull")
        Else
            sqlConnection.Close()
            MsgBox("Connection failed")
        End If
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

End Class

I would be very greatful if anyone can give me any advice or spot what I am doing wrong as I'm very new to all of this and this is the first application I've made. The error I get when debugging is:

Mysql.data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts.

Thanks again for your time.

Upvotes: 0

Views: 4951

Answers (2)

vsp
vsp

Reputation: 379

Usually it happens because the host side does not allow all IP to connect to mysql. try to add % in hosting side. If it does not work you need to go to mysql and run this script

GRANT ALL ON . to user@'%' IDENTIFIED BY 'password';

Above script will allow all ip address to access the mysql if it still doesn'tn work it means the host provider firewall is strict and my suggestion is you need VPS.

Good luck

Upvotes: 2

admdrew
admdrew

Reputation: 3873

"I have turned my firewall off but I'm still getting the same error." There is far more filtering going on between you and the destination than your personal firewall, none of which you'll be able to directly disable/change.

If the server IP in your example is what you're trying to connect to, then it is very likely a filtering/firewall issue, as cHao mentions. SQL Server's default connection port is tcp/1433, and based on a quick portscan of 188.121.42.33, that host is not accepting connections on tcp/1433:

> nc -vv 188.121.42.33 1433
nc: connect to 188.121.42.33 port 1433 (tcp) failed: Connection timed out

...and...

> nmap -O 188.121.42.33
Starting Nmap 6.25 ( http://nmap.org ) at 2013-09-05 22:01 ric
Nmap scan report for n1nlsmysqladm01.shr.prod.ams1.secureserver.net (188.121.42.33)
Host is up (0.068s latency).
Not shown: 983 filtered ports
PORT     STATE  SERVICE
80/tcp   closed http
113/tcp  closed ident
443/tcp  closed https
1248/tcp open   hermes
1720/tcp open   H.323/Q.931
6000/tcp closed X11
6001/tcp closed X11:1
6002/tcp closed X11:2
6003/tcp closed X11:3
6004/tcp closed X11:4
6005/tcp closed X11:5
6006/tcp closed X11:6
6007/tcp closed X11:7
6009/tcp closed X11:9
6025/tcp closed x11
6059/tcp closed X11:59
7001/tcp closed afs3-callback

Short answer, you will not be able to connect based on the current allowed traffic between source/destination. To get this working, you'll likely need some sort of VPN connection to that host over which tcp/1433 traffic can flow.

Upvotes: 1

Related Questions