2

My connection string as follows.

conn.ConnectionString = "Server=192.248.***.***; Port=3036; User id=admin; password=***; Database=abc; Connect Timeout=60;"

But it returns error "Error connecting to database: Unable to connect to any of the specified MySQL hosts."

But i can log in to the same using PHPMyAdmin.

//192.248.***.***/phpmyadmin

Also i can log in to local MySQL database using:

    conn.ConnectionString = "server=" & "localhost" & ";" & "user id=" & "admin" & ";" & "password=" & "" & ";" & "database=abc"

What is the wrong in this code. I want to connect to the remote database since it is the requirement of the system. Any help please.

Update: In immediate window it shows:

    PassbookPrinter.vshost.exe Error: 0 : Unable to connect to any of the specified MySQL hosts.
A first chance exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll
2
  • post your full stack trace error Commented Dec 10, 2012 at 9:18
  • maybe you have typo in your port .. the default port is 3306 not 3036. unless you changed it. Commented Feb 3, 2017 at 5:33

6 Answers 6

5

Connect to remote MySQL Database Using VB.Net

To connect vb.net to remote MySql database ; No matter what VB.Net version you might be using, just go throw the following steps.

1) Download Mysql Connector/Net from the url (https://dev.mysql.com/downloads/connector/net/)

2) Install the connector; by default the connector will be installed in the path (C:\Program Files\MySQL\Connector Net 6.9.6) that's the version i have installed.

3) Open VB.Net IDE and start the new project.

4) Add the "Mysql.Data.dll" as a reference to your project, which you can find it in the path (C:\Program Files\MySQL\Connector Net 6.9.6\Assemblies\v4.5);

5) Prepare your connection form as shown in this image; enter image description here

6) Create the class named "Database" and write in the following code.

Database class code

Imports MySql.Data.MySqlClient

Public Class Database

Private _connection As New MySqlConnection
Private _errormessge As String
Private _servername As String
Private _databasename As String
Private _userid As String
Private _password As String

Public WriteOnly Property ServerName() As String
    Set(ByVal value As String)
        _servername = value
    End Set
End Property

Public WriteOnly Property DatabaseName() As String
    Set(ByVal value As String)
        _databasename = value
    End Set
End Property

Public WriteOnly Property UserID() As String
    Set(ByVal value As String)
        _userid = value
    End Set
End Property

Public WriteOnly Property Password() As String
    Set(ByVal value As String)
        _password = value
    End Set
End Property

Public ReadOnly Property ErrorMessage() As String
    Get
        Return _errormessge
    End Get
End Property

Public Function Connection() As Boolean
    Try
        _connection.ConnectionString = "Server=" & _servername & ";Port=3306;Database=" & _databasename & ";User ID=" & _userid & ";Password=" & _password & ""
        _connection.Open()
        If _connection.State = ConnectionState.Open Then
            _connection.Close()
            Return True
        End If
    Catch ex As Exception
        _errormessge = ex.Message
        Return False
    End Try
End Function

End Class

Form Class Code

Public Class Frm_Main Private Sub btn_connect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_connect.Click

'Object declaration and instantiation

Dim data As New Database

    With data
        'Assing the object property values
        .ServerName = txt_server.Text
        .DatabaseName = txt_database.Text
        .UserID = txt_uid.Text
        .Password = txt_pwd.Text

        'Connection testing
        If .Connection Then
            MessageBox.Show("Database Conneted.")
        Else
            MessageBox.Show(.ErrorMessage)
        End If
    End With
End Sub

Private Sub btn_exit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_exit.Click
    Close()
End Sub

End Class

7) Run the project and try the connection; if the connection is successfully then your luck; and if the connection is not successfully with the following error message worry not just keep reading more; enter image description here

8) Note the ip address on error message after @ (thats your ip) and add it to your domain cpanel "remote mysql access" the image bellow illustrates how the remote mysql access looks like(they are the same bu they may defer in colors); Don't forget to press "add hosts" button. This settings can work daily for those who are in static ip. enter image description here

See the message of success after the above steps;

enter image description here

But if the error message persists try to leave the password text blank and connect again if you had no password in your remote database; if the error comes again except YES is changed to be NO then you have to check if your in DHCP;

9) If your in DHCP which means the ip is changing in every new Internet connection. If your using modem probably your in DHCP. If your in dynamic ips then check what is changing in the ip's 4 blocks. If the first ip was 197.250.3.201 in the first connection and the next ip is 197.250.60.70 and the next next ip is 197.250.80.24; you have to add 197.250.% in your cpanel access hosts for your connection to be stable. enter image description here

10) Note: As the percent symbol (wild card) flows to the left side of the ip address the more the door of security becomes open. On new error please contact your domain provider there might be some other security issues in the domain. Thanks!

Sign up to request clarification or add additional context in comments.

1 Comment

1

just try this , it will work

con.ConnectionString ="Persist Security Info=False;datasource=site.com;port=3306;username=username;password=password;database=database name"

You must add

Persist Security

in your code . to access your mysql database of your server of site . go cpanel > Remote MySQL , and add your ip address

see this image >>> https://www.dropbox.com/s/ytsz57spanwdpkz/cpanel.PNG

now enjoy :p

Comments

0

The default port for MySQL is 3306. If you can connect from phpmyadmin using the default settings it sounds like a typo and you should have:

conn.ConnectionString = "Server=192.248.***.***; Port=3306; User id=admin; password=***; Database=abc; Connect Timeout=60;"

Additionally remote access to MySQL Server is disabled by default. For example if you're running MySQL on a Debian Linux distro you might need to take additional steps such as the following to allow access from another machine:

http://www.debianhelp.co.uk/remotemysql.htm

For a Windows host the following may prove useful:

http://techminded.net/blog/allow-remote-connections-for-mysql-on-windows.html

8 Comments

Also try Uid=admin; Pwd=*** - I seem to remember the MySQL connector doesn't allow some aliases that SQL/Server does
Nope. Still the same error. Is there something should I have to do since it is secured.
Also noticed that phpadmin is on the other machine, maybe a firewall problem on the machine and port 3306 isn't open for remote connections? Maybe install MySQL Workbench and see if you can connect remotely to isolate the problem.
Further comment, if you can't connect from MySQL Workbench then we'll need to know the operating system MySQL is running on to solve the problem. Just about to update my answer.
Thanks Peter, I tried using MySQL WB. It says cannot connect. It shows 192.248.***.*** (10060). Is it port 10060? The OS is Win 2000 server.
|
0

Iv previously had the same issue, i was the same as it was running on a different server.

Try to ping the server from where the application is running and see if you can connect. that's how i found out and needed a firewall change.

Comments

0

First you need to edit your a file in your phpmyadmin from local to all granted Then create a new user set hostname to IP address of a client machineenter image description here

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-1

try changing port to 3307 on .ini config of mysql then bind-address=server's ip or hostname on .ini config of mysql also

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.