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 establish connection to MySQL database
Forum Update on Monday, May 27th 2025

Cannot establish connection to MySQL database

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 3 Posters 3.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.
  • H Offline
    H Offline
    Hmmm
    wrote on 30 Jun 2019, 12:54 last edited by
    #1

    I have a database created in MySQL Workbench called "mydb" and I used this code to connect with it:

    void MainWindow::on_pushButton_clicked()
    {
        QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");
        db.setHostName("localhost");
        db.setPort(3306);
        db.setUserName("root");
        db.setPassword(password);
        db.setDatabaseName("mydb");
    
        if(db.open())
        {
            QMessageBox::information(this,"Connection","Database connection succesful");
        }
        else{
            QMessageBox::information(this,"Not connected","Database connection not succesful");
        }
    }
    

    I also run

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

    to check if driver is available and it returns true. I also tested the connection on MySQL workbench to see if it was successful and it was.
    But when I run my app, it says: "Database connection not succesful"

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 30 Jun 2019, 13:59 last edited by
      #2

      You should check QSqlDatabase::lastError()

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • H Offline
        H Offline
        Hmmm
        wrote on 30 Jun 2019, 14:02 last edited by
        #3

        I did. It returns: QSqlError("", "", "")

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Cobra91151
          wrote on 30 Jun 2019, 14:03 last edited by
          #4

          Hello!

          I checked your code. The problem could be:

          1. If you are on Windows you must copy the libmysql.dll lib to the application directory.
          2. The database server is not started/working.

          Check it and it will work. Happy coding!

          H 1 Reply Last reply 30 Jun 2019, 14:25
          1
          • C Cobra91151
            30 Jun 2019, 14:03

            Hello!

            I checked your code. The problem could be:

            1. If you are on Windows you must copy the libmysql.dll lib to the application directory.
            2. The database server is not started/working.

            Check it and it will work. Happy coding!

            H Offline
            H Offline
            Hmmm
            wrote on 30 Jun 2019, 14:25 last edited by
            #5

            @Cobra91151 Thank you for your answer. I checked message log on MySQL, it says: 2019-06-30 16:15:59 - MySQL server is currently running
            Also I did copy libmysql.dll to the application directory but nothing helps :(

            C 1 Reply Last reply 30 Jun 2019, 14:43
            0
            • C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 30 Jun 2019, 14:27 last edited by
              #6

              Then please check with e.g. Dependency Walker if your qt mysql plugin really finds all necessary libraries.
              And where did you check for lastError?

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              H 1 Reply Last reply 30 Jun 2019, 14:57
              1
              • H Hmmm
                30 Jun 2019, 14:25

                @Cobra91151 Thank you for your answer. I checked message log on MySQL, it says: 2019-06-30 16:15:59 - MySQL server is currently running
                Also I did copy libmysql.dll to the application directory but nothing helps :(

                C Offline
                C Offline
                Cobra91151
                wrote on 30 Jun 2019, 14:43 last edited by
                #7

                @Hmmm

                Ok. Try to navigate to the application directory and check if it contains: sqldrivers directory with qsqlmysql.dll lib?

                H 1 Reply Last reply 30 Jun 2019, 15:00
                1
                • C Christian Ehrlicher
                  30 Jun 2019, 14:27

                  Then please check with e.g. Dependency Walker if your qt mysql plugin really finds all necessary libraries.
                  And where did you check for lastError?

                  H Offline
                  H Offline
                  Hmmm
                  wrote on 30 Jun 2019, 14:57 last edited by
                  #8

                  @Christian-Ehrlicher I checked for lastError in the MainWindow, its not written above as I tried that later.
                  About Dependeny Walker, I'm not very familiar with it, is this what I'm supposed to do:alt text

                  1 Reply Last reply
                  0
                  • C Cobra91151
                    30 Jun 2019, 14:43

                    @Hmmm

                    Ok. Try to navigate to the application directory and check if it contains: sqldrivers directory with qsqlmysql.dll lib?

                    H Offline
                    H Offline
                    Hmmm
                    wrote on 30 Jun 2019, 15:00 last edited by
                    #9

                    @Cobra91151 This is what my application directory contains:
                    0_1561906819643_969b9860-924a-4ccd-bd79-f1476b6ff7f6-image.png

                    C 1 Reply Last reply 30 Jun 2019, 15:06
                    0
                    • H Hmmm
                      30 Jun 2019, 15:00

                      @Cobra91151 This is what my application directory contains:
                      0_1561906819643_969b9860-924a-4ccd-bd79-f1476b6ff7f6-image.png

                      C Offline
                      C Offline
                      Cobra91151
                      wrote on 30 Jun 2019, 15:06 last edited by
                      #10

                      @Hmmm

                      It is source directory. Application directory is where your .exe file is present (Debug/Release dir)! That's why it can't locate the MySQL libs. Copy libmysql.dll into Debug/Release dir (it depends on your Qt Creator configuration) and start the program. It 100% must work :)

                      H 1 Reply Last reply 30 Jun 2019, 15:34
                      1
                      • C Offline
                        C Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on 30 Jun 2019, 15:22 last edited by
                        #11

                        @Hmmm said in Cannot establish connection to MySQL database:

                        I checked for lastError in the MainWindow, its not written above as I tried that later.

                        You have to call it after db.open()
                        And you should check the qt mysql plugin, not the mysql dll ... it's qsqlmysql.dll/qsqlmysqld.dll and the location is for sure not in your source tree...

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        H 1 Reply Last reply 30 Jun 2019, 15:32
                        0
                        • C Christian Ehrlicher
                          30 Jun 2019, 15:22

                          @Hmmm said in Cannot establish connection to MySQL database:

                          I checked for lastError in the MainWindow, its not written above as I tried that later.

                          You have to call it after db.open()
                          And you should check the qt mysql plugin, not the mysql dll ... it's qsqlmysql.dll/qsqlmysqld.dll and the location is for sure not in your source tree...

                          H Offline
                          H Offline
                          Hmmm
                          wrote on 30 Jun 2019, 15:32 last edited by
                          #12

                          @Christian-Ehrlicher Ok, about lastError, I put it after and it says:

                          QSqlError("2059", "QMYSQL: Unable to connect", "Authentication plugin 'caching_sha2_password' cannot be loaded: The specified module could not be found.\r\n")`
                          

                          And this is the ouput of qsqlmysql.dll in Dependeny Walker:

                          Error: At least one required implicit or forwarded dependency was not found.
                          Error: At least one module has an unresolved import due to a missing export function in an implicitly dependent module.
                          Error: Modules with different CPU types were found.
                          Error: A circular dependency was detected.
                          Warning: At least one delay-load dependency module was not found.
                          Warning: At least one module has an unresolved import due to a missing export function in a delay-load dependent module.
                          
                          1 Reply Last reply
                          0
                          • C Cobra91151
                            30 Jun 2019, 15:06

                            @Hmmm

                            It is source directory. Application directory is where your .exe file is present (Debug/Release dir)! That's why it can't locate the MySQL libs. Copy libmysql.dll into Debug/Release dir (it depends on your Qt Creator configuration) and start the program. It 100% must work :)

                            H Offline
                            H Offline
                            Hmmm
                            wrote on 30 Jun 2019, 15:34 last edited by
                            #13

                            @Cobra91151 Ok, my mistake. I did that now but still the same problem

                            1 Reply Last reply
                            0
                            • C Offline
                              C Offline
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on 30 Jun 2019, 15:42 last edited by
                              #14

                              @Hmmm said in Cannot establish connection to MySQL database:

                              Authentication plugin 'caching_sha2_password' cannot be loaded: The specified module could not be found.

                              So this is a mysql problem, don't see what Qt can do against it. Maybe google can help here.

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              1 Reply Last reply
                              2
                              • H Offline
                                H Offline
                                Hmmm
                                wrote on 30 Jun 2019, 15:58 last edited by
                                #15

                                Omg, I finally did it with running this SQL query:

                                ALTER USER 'yourusername'@'localhost' IDENTIFIED WITH mysql_native_password BY 'yourpassword';
                                

                                Thank you all for helping me :D

                                1 Reply Last reply
                                0

                                1/15

                                30 Jun 2019, 12:54

                                • Login

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