0

I have this code:

using MySql.Data.MySqlClient;

namespace DigitalWilds.Models


{
    public class DatabaseConn
    {
        public string status = "failed";
        MySqlConnection conn;
        public string connectionString;
        public DatabaseConn()
        {               
            string webHost = "(host IP)";            
            string dataBase = "DB";            
            string userID = "UID";
            string password = "PASS";
            string port = "3306";

            
            connectionString = "Server="+webHost+";Port="+port+";Database=" + dataBase+ ";Uid ="+userID+";Pwd="+password;
            

            try
            {
                conn = new MySqlConnection();
                conn.ConnectionString = connectionString;
                conn.Open();
                status = "success";
                conn.Close(); conn.Dispose();

            }
            catch (MySqlException ex)
            {
                status = ex.Message;
            }
            
        }

    }
}

The error message is :

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

I am trying to initiate a connection to my DB server. I keep ending up in the catch block. I have used, almost ver batum, the code from here: https://dev.mysql.com/doc/connector-net/en/connector-net-connections-string.html. I now use the string format from this site: https://www.connectionstrings.com/mysql/ I am spitting out both status and connectionString. status comes back as "Unable to connect to any of the specified MySQL hosts." and connectionString comes back as what I expect it. I have tried with and without the port, I even also tried ip:port just to see. What am I doing wrong?
Edit:
It was pointed out that I can use Exception.ToString. Doing so gives:

MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts. ---> MySql.Data.MySqlClient.MySqlException (0x80004005): Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. ---> System.TimeoutException: The operation has timed out. at MySql.Data.Common.StreamCreator.GetTcpStreamAsync(MySqlConnectionStringBuilder settings, CancellationToken cancellationToken, Boolean execAsync) at MySql.Data.Common.StreamCreator.GetStreamAsync(MySqlConnectionStringBuilder settings, CancellationToken cancellationToken, Boolean execAsync) at MySql.Data.MySqlClient.NativeDriver.OpenAsync(Boolean execAsync, CancellationToken cancellationToken) at MySql.Data.MySqlClient.NativeDriver.OpenAsync(Boolean execAsync, CancellationToken cancellationToken) at MySql.Data.MySqlClient.Driver.OpenAsync(Boolean execAsync, CancellationToken cancellationToken) at MySql.Data.MySqlClient.Driver.CreateAsync(MySqlConnectionStringBuilder settings, Boolean execAsync, CancellationToken cancellationToken) at MySql.Data.MySqlClient.Driver.CreateAsync(MySqlConnectionStringBuilder settings, Boolean execAsync, CancellationToken cancellationToken) at MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnectionAsync(Boolean execAsync, CancellationToken cancellationToken) at MySql.Data.MySqlClient.MySqlPool.GetPooledConnectionAsync(Boolean execAsync, CancellationToken cancellationToken) at MySql.Data.MySqlClient.MySqlPool.TryToGetDriverAsync(Boolean execAsync, CancellationToken cancellationToken) at MySql.Data.MySqlClient.MySqlPool.GetConnectionAsync(Boolean execAsync, CancellationToken cancellationToken) at MySql.Data.MySqlClient.MySqlConnection.OpenAsync(Boolean execAsync, CancellationToken cancellationToken) at MySql.Data.MySqlClient.MySqlConnection.Open() at DigitalWilds.Models.DatabaseConn..ctor() in C:\Users\Me\source\repos\DigitalWilds\Models\DatabaseConn.cs:line 29

Son of Man
  • 1,213
  • 2
  • 7
  • 27
Tyler
  • 35
  • 9
  • 2
    What's the actual error? Right now you haven't provided any information. Nobody can guess what's wrong without that (except the obvious connection leak). Does the error says you're connecting to the wrong server or that the username or password are wrong perhaps? Can you connect to MySQL with those settings using eg MySQL Workbench? – Panagiotis Kanavos May 25 '23 at 06:44
  • @PanagiotisKanavos I switched to a MySqlException, and ex.Message gives "Unable to connect to any of the specified MySQL hosts." Sorry about that. – Tyler May 25 '23 at 06:58
  • Please [edit] the question. That is crucial info but also not something we can fix as you know which hosts you have with MySql on it. Does the host have a firewall? – rene May 25 '23 at 06:59
  • 1
    That partial message says the server name is wrong. Or MySQL isn't even running. The rest of the exception probably contains more information. Put the entire exception text in the question, not just parts of the message. You can get the full text easily with `Exception.ToString()` or by clicking on `Copy Details` in the exception popup while debugging – Panagiotis Kanavos May 25 '23 at 07:03
  • Since it was closed and I can't answer it. I talked with tech support 2 times. The first time was prior to my post here, just to clarify that all my connection information was correct, which it was. The second time I gave them my error. The issue was that each IP has to be manually given permission to access the DB. They added my IP and everything works now. The issue that this was marked as a duplicate of doesn't appear to be the same issue. – Tyler May 25 '23 at 07:40
  • If someone sees this that has the ability to reopen the post and wishes to answer with my above comment, I will mark it as solved. Kind of frustrating that it was closed so quickly without being able to get a solution posted. – Tyler May 25 '23 at 08:01

0 Answers0