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

后端 未结 15 1390
庸人自扰
庸人自扰 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:25
    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();
            }
        }
    }
    

    make sure your database server is running if its not running then its not able to make connection and bydefault mysql running on 3306 so don't need mention port if same in case of port number is different then we need to mention port

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

    In my case the hosting had a whitelist for remote connection to MySql. Try to add your IP to whitelist in hosting admin panel.

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

    for users running VS2013

    In windows 10.

    Check if apache service is running. since it gets replaced with World wide web service.

    run netstat -n to check this.

    stop the service. start apache. restart the service.

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

    Try this:

    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);
    }
    
    0 讨论(0)
  • 2020-11-29 07:34

    Sometime the problem could be on your windows firewall, make sure your server allow access to all port associated with your mysql database.

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

    Sometimes spacing and Order of parameters in connection string matters (based on personal experience and a long night :S)

    So stick to the standard format here

    Server=myServerAddress; Port=1234; Database=myDataBase; Uid=myUsername; Pwd=myPassword;

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