无法连接到任何指定的mysql主机。 C#MySQL
我执行代码时遇到上述错误 –
MySqlConnection mysqlConn=new MySqlConnection("server=127.0.0.1;uid=pankaj;port=3306;pwd=master;database=patholabs;"); mysqlConn.Open();
我已经尝试将服务器设置为localhost,用户设置为root但我收到以下错误 –
Error: 0 : Unable to connect to any of the specified MySQL hosts. System.Transactions Critical: 0 : http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/UnhandledUnhandled exceptionDBSync.exeMySql.Data.MySqlClient.MySqlException, MySql.Data, Version=6.7.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44dUnable 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& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& 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 27MySql.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& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& 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 27Server Error Code1042
我可以通过mysql workbench连接到mysql服务器并查询数据库。 只有代码不起作用。 编辑:我注意到,当我使用sharpdevelop而不是在使用Visual Studio时,错误就会出现。
有时间隔和连接字符串中的参数顺序很重要(基于个人经验和漫长的夜晚:S)
所以坚持这里的标准格式
服务器= myServerAddress; 端口= 1234; 数据库= MYDATABASE; UID =名为myUsername; PWD = MYPASSWORD;
由于这是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)
你需要创建它们。 默认情况下它们不存在。
在Vista / 2008上,您可以使用netsh将其更改为:
netsh int ipv4 set dynamicport tcp start=10000 num=50000
…但真正的解决方案是使用连接池,以便“打开”连接确实重用现有连接。 大多数框架自动执行此操作,但在我的情况下,应用程序由于某种原因手动处理连接。
我在本地网络上的计算机上运行mysql。 MySQL Workbench可以连接到该服务器,但不能连接到我的c#代码。 我通过断开与正在运行的vpn客户端解决了我的问题。
更新您的连接字符串,如下所示(也没有port
变量):
MysqlConn.ConnectionString = "Server=127.0.0.1;Database=patholabs;Uid=pankaj;Pwd=master;"
希望这可以帮助…
使用SqlConnectionStringBuilder
来简化连接
System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder(); builder["Initial Catalog"] = "Server"; builder["Data Source"] = "db"; builder["integrated Security"] = true; string connexionString = builder.ConnectionString; SqlConnection connexion = new SqlConnection(connexionString); try { connexion.Open(); return true; } catch { return false; }
刚遇到同样的问题。 在目标计算机上安装.NET框架解决了这个问题。
更好的是,确保在运行代码的机器中存在所有必需的依赖项。
对于运行VS2013的用户
在Windows 10中。
检查apache服务是否正在运行。 因为它被万维网服务所取代。
运行netstat -n来检查这个。
停止服务。 启动apache。 重启服务。
有时问题可能在您的Windows防火墙上,请确保您的服务器允许访问与您的mysql数据库关联的所有端口。
试试这个:
MySqlConnectionStringBuilder conn_string = new MySqlConnectionStringBuilder(); conn_string.Server = "127.0.0.1"; conn_string.Port = 3306; conn_string.UserID = "root"; conn_string.Password = "myPassword"; conn_string.Database = "myDB"; MySqlConnection MyCon = new MySqlConnection(conn_string.ToString()); try { MyCon.Open(); MessageBox.Show("Open"); MyCon.Close(); MessageBox.Show("Close"); } catch (Exception ex) { MessageBox.Show(ex.Message); }
我有完全相同的错误。
这是你需要做的:
如果您使用的是MAMP,请关闭服务器。 再次打开MAMP时(在重新启动服务器之前),单击首选项按钮。
然后,您需要单击端口选项卡,然后单击“将Web和MySQL端口设置为80和3306”按钮。
using System; using System.Linq; using MySql.Data.MySqlClient; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { // add here your connection details String connectionString = "Server=localhost;Database=database;Uid=username;Pwd=password;"; try { MySqlConnection connection = new MySqlConnection(connectionString); connection.Open(); Console.WriteLine("MySQL version: " + connection.ServerVersion); connection.Close(); } catch (Exception ex) { Console.WriteLine(ex); } Console.ReadKey(); } } }
确保你的数据库服务器正在运行,如果它没有运行,那么它无法建立连接并且在3306上运行默认的mysql所以如果端口号不同则不需要提及端口,那么我们需要提到端口