tags:

views:

63

answers:

2

Hello,

I am trying to connect to a local MySQL server, but cannot find out how to select if QSqlDatabase should connect through a socket or port. Qt is version 4.6.3. My simple test code is as follows:

QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setPort(3306);
db.setDatabaseName("rem");
db.setUserName("test");
db.setPassword("test");

if (!db.open()) {
    qDebug() << db.lastError();
}

This gives me:

QSqlError(2002, "QMYSQL: Unable to connect",
    "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)")

As you can see it wants to connect using the default socket, which I don't want.

I found db.setConnectOptions("UNIX_SOCKET=blabla"), with which you can set the socket. Unfortunately I cannot figure out how to unset it.

+1  A: 

use db.setHostName("127.0.0.1"); instead.

Lars
Thank you. This works. Could you elaborate why this happens? I am thinking about how to let the user input the server address.
mrothe
From the MySql Manual: On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. This occurs even if a --port or -P option is given to specify a port number. To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of 127.0.0.1, or the IP address or name of the local server. You can also specify the connection protocol
Lars
A: 

Thanks for the solution ...I really appreciate this.

Regards

hami