0

I am creating website in ASP.NET Core 2.0 and now I need to get data from my mysql database.

Database is located on hosting which use cpanel

In past I have created simple REST API which get some data from that database and it used this config:

server: db5.cpanelhosting.rs
Port: 3306
user: termodom_guest
pw: a1s2d3f4g5h6j7k8

Now problem is when I use code like this:

using (MySqlConnection con = new MySqlConnection("server=217.26.215.19;port=3306;database=termodom_TDMajstor;user=termodom_guest;password=a1s2d3f4g5h6j7k8"))
{
    con.Open();
    using (MySqlCommand cmd = new MySqlCommand("SELECT * FROM KORISNIK", con))
    {
        MySqlDataReader dr = cmd.ExecuteReader();

        if(dr.Read())
        {
            return true;
        }
    }
}

It drops me error

MySql.Data.MySqlClient.MySqlException (0x80004005): Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. at MySql.Data.Common.StreamCreator.GetTcpStream(MySqlConnectionStringBuilder settings) at MySql.Data.MySqlClient.NativeDriver.Open().

I have also tried online testing connection but it also cannot connect. What to do?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Aleksa Ristic
  • 2,394
  • 3
  • 23
  • 54
  • Do you see an error? Also, you should remove your real server connection details. – Blair Jul 20 '18 at 10:02
  • I will leave it for now. I get error `MySqlException: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.` – Aleksa Ristic Jul 20 '18 at 10:03
  • Try to connect using MySQL workbench. If that fails, talk to your provider. See also: http://stackoverflow.com/questions/3475867/mysql-data-mysqlclient-mysqlexception-timeout-expired – Blair Jul 20 '18 at 10:05
  • 1
    Are you sure you want to expose your username/password? I'm guessing you are receiving the error on the `con.Open();` line, and the server is not responding. – kry Jul 20 '18 at 10:05
  • @kry it drops on `Open()` – Aleksa Ristic Jul 20 '18 at 10:05
  • If it was me I wouldn't have shared all your connection details publicly...they're not really necessary for people to be able to assist. I note though that in the details at the top you quote a hostname, whereas in the code you use an IP address. Any reason for the difference? Have you tried with the hostname? Also are you sure there is no firewall between where your code is running and the MySQL server? most hosting companies don't allow remote connections by default, you usually have to whitelist the IP addresses/hostnames of the clients, if it's allowed at all. – ADyson Jul 20 '18 at 10:27
  • @ADyson I have whitelisted it. I cannot find IP address of it. – Aleksa Ristic Jul 20 '18 at 10:34
  • " I have whitelisted it. I cannot find IP address of it." ...how can you whitelist "it" (which which I assume you mean your client machine where the .NET code runs?) if you don't know its IP? That also doesn't answer why, for the server config, you used a hostname in one place and an IP in another place – ADyson Jul 20 '18 at 10:42
  • Sorry to make confusion. I copy paste code so didn't see what i wrote. That IP is IP i "cannot get" - I think that is ip but when i go to `cmd` and `ping` that ip it doesn't return anything. I whitelisted it by adding `%` in cpanel mysql remote access – Aleksa Ristic Jul 20 '18 at 10:44
  • It's possible it _is_ reaching the server but the connection is taking too long to establish. Try increasing the connection timeout: https://dev.mysql.com/doc/dev/connector-net/6.10/html/P_MySql_Data_MySqlClient_MySqlConnection_ConnectionTimeout.htm – ADyson Jul 20 '18 at 12:35

0 Answers0