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");
#------------------------------------------------- # # 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? :/
-
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.
-
https://youtu.be/0qjcVg-8Vgw?t=210
polish language but you must have :- download mysql c connector
- 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) - 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!!!
-
@mateczek said in Cannot connect to MySQL server:
https://youtu.be/0qjcVg-8Vgw?t=210
polish language but you must have :- download mysql c connector
- 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) - 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:
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.
-
@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
-
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?
-
Did you download the sources from your Qt version ? It can be done through the installer.
-
@t0msk said in Cannot connect to MySQL server:
Where should be $QTDIR
Where your Qt installation containing src subfolder is.