1

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.

2
  • This code works with local database. I want to change it to work with online database (same as local one). Commented Jul 10, 2011 at 16:20
  • 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. Commented Jul 10, 2011 at 16:40

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I want my database to be available to all clients over internet. so I add record '%.%.%.%' on remote database access control of CPanel. I'm not sure if this is true or not ...
@Sorush: did you at least try to access your database from a remote client with an other tool ? Just to confirm credentials issues are not the problem here ...
I tried with MySQL admin and Query Browser, but any of them didn't connect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.