PDA

View Full Version : connecting MySQL remotely



rickym
24th March 2007, 15:00
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:


#ifndef CONNECTION_H
#define CONNECTION_H

#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QFile>
#include <QTextStream>
#include <QString>

static bool createConnection()
{
QFile file;
file.setFileName("connection.txt");
if (!file.open(QFile::ReadOnly| QFile::Text)) {
QMessageBox::critical(0, qApp->tr("Error"),
qApp->tr("Cannot open connection file."
"Click Cancel to exit."), QMessageBox::Cancel);
return false;
}

QTextStream in(&file);

QString DatabaseName = in.readLine();
QString UserName = in.readLine();
QString Password = in.readLine();
QString HostName = in.readLine();


QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
//db.setConnectOptions("CLIENT_SSL=1;CLIENT_IGNORE_SPACE=1");
db.setPort (3306);
db.setDatabaseName( DatabaseName );
db.setUserName( UserName );
db.setPassword( Password );
db.setHostName( HostName );

if (!db.open()) {
QMessageBox::critical(0, qApp->tr("Cannot open database"),
qApp->tr("Unable to establish a database connection."
"Click Cancel to exit."), QMessageBox::Cancel);
return false;
}

return true;
}

#endif

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!

Kumosan
24th March 2007, 17:04
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?

rickym
24th March 2007, 19:40
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.

Kumosan
24th March 2007, 20:01
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/connecting_to_remote_mysql_db.php

Are you sure all your 'GRANTS' are correct?

rickym
25th March 2007, 08:46
Yes. the user is setup to access the db from any host and has the right privileges.

Kumosan
25th March 2007, 09:55
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