Cannot connect to MySQL server



  • Hello, I cannot connect to MySQL server using this code:

    loader.cpp:

    #include "QMessageBox"
    #include <QSqlDatabase>
    #include <QSqlDriver>
    #include <QSqlError>
    #include <QSqlQuery>
    #include "QDebug"
    
    QSqlDatabase db2 = QSqlDatabase::addDatabase("QMYSQL");
        db2.setHostName("127.0.0.1");
        db2.setDatabaseName("pve");
        db2.setUserName("root");
        db2.setPassword("test123");
        if(!db2.open())
            QMessageBox::critical(this,"Error","Unable to connect to the database");
    

    PVE.pro:

    #-------------------------------------------------
    #
    # Project created by QtCreator 2017-01-31T22:29:06
    #
    #-------------------------------------------------
    
    QT       += core gui sql
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    TARGET = PVE
    TEMPLATE = app
    CONFIG += thread
    
    
    SOURCES += main.cpp\
            loader.cpp \
        mainwindow.cpp \
        global.cpp
    
    HEADERS  += loader.h \
        mainwindow.h \
        global.h
    
    FORMS    += loader.ui \
        mainwindow.ui
    

    If I set same login details into MySQL workbench I will log with no problems.

    Can you help me please? :/


  • Moderators

    @t0msk

    you can check with QSqlDatabase::isDriverAvailable("QMYSQL") if the driver is available.



  • I added:

    bool test = QSqlDatabase::isDriverAvailable("QMYSQL");
    

    And i got true, so driver is probably ok.


  • Moderators

    @t0msk

    I have compared with my implementation the only difference was the driver test.

    Another thing is that you are not linking with an SQL library. In my case I have

    LIBS += -llibmysql
    

    However, that should give you already a link error.



  • I got an error:

    error: cannot find -llibmysql
    

    Why there is " - " in:

    LIBS += -llibmysql
    

    ?



  • https://youtu.be/0qjcVg-8Vgw?t=210
    polish language but you must have :

    1. download mysql c connector
    2. copy libmysql.dll to bin folder (start app from qtcreator)
      3 . copy libmysql.dll to build folder (start application from double clicked on exe file)
    3. copy plugin folder to folder with your program (start application from double clicked on exe file)

    In my project I never don't need this

    LIBS += -llibmysql
    

    to correctly connect to db!!!


  • Moderators

    @t0msk

    Sorry, did not think that this may work in your case. However, are you linking with a MySql library?
    I would guess so, because otherwise you should not be able to link and run the application.

    What is your operation system?



  • @mateczek said in Cannot connect to MySQL server:

    https://youtu.be/0qjcVg-8Vgw?t=210
    polish language but you must have :

    1. download mysql c connector
    2. copy libmysql.dll to bin folder (start app from qtcreator)
      3 . copy libmysql.dll to build folder (start application from double clicked on exe file)
    3. copy plugin folder to folder with your program (start application from double clicked on exe file)

    In my project I never don't need this

    LIBS += -llibmysql
    

    to correctly connect to db!!!

    Thank you it works ^^

    @koahnig said in Cannot connect to MySQL server:

    @t0msk

    Sorry, did not think that this may work in your case. However, are you linking with a MySql library?
    I would guess so, because otherwise you should not be able to link and run the application.

    What is your operation system?

    So problem is solved, my OS is Windows 7, but I wonder why SQL connector is not built in Qt, it is quite common thing.



  • on widnows You must download "mysql c connector" :(

    on ubuntu You must install

    sudo apt-get install libmysqlclient-dev
    


  • @mateczek said in Cannot connect to MySQL server:

    on widnows You must download "mysql c connector" :(

    on ubuntu You must install

    sudo apt-get install libmysqlclient-dev
    

    Good to know, because I have some Linux machines :)


  • Moderators

    @t0msk said in Cannot connect to MySQL server:

    So problem is solved, my OS is Windows 7, but I wonder why SQL connector is not built in Qt, it is quite common thing.

    Probably because of license issues it cannot be included in the distribution.



  • @koahnig said in Cannot connect to MySQL server:

    @t0msk said in Cannot connect to MySQL server:

    So problem is solved, my OS is Windows 7, but I wonder why SQL connector is not built in Qt, it is quite common thing.

    Probably because of license issues it cannot be included in the distribution.

    Ah ok, thank you :)



  • @mateczek said in Cannot connect to MySQL server:

    on widnows You must download "mysql c connector" :(

    on ubuntu You must install

    sudo apt-get install libmysqlclient-dev
    

    Sorry for opening older topic, but how to fix missing driver on Ubuntu? I run sudo apt-get install libmysqlclient-dev

    tomsk@tomsk-U36SD:~/Qt/5.7/gcc_64/plugins/sqldrivers$ sudo apt-get install libmysqlclient-dev
    Reading package lists... Done
    Building dependency tree       
    Reading state information... Done
    libmysqlclient-dev is already the newest version (5.7.17-0ubuntu0.16.04.1).
    0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
    tomsk@tomsk-U36SD:~/Qt/5.7/gcc_64/plugins/sqldrivers$
    

    And this is error from Qt

    QSqlDatabase: QMYSQL driver not loaded
    QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QPSQL QPSQL7
    

  • Lifetime Qt Champion

    Hi,

    Rebuild the Qt MySQL plugin.

    If you call ldd on the plugin you'll see that the linked libraries are not the same as the one you installed. That's normal, don't worry, just rebuild the plugin.



  • I never rebuilded plugin, how to do that?

    I found:

    cd $QTDIR/src/plugins/sqldrivers/mysql
    qmake "INCLUDEPATH+=/usr/local/include" "LIBS+=-L/usr/local/lib -lmysqlclient_r" mysql.pro
    make
    

    But

    tomsk@tomsk-U36SD:~$ cd $QTDIR/src/plugins/sqldrivers/mysql
    bash: cd: /src/plugins/sqldrivers/mysql: No such file or directory
    

    Where should be $QTDIR?


  • Lifetime Qt Champion

    Did you download the sources from your Qt version ? It can be done through the installer.


  • Moderators

    @t0msk said in Cannot connect to MySQL server:

    Where should be $QTDIR

    Where your Qt installation containing src subfolder is.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.