PDA

View Full Version : Cannot connect to MySQL on localhost



syedzia5
6th September 2012, 17:00
Hi,

I am trying to connect from Qt application(Windows) to MySQL(on the same m/c) with the following code but cannot connect:


qDebug() << QSqlDatabase::drivers();
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setDatabaseName("test");
db.setHostName("localhost");
db.setPort(3306);
db.setUserName("root");
db.setPassword("zrsyed15967");
if (!db.open()) {
qDebug()<<db.lastError();
QMessageBox::critical(0, tr("Cannot open database"),
tr("Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it."), QMessageBox::Cancel);
return;
}

This is the output I get:

("QSQLITE", "QMYSQL3", "QMYSQL", "QODBC3", "QODBC")
QSqlError(2003, "QMYSQL: Unable to connect", "Can't connect to MySQL server on 'localhost' (10061)")

================================================== ==============================

However when I try to connect to MySQL server via command line "mysql -u root -p -h localhost" it connects, here is the output:

Enter password: ***********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 44
Server version: 5.5.27 MySQL Community Server (GPL)

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

================================================== =================

Kindly help :(

Zia

viulskiez
7th September 2012, 02:12
Please format your codes with code tags.
Try change "localhost" to "127.0.0.1".

syedzia5
7th September 2012, 08:52
Hi,

I had tried 127.0.0.1 and 10.0.0.1 before posting this issue.

Regards

Zia

wysota
8th September 2012, 07:39
Is your MySql server listening on a TCP socket at all? The console may be using a local socket/pipe to connect to the server and not a TCP socket.

syedzia5
8th September 2012, 16:46
Hi Wyosota,

Sorry didn't quite get it.

Can you explain it in a bit more detail.

And how do I check the above to things?

Regards

Zia

Rhayader
8th September 2012, 21:55
In configuration file my.ini in windows or my.cnf in linux there is an option named "skip-networking" which is enabled by default for security reasons. Comment it by placing a # before so as to enable tcp-connection.

syedzia5
9th September 2012, 05:23
Hi RHayader,

It is off as can you can see in the attached output of the command

Hi RHayder,

The "skip-networking" is already OFF as can be seen from the attached output of 'myadmin variable -u root -p'

regards

Zia

Added after 11 minutes:

Hi,

Also find attached file countaining output of 'mysqladmin variable -u root -p'

Regards

Zia

wysota
9th September 2012, 12:12
Are you able to connect to the server using a simple C program such as this?


#include <mysql.h>

int main(int argc, char **argv) {
MYSQL mysql;

mysql_init(&mysql);
if(!mysql_connect(&mysql, "localhost", "root", NULL)) {
printf("%s\n", mysql_error(&mysql));
return 1;
}
printf("Connection ok\n");
return 0;
}

Compile with:
gcc main.c -I/usr/include/mysql -lmysqlclient -o testconnection

or equivalent.

syedzia5
10th September 2012, 16:57
It didn't work :( It didn't connect :( I tried following code:


#include <QtCore/QCoreApplication>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <mysql.h>

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MYSQL mysql;

mysql_init(&mysql);
if(!mysql_real_connect(&mysql, "localhost", "root", "zrsyed15967", "test", 3306,NULL,CLIENT_MULTI_STATEMENTS)) {
printf("%s\n", mysql_error(&mysql));
return 1;
}

return a.exec();
}


Since the mysql lib installation on my windows m/c doesn't has mysql_connect, I have used mysql_real_connect and this is used by QSqlDatabase class also.

The Library I am using is ConnectorC6.0.2.

ChrisW67
11th September 2012, 03:12
"It didn't work," is not a useful description. What was the error message printed? What have you done to diagnose the problem with your MySql instance?

This problem no longer has anything to do with Qt.

If the host name is "localhost" or NULL then a connection is attempted using shared memory (Windows) or local socket (UNIX) in preference to a TCP connection (http://dev.mysql.com/doc/refman/5.0/en/mysql-real-connect.html). To force a TCP connection you must use "127.0.0.1" or another name that resolves to the machine.

Rhayader
11th September 2012, 13:03
I agree that this has not got to do with Qt. There are many reasons that you cannot connect to MySQL server and most of them are referenced in http://dev.mysql.com/doc/refman/5.5/en/can-not-connect-to-server.html.
Maybe you are running more than one instance of MySQL server if you upgraded from a previous version. And as ChrisW67 suggested try
mysqladmin -h 127.0.0.1 version variables