C# .NET - Unable to connect to any of the specified MySQL hosts".

Asked By Babu D on 30-Jul-11 06:25 AM


hai.. sir

        Unable to connect to any of the specified MySQL hosts".

Give me solution for above error..

thank you
Reena Jain replied to Babu D on 30-Jul-11 06:34 AM
HI,

You may check mysql server is working or not and you can add port number of mysql in connection string like this "Server=myServerAddress;Port=1234;Database=myDataBase;Uid=myUsername;Pwd=myPassword;"

for more information check this and let me know
http://ziya.suzen.net/2009/01/mysql-high-volume-connection-issue-when.html

TSN ... replied to Babu D on 30-Jul-11 06:38 AM

Hi,

if the databaseuser who wants to connect to the MySQL Server is also the rootuser then you need to execute the MySQL Server Instance Configuration Wizard and mark the checkbox "Enable root access from remote machine".

Or use a query on the computer where your code did workand do a query on the information_schema database. Like:

select * from information_schema.user_privileges

there you should get back a column "GRANTEE". There you should find your database user with:

'YourDatabaseUsername'@'%' for accessing the database from remote site (not localhost).

Anoop S replied to Babu D on 30-Jul-11 06:49 AM
Usually in batch type of MySql client code you might have seen this exception before:

MySqlException: Unable to connect to any of the specified MySQL hosts

(or similar, this one is specific to MySql .Net client)

Of course it might be what it says, but actually message can be a little misleading if it happens in some batch jobs (with huge number of connects and disconnects) but nowhere else.

After a little digging and debugging in the MySql .Net client library code it became obvious that it was a low level TCP socket issue. Because of the high number of connects and disconnects, OS (WinXP/2003 in this case) network layer was not finding enough user ports to assign to the server (by design TCP keeps the connection in a TIME_WAIT state before disposing the connection). Little googling quickly relieved a few solutions:

First one is simple. If you can alter the code, the obvious solution is to use pooling or keep the connection around while querying the database. This would be much more efficient as well.


Refer this for more details
http://ziya.suzen.net/2009/01/mysql-high-volume-connection-issue-when.html
Riley K replied to Babu D on 30-Jul-11 07:26 AM
That simple statement is not at all helpful

  •   Check if MySql is running
  •   Once check your connection string is proper or not
Use Namespace
  MySql.Data.MySqlClient.MySqlConnection



Standard
Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;
Default port is 3306.
 
Specifying port
Server=myServerAddress;Port=1234;Database=myDataBase;Uid=myUsername;Pwd=myPassword;


 
Named pipes
Server=myServerAddress;Port=-1;Database=myDataBase;Uid=myUsername;Pwd=myPassword;


 
Multiple servers

Use this to connect to a server in a replicated server configuration without concern on which server to use.

Server=serverAddress1 & serverAddress2 & etc..;Database=myDataBase;Uid=myUsername;Pwd=myPassword;
 
 
Using encryption

This one activates SSL encryption for all data sent between the client and server. The server needs to have a certificate installed.

Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;Encryption=true;
Devil Scorpio replied to Babu D on 30-Jul-11 07:39 AM
Hi Babu,

Set up the server you want to work with on the Data Connections tab of  Visual Studio. With the 5.2.1  install of the .NET MySQL connector, it also installs some nifty features into VS like being able to view\edit\query,etc. to the Mysql database  from the Data connections window.   My point here is to check to see if the data connection, which is the formal way that mySQL developers suggest you create a tie in to your database, works.  You could have a firewall, norton,  etc. issue or it could be specific to Visual studio. Are you able to connect via MySQL Query browser?   Check the permissions and if push comes to shove, load up a  http sniffer to see if the requested connection is actually going to the remote server and coming back with a response.

Hope this helps.
Radhika roy replied to Babu D on 30-Jul-11 11:58 AM
you can easily connect to the any server ,

you have to just specify the address (IP Address) of server in connection string.

like this-

Server=IP Adddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;