Results 1 to 6 of 6

Thread: connecting MySQL remotely

  1. #1

    Default connecting MySQL remotely

    Hello everybody!

    I have written a program that needs to connect to a mysql db installed on one of the computers in the network. All others computers in the network running the program should be able to connect to the db.

    I have a script that connect to the server, it looks like this:

    Qt Code:
    1. #ifndef CONNECTION_H
    2. #define CONNECTION_H
    3.  
    4. #include <QMessageBox>
    5. #include <QSqlDatabase>
    6. #include <QSqlError>
    7. #include <QSqlQuery>
    8. #include <QFile>
    9. #include <QTextStream>
    10. #include <QString>
    11.  
    12. static bool createConnection()
    13. {
    14. QFile file;
    15. file.setFileName("connection.txt");
    16. if (!file.open(QFile::ReadOnly| QFile::Text)) {
    17. QMessageBox::critical(0, qApp->tr("Error"),
    18. qApp->tr("Cannot open connection file."
    19. "Click Cancel to exit."), QMessageBox::Cancel);
    20. return false;
    21. }
    22.  
    23. QTextStream in(&file);
    24.  
    25. QString DatabaseName = in.readLine();
    26. QString UserName = in.readLine();
    27. QString Password = in.readLine();
    28. QString HostName = in.readLine();
    29.  
    30.  
    31. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    32. //db.setConnectOptions("CLIENT_SSL=1;CLIENT_IGNORE_SPACE=1");
    33. db.setPort (3306);
    34. db.setDatabaseName( DatabaseName );
    35. db.setUserName( UserName );
    36. db.setPassword( Password );
    37. db.setHostName( HostName );
    38.  
    39. if (!db.open()) {
    40. QMessageBox::critical(0, qApp->tr("Cannot open database"),
    41. qApp->tr("Unable to establish a database connection."
    42. "Click Cancel to exit."), QMessageBox::Cancel);
    43. return false;
    44. }
    45.  
    46. return true;
    47. }
    48.  
    49. #endif
    To copy to clipboard, switch view to plain text mode 

    The program works when running on the computer where MySQL is installed but fails to connect when running on all the computers in the network.

    I am using the ip address of the MySQL server as hostname.

    Hope that someone could help me out in this.

    Thanks in advance!

  2. #2
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: connecting MySQL remotely

    I don't know, whether it is a help. I don't have that much experience with MySQL, but quite some with Postgres. In Postgres you have to activate the remote access in the config files. With the default settings you can only have only local connections. It is a security feature. Perhaps it is the same with MySQL?

  3. #3

    Default Re: connecting MySQL remotely

    thanks for the reply. The MySQL server is setup to accept tcp/ip connection during the configuration. I do not see any other way in further configuring this. Am I missing something? By the way the database is running on windows machine and I am using MySQL 5.

  4. #4
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: connecting MySQL remotely

    As I said, I am not that experienced with MySQL. But since you can connect locally your code cannot be that wrong. I still suspect a permissions problem.

    A short google search:
    http://help.hardhathosting.com/question.php/87
    http://www.plus2net.com/articles/con...e_mysql_db.php

    Are you sure all your 'GRANTS' are correct?

  5. #5

    Default Re: connecting MySQL remotely

    Yes. the user is setup to access the db from any host and has the right privileges.

  6. #6
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: connecting MySQL remotely

    Ok, then I give up. Only one thing, can you connect without your Qt program remotely to your database? Using something like this:

    mysql -h <your remote ip> -u <your db user> -p

Similar Threads

  1. Crash while connecting to MySQL
    By vfernandez in forum Qt Programming
    Replies: 1
    Last Post: 9th January 2007, 14:05
  2. Qt 4.1.4 & Mysql 5 on Linux x64
    By bothapn in forum Installation and Deployment
    Replies: 7
    Last Post: 4th August 2006, 14:23
  3. connecting to MySQL through it's API
    By Philip_Anselmo in forum Installation and Deployment
    Replies: 20
    Last Post: 31st May 2006, 00:22
  4. Error connecting to MySQL
    By probine in forum Qt Programming
    Replies: 20
    Last Post: 24th February 2006, 00:13

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.