Important: Please read the Qt Code of Conduct -

[solved] MYSQL plugin is installed, but I can't connect to local database

  • Hi!I need to connect with my Qt application to a MySql database.

    So I compiled MySql plugin for Qt, and put result file ( into QT's plugins directory. It seems to work.

    I installed Xampp to run a local Mysql database, and i created a database named "Test". It seems to work.

    I modified the example project in QT, "Masterdetail (Music Archive) to connect with local database. I modified only the "database.h" file like this:

    #ifndef DATABASE_H
    #define DATABASE_H

    #include <QMessageBox>
    #include <QSqlDatabase>
    #include <QSqlError>
    #include <QSqlQuery>

    static bool createConnection()
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");

    if (! {
       qDebug() << db.lastError();
       QMessageBox::critical(0, qApp->tr("Cannot open database"),
                     "This example needs SQLite support. Please read "
                     "the Qt SQL driver documentation for information how "
                     "to build it.\n\n"
                     "Click Cancel to exit."), QMessageBox::Cancel);
        return false;


    I receive this error:
    QSqlError(2002, "QMYSQL: Unable to connect", "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")

    Where am I wrong?thanks

    O.S Ubuntu 10
    QT.Qt Creator 2.0.1
    Based on Qt 4.7.0 (32 bit)

  • Is your mysqld actually running?

    Can you connect on the command line? Check with

    mysql -u root -p -h localhost Test

  • I found the solution here: "Your text to link here...":

    In particular, I didn't have this folder " /var/run/mysqld"

    so i typed
    @sudo mkdir /var/run/mysqld
    and then
    sudo ln -s /opt/lampp/var/mysql/mysql.sock /var/run/mysqld/mysqld.sock
    As explained in post, and after this it worked!

    Thanks Volker for reply. Your command returns this message:

    Program "mysql" can be found in this packages:

    • mysql-client-core-5.1
    • mysql-cluster-client-5.1
      Try: sudo apt-get install <SELECTED PACKAGE>

Log in to reply