Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • Hi, I'm trying to connect Qt with MySql and when I run my program I get this error:

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

    I use:
    macOS High Sierra
    XAMPP 7.4.4,
    Qt Creator 5.14.2

    Here is my code:

    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost"); //127.0.0.1
    db.setUserName("root");
    db.setPassword("");
    db.setDatabaseName("monitoringDB");
    
    if (!db.open()) {
       qDebug() << "Couldn't open db";
       qDebug() << "Error: " << db.lastError().text();
    } else {
       qDebug() << "DB opened!";
    }
    

    I have XAMPP started up and running, including MySql database. I also checked that my databaseName, userName and password are correct.

    I tried to google the problem but people usually refer to MySql installed separatelly, not within Xampp and their solutions doesn't apply to my case (at least I couldn't sort it out).

    Regarding the error itself in my case '/tmp/mysql.sock' doesn't exist in the filesystem. I cannot see even '/tmp/'

    Can anybody help me please with this because I'm out of ideas at this point..
    Additionally can you say from your experience if using Xampp with Qt is a good idea? or maybe I should install all components (Apache, MySql, PHP) individually?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    How exactly did you configure MySQL ?
    Can you connect to it from the command line ?



  • Installing Xampp doesn't require any additional configuration of MySql. However I did use brew install mysql-client and it was successful. After that I compiled qt sources: sqldrivers.pro and mysql.pro (I added two lines to mysql.pro file: INCLUDEPATH and QMAKE_LIBDIR). Afterwards I did "make" to all. As a result I had libqsqlmysql.dylib and I copied it into .../clang_64/plugins/sqldrivers. I've checked and libqsqlmysql.dylib refers to libmysqlclient.21.dylib which is correct (this is the one I have).

    When I type mysql -u root -p in the terminal I get:
    ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

    Now I also try to do the same project on Ubuntu to see how it goes, but this time I do not use Xampp but I am installing and configuring everything separately. Do you think it is a better approach? Is it Xampp which is making things complicated?


  • Lifetime Qt Champion

    @JarekT said in Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) QMYSQL: Unable to connect:

    Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

    Check your MySQL configuration to see where that socket is configured to be.



  • How can I check it??
    What I was able to find where mysql.sock is. I have it at:
    /Users/jarek/.bitnami/stackman/machines/xampp/volumes/root/var/mysql/mysql.sock

    I tried to do a symbolic link of this file to /tmp/mysql.sock but it didn't work..



  • I found that I can do some configuration in my.cnf file (location: /Users/jarek/.bitnami/stackman/machines/xampp/volumes/root/etc/my.cnf). I have read somewhere that I need to copy it to /etc/my.cnf to have a possibility to change it. So I copied it there. Afterwards I changed the following:
    socket =/opt/lampp/var/mysql/mysql.sock -->> changed to -->> socket =/Users/jarek/.bitnami/stackman/machines/xampp/volumes/root/var/mysql/mysql.sock
    and
    plugin_dir=/opt/lampp/lib/mysql/plugin/ -->> changed to -->> plugin_dir=/Users/jarek/.bitnami/stackman/machines/xampp/volumes/root/lib/mysql/plugin/

    Now when I do sudo mysql I have:
    ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/Users/jarek/.bitnami/stackman/machines/xampp/volumes/root/var/mysql/mysql.sock' (61) so it didn't help...


Log in to reply