无法连接到任何指定的mysql主机.C#MySQL

Opa*_*Web 14 c# mysql sharpdevelop visual-studio-2010

我执行代码时遇到上述错误 -

MySqlConnection mysqlConn=new MySqlConnection("server=127.0.0.1;uid=pankaj;port=3306;pwd=master;database=patholabs;");
        mysqlConn.Open();
Run Code Online (Sandbox Code Playgroud)

我已经尝试将服务器设置为localhost,用户设置为root但我收到以下错误 -

Error: 0 : Unable to connect to any of the specified MySQL hosts.
System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>Unhandled exception</Description><AppDomain>DBSync.exe</AppDomain><Exception><ExceptionType>MySql.Data.MySqlClient.MySqlException, MySql.Data, Version=6.7.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d</ExceptionType><Message>Unable to connect to any of the specified MySQL hosts.</Message><StackTrace>
at  MySql.Data.MySqlClient.NativeDriver.Open()
at MySql.Data.MySqlClient.Driver.Open()
at MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings)
at MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection()
at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection()
at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()
at MySql.Data.MySqlClient.MySqlPool.GetConnection()
at MySql.Data.MySqlClient.MySqlConnection.Open()
at DBSync.MainForm.BtnCalculateClick(Object sender, EventArgs e) in c:\Documents  and Settings\Test01\My Documents\SharpDevelop Projects\DBSync\DBSync\MainForm.cs:line 51
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message&amp;amp; m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message&amp;amp; m)
at System.Windows.Forms.ButtonBase.WndProc(Message&amp;amp; m)
at System.Windows.Forms.Button.WndProc(Message&amp;amp; m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp;amp; m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp;amp; m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp;amp; msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at DBSync.Program.Main(String[] args) in c:\Documents and Settings\Test01\My Documents\SharpDevelop Projects\DBSync\DBSync\Program.cs:line 27</StackTrace><ExceptionString>MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts.
at MySql.Data.MySqlClient.NativeDriver.Open()
at MySql.Data.MySqlClient.Driver.Open()
at MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings)
at MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection()
at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection()
at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()
at MySql.Data.MySqlClient.MySqlPool.GetConnection()
at MySql.Data.MySqlClient.MySqlConnection.Open()
at DBSync.MainForm.BtnCalculateClick(Object sender, EventArgs e) in c:\Documents and Settings\Test01\My Documents\SharpDevelop Projects\DBSync\DBSync\MainForm.cs:line 51
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message&amp;amp; m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message&amp;amp; m)
at System.Windows.Forms.ButtonBase.WndProc(Message&amp;amp; m)
at System.Windows.Forms.Button.WndProc(Message&amp;amp; m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp;amp; m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp;amp; m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp;amp; msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at DBSync.Program.Main(String[] args) in c:\Documents and Settings\Test01\My Documents\SharpDevelop Projects\DBSync\DBSync\Program.cs:line 27</ExceptionString><DataItems><Data><Key>Server Error Code</Key><Value>1042</Value></Data></DataItems></Exception></TraceRecord>
Run Code Online (Sandbox Code Playgroud)

我可以通过mysql workbench连接到mysql服务器并查询数据库.只有代码不起作用.编辑:我注意到,当我使用sharpdevelop而不是在使用Visual Studio时,错误就会出现.

Tam*_*eer 16

有时间隔和连接字符串中的参数顺序很重要(基于个人经验和漫长的夜晚:S)

所以坚持这里的标准格式

服务器= myServerAddress; 端口= 1234; 数据库= MYDATABASE; UID =名为myUsername; PWD = MYPASSWORD;


Blu*_*eft 11

由于这是Google的最佳结果:

如果您的连接最初工作,但在许多成功连接后您开始看到此错误,则可能是此问题.

总结:如果您打开和关闭连接,Windows会保留TCP端口以供将来使用,原因有些愚蠢.执行此操作多次后,它将耗尽可用端口.

该文章提供了一个注册表黑客来解决这个问题......

这是我在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)
Run Code Online (Sandbox Code Playgroud)

你需要创建它们.默认情况下它们不存在.

在Vista/2008上,您可以使用netsh将其更改为:

netsh int ipv4 set dynamicport tcp start=10000 num=50000
Run Code Online (Sandbox Code Playgroud)

...但真正的解决方案是使用连接池,以便"打开"连接确实重用现有连接.大多数框架会自动执行此操作,但在我的情况下,应用程序由于某种原因手动处理连接.


Jim*_*mmy 9

我在本地网络上的计算机上运行mysql.MySQL Workbench可以连接到该服务器,但不能连接到我的c#代码.我通过断开与正在运行的vpn客户端解决了我的问题.

  • 滑稽.我再次遇到同样的问题,做了谷歌搜索,找到了我自己的帖子(没有注意谁发布).它再次解决了我的问题.打算upvote :) (17认同)
  • 代表您投票.我已经好几次了.谢谢你过去. (3认同)
  • 哈哈!谢谢现任约书亚. (3认同)

Mur*_*dız 6

如下所示更新您的连接字符串(port也没有变量):

MysqlConn.ConnectionString = 
    "Server=127.0.0.1;Database=patholabs;Uid=pankaj;Pwd=master;"
Run Code Online (Sandbox Code Playgroud)


Ahm*_*ish 5

我找到了解决我的问题的方法,我也遇到了同样的问题。以前我有我的连接字符串,因为这通知端口:3306需要要么像这样附加到服务器127.0.0.1:3306,要么像这样从服务器中删除,Server=127.0.0.1;Port=3306具体取决于您的 .NET 环境:

"Server=127.0.0.1:3306;Uid=username;Pwd=password;Database=db;"
Run Code Online (Sandbox Code Playgroud)

它运行得很好,直到发生了一些事情,我不确定它到底是什么,可能是我的 .NET 应用程序包的最近更新。看起来连接字符串的格式和间距很重要。无论如何,以下格式似乎对我有用:

"Server=127.0.0.1;Port=3306;Uid=username;Pwd=password;Database=db;"
Run Code Online (Sandbox Code Playgroud)

尝试其中一个版本,看看哪个版本适合您。

我还注意到你没有使用骆驼肠衣,这可能就是这样。确保您的属性名称采用大写形式

Server
Port
Uid
Pwd
Database
Run Code Online (Sandbox Code Playgroud)