Qt5.8连接MySQL数据库,会出现如下错误:
QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QPSQL QPSQL7
解决方法是对症下药,在网上找了很多教程都没有成功,下面的解决方法经过验证是可以的:
1 安装mysql
sudo apt-get install mysql-server
sudo apt-get isntall mysql-client
sudo apt-get install libmysqlclient-dev
2 查看哪些依赖库是缺失的
其实无法启动的真正原因是依赖库确实,而不是缺少MySQL驱动。
用命令行打开如下文件夹,根据自己的情况做调整:
cd /opt/Qt5.8/5.8/gcc_64/plugins/sqldrivers
输入:
ldd libqsqlmysql.so
会看到类似下面的内容:
qking@qkingros:/opt/Qt5.8/5.8/gcc_64/plugins/sqldrivers$ ldd libqsqlmysql.so
linux-vdso.so.1 => (0x00007ffeb19d7000)
libQt5Sql.so.5 => /opt/Qt5.8/5.8/gcc_64/plugins/sqldrivers/./../../lib/libQt5Sql.so.5 (0x00007f98b97b4000)
libQt5Core.so.5 => /opt/Qt5.8/5.8/gcc_64/plugins/sqldrivers/./../../lib/libQt5Core.so.5 (0x00007f98b9093000)
libmysqlclient.so.18 => /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18 (0x00007f98b8b33000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f98b8915000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f98b86fb000)
libssl.so.10 => not found
libcrypto.so.10 => not found
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f98b84f6000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f98b81f2000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f98b7eec000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f98b7cd5000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f98b790d000)
libicui18n.so.56 => /opt/Qt5.8/5.8/gcc_64/plugins/sqldrivers/./../../lib/libicui18n.so.56 (0x00007f98b7474000)
libicuuc.so.56 => /opt/Qt5.8/5.8/gcc_64/plugins/sqldrivers/./../../lib/libicuuc.so.56 (0x00007f98b70bb000)
libicudata.so.56 => /opt/Qt5.8/5.8/gcc_64/plugins/sqldrivers/./../../lib/libicudata.so.56 (0x00007f98b56d8000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f98b54d0000)
libgthread-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgthread-2.0.so.0 (0x00007f98b52cd000)
libglib-2.0.so.0 => /lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007f98b4fc5000)
/lib64/ld-linux-x86-64.so.2 (0x0000560f0cd61000)
libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f98b4d86000)
你会发现有几项依赖库是缺失的:
libssl.so.10 => not found
libcrypto.so.10 => not found
这就是问题所在。
3 安装缺失的依赖
针对我的错误安装,如果有其他错误可以自行百度或者参考最后的链接。还可以看一下下一篇。
sudo apt-get update
sudo apt-get install libssl1.0.0 libssl-dev
然后建立连接:
cd /lib/x86_64-linux-gnu
sudo ln -s libssl.so.1.0.0 libssl.so.10
sudo ln -s libcrypto.so.1.0.0 libcrypto.so.10
这样就可以了,再次执行:
ldd libqsqlmysql.so
就没有缺失的依赖了。