1
votes

I want to connect to a MySQL database from a Qt C++ application. Database is located on a host and is available on internet. I have the MySQL username, password and IP of host and a port number (that I don't know what is)

    Application::getInstance()->db = QSqlDatabase::addDatabase("QMYSQL");
    QSqlDatabase *db = &Application::getInstance()->db;
    db->setHostName("64.34.119.12");    // The string is IP address of my host (not real) 
    db->setDatabaseName("foo");
    db->setUserName("root");
    db->setPassword("password");
    db->open(); // returns false

I tried to pass IP address to db->setHostName() but not worked: Connection fails.

1
This code works with local database. I want to change it to work with online database (same as local one).sorush-r
You need to find our the port number and ensure the firewall isn't blocking it. Easy way is to try and telnet into the ip/port to determine if a connection can be made. If not then it's very likely a firewall issue.Andrew T Finnell

1 Answers

4
votes

Well, because you gave very little information, I cannot give you anything but a hunch, so here I go: Often webhosters configure there MySQL Databases to only accept connections from localhost (for a php or similar site, that is locate there, that's enough and that way it is more secure and abuse is less likely). That could be the reason why a connection from your machine is not accepted, though you have the right credentials.