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 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
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 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 last edited by
        #3

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

        1 Reply Last reply
        0
        • Cobra91151C Offline
          Cobra91151C Offline
          Cobra91151
          wrote on 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
          1
          • Cobra91151C Cobra91151

            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 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 :(

            Cobra91151C 1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 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
              1
              • H Hmmm

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

                Cobra91151C Offline
                Cobra91151C Offline
                Cobra91151
                wrote on 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
                1
                • Christian EhrlicherC Christian Ehrlicher

                  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 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
                  • Cobra91151C Cobra91151

                    @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 last edited by
                    #9

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

                    Cobra91151C 1 Reply Last reply
                    0
                    • H Hmmm

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

                      Cobra91151C Offline
                      Cobra91151C Offline
                      Cobra91151
                      wrote on 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
                      1
                      • Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on 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
                        0
                        • Christian EhrlicherC Christian Ehrlicher

                          @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 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
                          • Cobra91151C Cobra91151

                            @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 last edited by
                            #13

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

                            1 Reply Last reply
                            0
                            • Christian EhrlicherC Offline
                              Christian EhrlicherC Offline
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on 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 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

                                • Login

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