In this article I will explain with an example, how to resolve the following error: Unable to connect to any of the specified MySQL hosts while working with MySQL database.
 
 

Error

This error occurs due to the following reason in MySQL database.
1. Data Source or port number specified in connection string is not correct.
MySqlException: Unable to connect to any of the specified MySQL hosts error in MySql Database
 
In the below connection string, the Data Source and port number are incorrect.
<connectionStrings>
    <add name="constr" connectionString="Data Source=123.12.0.1;port=3307;Initial Catalog=Northwind;User Id=root;password=pass@123" />
</connectionStrings>
 
 

Solution

Following is the solution for the above error.
1. Make sure that the provided Data Source and port number are correct in the connection string as shown below.
<connectionStrings>
    <add name="constr" connectionString="Data Source=localhost;port=3306;Initial Catalog=Northwind;User Id=root;password=pass@123" />
</connectionStrings>