Results 1 to 11 of 11

Thread: Cannot connect to MySQL on localhost

  1. #1
    Join Date
    May 2011
    Posts
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Unhappy Cannot connect to MySQL on localhost

    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

  2. #2
    Join Date
    Jul 2010
    Location
    Indonesia
    Posts
    83
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Cannot connect to MySQL on localhost

    Please format your codes with code tags.
    Try change "localhost" to "127.0.0.1".
    ~ We are nothing in this universe ~

  3. #3
    Join Date
    May 2011
    Posts
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Cannot connect to MySQL on localhost

    Hi,

    I had tried 127.0.0.1 and 10.0.0.1 before posting this issue.

    Regards

    Zia

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Cannot connect to MySQL on localhost

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    May 2011
    Posts
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Cannot connect to MySQL on localhost

    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

  6. #6
    Join Date
    Aug 2009
    Location
    Greece
    Posts
    69
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Cannot connect to MySQL on localhost

    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.

  7. #7
    Join Date
    May 2011
    Posts
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Cannot connect to MySQL on localhost

    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
    Attached Files Attached Files
    Last edited by syedzia5; 9th September 2012 at 04:23.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Cannot connect to MySQL on localhost

    Are you able to connect to the server using a simple C program such as this?

    Qt Code:
    1. #include <mysql.h>
    2.  
    3. int main(int argc, char **argv) {
    4. MYSQL mysql;
    5.  
    6. mysql_init(&mysql);
    7. if(!mysql_connect(&mysql, "localhost", "root", NULL)) {
    8. printf("%s\n", mysql_error(&mysql));
    9. return 1;
    10. }
    11. printf("Connection ok\n");
    12. return 0;
    13. }
    To copy to clipboard, switch view to plain text mode 

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

    or equivalent.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    May 2011
    Posts
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Cannot connect to MySQL on localhost

    It didn't work It didn't connect I tried following code:

    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <winsock2.h>
    3. #include <ws2tcpip.h>
    4. #include <stdio.h>
    5. #include <mysql.h>
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. QCoreApplication a(argc, argv);
    10. MYSQL mysql;
    11.  
    12. mysql_init(&mysql);
    13. if(!mysql_real_connect(&mysql, "localhost", "root", "zrsyed15967", "test", 3306,NULL,CLIENT_MULTI_STATEMENTS)) {
    14. printf("%s\n", mysql_error(&mysql));
    15. return 1;
    16. }
    17.  
    18. return a.exec();
    19. }
    To copy to clipboard, switch view to plain text mode 

    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.

  10. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Cannot connect to MySQL on localhost

    "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/...l-connect.html). To force a TCP connection you must use "127.0.0.1" or another name that resolves to the machine.

  11. #11
    Join Date
    Aug 2009
    Location
    Greece
    Posts
    69
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Cannot connect to MySQL on localhost

    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/...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
    Qt Code:
    1. mysqladmin -h 127.0.0.1 version variables
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. cannot connect to mysql in QT 4.7.2
    By vahidsamimi in forum Installation and Deployment
    Replies: 3
    Last Post: 5th September 2012, 13:55
  2. Replies: 13
    Last Post: 17th June 2011, 17:19
  3. How to connect Qt with MYSQL??
    By Gokulnathvc in forum Newbie
    Replies: 10
    Last Post: 24th March 2011, 00:52
  4. Qt connect different MySQL
    By weixj2003ld in forum Qt Programming
    Replies: 0
    Last Post: 5th August 2009, 08:35
  5. How to connect MySQL with QT
    By diego in forum Qt Programming
    Replies: 0
    Last Post: 27th May 2009, 05:34

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.