Question

I am trying to connect to Mysql database through vb.net However I run into the error:

Unable to connect to any of the specified MySQL hosts

enter image description here Here is the code:

    Imports MySql.Data.MySqlClient
Public Class DBProject_1
    Dim MysqlConn As MySqlConnection

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    MysqlConn = New MySqlConnection
    MysqlConn.ConnectionString = "server=localhost;userid=root;password=root;database=My_Mysql_Database"
    Try
        MysqlConn.Open()
        MessageBox.Show("Connection Successful")
        MysqlConn.Close()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        MysqlConn.Dispose()

    End Try
End Sub
End Class

I have even tried to use

MysqlConn.ConnectionString = "server=**127.0.0.1**;userid=root;password=root;database=My_Mysql_Database"

This may be the reason: When I test connection of the Mysql database in Mysql Workbench the test fails. The error is Failed to Connect to Mysql at 127.0.0.1:3306 with user root enter image description here

Was it helpful?

Solution

Try with this ConnectionString:

 MysqlConn.ConnectionString = "Server=localhost;Database=My_Mysql_Database;Uid=root;Pwd=root;"

OTHER TIPS

The "Can't connect to MySQL server" error usually is the result of not being able to reach the server, e.g. if it is not running or it is on a remote machine but firewalls block access. See also the video I added to the MySQL channel for how to set up connections.

Please check that MySql Service is running by typing "services.msc" in the "run". I hope it will help you!

I have the same problem. But I was a server, in a domain, and now the server is outside the domain.

I try to change the rulesin firewall of the server (where there is MySql server). So go to inbound rules, and search the 2 Mysql rules.

Open them and search th Advanced item. Then check Domain, or/and Private, or/and Public

(for me, Domain was the only cheked option, not good for me because I 'm now outside the domain).

Hope it helps ! Cheers :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top