当前位置: > CentOS > CentOS故障 >

Centos 7 中 QT出现QSqlDatabase: MYSQL driver not loaded

时间:2016-04-22 21:36来源:linux.it.net.cn 作者:IT

系统版本 : Centos7
QT VERSION : 5.6

测试代码:

#include <iostream>
using namespace std;

#include <QApplication>
#include "gui/mainwindow.h"

#include <QSqlDatabase>
#include <QDebug>
#include <QSqlError>
#include <QSqlQuery>

using namespace std;

int main (int argc, char **argv) {
    //输出可用数据库
    QCoreApplication::addLibraryPath("/home/njq/dev_env/Qt5.6.0/5.6/gcc_64/plugins/sqldrivers");
    qDebug()<<"available drivers:";
    QStringList drivers = QSqlDatabase::drivers();
    foreach(QString driver, drivers)
        qDebug()<<driver;

    //打开MySQL
    QSqlDatabase data_base = QSqlDatabase::addDatabase("QMYSQL");
    data_base.setHostName("192.168.0.200");  //设置主机地址
    data_base.setPort(3306);  //设置端口
    data_base.setDatabaseName("alarmcenter");  //设置数据库名称
    data_base.setUserName("root");  //设置用户名
    data_base.setPassword("root");  //设置密码
    if(!data_base.open())
        qDebug()<<"failed to connect to mysql" << data_base.lastError ().text ();
    else {
        qDebug()<<"success";

        QString strSQL = "SELECT alarm_id FROM alarm;";
        QSqlQuery sql_query;
        sql_query.prepare (strSQL);
        sql_query.exec ();
        while (sql_query.next ()) {
            int id = sql_query.value (0).toInt ();
            qDebug () << id;
        }
    }
}

出现了如下的错误:

QSqlDatabase: MYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QPSQL QPSQL7
QSqlDatabase: an instance of QCoreApplication is required for loading driver plugins
failed to connect to mysql "Driver not loaded Driver not loaded"
查看QT的安装目录
~/dev_env/Qt5.6.0/5.6/gcc_64/plugins/sqldrivers


有一个文件:
    libqsqlmysql.so


使用ldd命令检查下依赖

[xxx@localhost sqldrivers]$ ldd libqsqlmysql.so
    linux-vdso.so.1 =>  (0x00007ffcf65b4000)
    libmysqlclient_r.so.16 => not found
    libz.so.1 => /lib64/libz.so.1 (0x00007f452c898000)

看到 libmysqlclient_r.so.16 这个so文件的依赖没有找到  通过查找资料,发现如下解决办法

重新编译MySQL驱动

[njq@localhost mysql]$ cd ~/dev_env/Qt5.6.0/5.6/Src/qtbase/src/plugins/sqldrivers/mysql
[xxx@localhost mysql]$ ~/dev_env/Qt5.6.0/5.6/gcc_64/bin/qmake  #因为没有配置qmake路径,所以用了全路径
[xxx@localhost mysql]$ make

看到编译后的最后一行

mv -f libqsqlmysql.so ../../../../plugins/sqldrivers/
把这个生成的libqsqlmysql.so覆盖到……./Qt5.6.0/5.6/gcc_64/plugins/sqldrivers

[xxx@localhost sqldrivers]$ mv libqsqlmysql.so  ~/dev_env/Qt5.6.0/5.6/gcc_64/plugins/sqldrivers/
然后再查看libqsqlmysql.so的依赖

[xxx@localhost sqldrivers]$ ldd libqsqlmysql.so
    linux-vdso.so.1 =>  (0x00007fffc437c000)
    libmysqlclient.so.18 => /usr/lib64/mysql/libmysqlclient.so.18 (0x00007f3de67bc000)
    libz.so.1 => /lib64/libz.so.1 (0x00007f3de65a5000)
    libcrypt.so.1 => /lib64/libcrypt.so.1 (0x00007f3de636e000)
    libnsl.so.1 => /lib64/libnsl.so.1 (0x00007f3de6155000)
    libssl.so.10 => /lib64/libssl.so.10 (0x00007f3de5ee7000)
    libcrypto.so.10 => /lib64/libcrypto.so.10 (0x00007f3de5aff000)
    libQt5Sql.so.5 => not found
    libQt5Core.so.5 => not found
    libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f3de58e2000)
    libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007f3de55da000)
    libm.so.6 => /lib64/libm.so.6 (0x00007f3de52d7000)
    libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f3de50c1000)
    libc.so.6 => /lib64/libc.so.6 (0x00007f3de4d00000)
    libdl.so.2 => /lib64/libdl.so.2 (0x00007f3de4afb000)
    libfreebl3.so => /lib64/libfreebl3.so (0x00007f3de48f8000)
    libgssapi_krb5.so.2 => /lib64/libgssapi_krb5.so.2 (0x00007f3de46ac000)
    libkrb5.so.3 => /lib64/libkrb5.so.3 (0x00007f3de43c6000)
    libcom_err.so.2 => /lib64/libcom_err.so.2 (0x00007f3de41c2000)
    libk5crypto.so.3 => /lib64/libk5crypto.so.3 (0x00007f3de3f90000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f3de6ee3000)
    libkrb5support.so.0 => /lib64/libkrb5support.so.0 (0x00007f3de3d80000)
    libkeyutils.so.1 => /lib64/libkeyutils.so.1 (0x00007f3de3b7c000)
    libresolv.so.2 => /lib64/libresolv.so.2 (0x00007f3de3962000)
    libselinux.so.1 => /lib64/libselinux.so.1 (0x00007f3de373c000)
    libpcre.so.1 => /lib64/libpcre.so.1 (0x00007f3de34db000)
    liblzma.so.5 => /lib64/liblzma.so.5 (0x00007f3de32b5000)

看到问题已经搞定

重新运行程序,看到如下结果

available drivers:
"QSQLITE"
"QMYSQL"
"QMYSQL3"
"QPSQL"
"QPSQL7"
success
171118
171119
171120
171121
171122
171123
171124
171125
171126
171127
171128
171129
171130
171131
171132
171133
171134
171135
171136

(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容