Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Cannot connect to MySQL server
QtWS25 Last Chance

Cannot connect to MySQL server

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 5 Posters 6.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    t0msk
    wrote on last edited by
    #1

    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? :/

    Student who loves C/C++

    K 1 Reply Last reply
    0
    • T t0msk

      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? :/

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @t0msk

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

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • T Offline
        T Offline
        t0msk
        wrote on last edited by
        #3

        I added:

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

        And i got true, so driver is probably ok.

        Student who loves C/C++

        K 1 Reply Last reply
        0
        • T t0msk

          I added:

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

          And i got true, so driver is probably ok.

          K Offline
          K Offline
          koahnig
          wrote on last edited by koahnig
          #4

          @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.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • T Offline
            T Offline
            t0msk
            wrote on last edited by
            #5

            I got an error:

            error: cannot find -llibmysql
            

            Why there is " - " in:

            LIBS += -llibmysql
            

            ?

            Student who loves C/C++

            K 1 Reply Last reply
            0
            • M Offline
              M Offline
              mateczek
              wrote on last edited by mateczek
              #6

              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!!!

              1 Reply Last reply
              1
              • T t0msk

                I got an error:

                error: cannot find -llibmysql
                

                Why there is " - " in:

                LIBS += -llibmysql
                

                ?

                K Offline
                K Offline
                koahnig
                wrote on last edited by
                #7

                @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?

                Vote the answer(s) that helped you to solve your issue(s)

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  t0msk
                  wrote on last edited by
                  #8

                  @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.

                  Student who loves C/C++

                  K 1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mateczek
                    wrote on last edited by
                    #9

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

                    on ubuntu You must install

                    sudo apt-get install libmysqlclient-dev
                    
                    T 2 Replies Last reply
                    1
                    • M mateczek

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

                      on ubuntu You must install

                      sudo apt-get install libmysqlclient-dev
                      
                      T Offline
                      T Offline
                      t0msk
                      wrote on last edited by
                      #10

                      @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 :)

                      Student who loves C/C++

                      1 Reply Last reply
                      0
                      • T t0msk

                        @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.

                        K Offline
                        K Offline
                        koahnig
                        wrote on last edited by
                        #11

                        @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.

                        Vote the answer(s) that helped you to solve your issue(s)

                        T 1 Reply Last reply
                        1
                        • K koahnig

                          @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.

                          T Offline
                          T Offline
                          t0msk
                          wrote on last edited by
                          #12

                          @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 :)

                          Student who loves C/C++

                          1 Reply Last reply
                          0
                          • M mateczek

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

                            on ubuntu You must install

                            sudo apt-get install libmysqlclient-dev
                            
                            T Offline
                            T Offline
                            t0msk
                            wrote on last edited by t0msk
                            #13

                            @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
                            

                            Student who loves C/C++

                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              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.

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              0
                              • T Offline
                                T Offline
                                t0msk
                                wrote on last edited by
                                #15

                                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?

                                Student who loves C/C++

                                jsulmJ 1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

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

                                  Interested in AI ? www.idiap.ch
                                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  1 Reply Last reply
                                  0
                                  • T t0msk

                                    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?

                                    jsulmJ Offline
                                    jsulmJ Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #17

                                    @t0msk said in Cannot connect to MySQL server:

                                    Where should be $QTDIR

                                    Where your Qt installation containing src subfolder is.

                                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    1 Reply Last reply
                                    0

                                    • Login

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • Users
                                    • Groups
                                    • Search
                                    • Get Qt Extensions
                                    • Unsolved