Unable to connect to any of the specified mysql hosts. C# MySQL

后端 未结 15 1388
庸人自扰
庸人自扰 2020-11-29 07:11

I am getting the above error when I execute the code -

MySqlConnection mysqlConn=new MySqlConnection(\"server=127.0.0.1;uid=pankaj;port=3306;pwd=master;datab         


        
相关标签:
15条回答
  • 2020-11-29 07:12

    I am running mysql on a computer on a local network. MySQL Workbench could connect to that server, but not my c# code. I solved my issue by disconnecting from a vpn client that was running.

    0 讨论(0)
  • 2020-11-29 07:12

    In my case by removing portion "port=3306;" from the connection string solved the problem.

    0 讨论(0)
  • 2020-11-29 07:14

    I found the solution to my problem, I was having the same issue. Previously I had my connection string as this notice the port :3306 needs to be either attached to the server like that 127.0.0.1:3306 or removed from server like that Server=127.0.0.1;Port=3306 depending on your .NET environment:

    "Server=127.0.0.1:3306;Uid=username;Pwd=password;Database=db;"
    

    It was running fine until something happened which I am not sure exactly what it is, might be a recent update to my .NET application packages. It looks like format and spacing of the connection string is important. Anyways, the following format seems to be working for me:

    "Server=127.0.0.1;Port=3306;Uid=username;Pwd=password;Database=db;"
    

    Try either of the versions and see which one works for you.

    Also I noticed that you are not using camel casing, this could be it. Make sure your property names are in capital casing like that

    Server
    Port
    Uid
    Pwd
    Database
    
    0 讨论(0)
  • 2020-11-29 07:19

    Since this is the top result on Google:

    If your connection works initially, but you begin seeing this error after many successful connections, it may be this issue.

    In summary: if you open and close a connection, Windows reserves the TCP port for future use for some stupid reason. After doing this many times, it runs out of available ports.

    The article gives a registry hack to fix the issue...

    Here are my registry settings on XP/2003:

    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\MaxUserPort 0xFFFF (DWORD)
    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\MaxUserPort\TcpTimedWaitDelay 60 (DWORD)
    

    You need to create them. By default they don't exists.

    On Vista/2008 you can use netsh to change it to something like:

    netsh int ipv4 set dynamicport tcp start=10000 num=50000
    

    ...but the real solution is to use connection pooling, so that "opening" a connection really reuses an existing connection. Most frameworks do this automatically, but in my case the application was handling connections manually for some reason.

    0 讨论(0)
  • 2020-11-29 07:22

    Update your connection string as shown below (without port variable as well):

    MysqlConn.ConnectionString = "Server=127.0.0.1;Database=patholabs;Uid=pankaj;Pwd=master;"
    

    Hope this helps...

    0 讨论(0)
  • 2020-11-29 07:23

    If you are accessing the live database by using localhost URL then it will not work. Please deploy your service or website on IIS and create URL and then access the database by using new URL, It will work.

    0 讨论(0)
提交回复
热议问题