qt5.10.1 ubuntu18.04 mysql安装与连接过程和遇到的问题

安装MySQL

sudo apt-get install mysql-server
sudo apt-get install mysql-client

问题1:

mysql -u root -p

mysql-5.7后安装完会自动生成密码,如果你直接敲回车会出现

ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: NO)

你需要的执行

sudo gedit /etc/mysql/debian.cnf

得到user和password,我这user显示的是debian-sys-maint再执行

mysql -u debian-sys-maint -p ##输入密码

进入Mysql后执行下面操作更改root的密码

use mysql;
update user set authentication_string=PASSWORD(“root”) where User=’root’; #更改密码root
update user set plugin=”mysql_native_password”;
flush privileges; #刷新
quit; 退出

现在可以用root用户登陆了

sudo /etc/init.d/mysql stop
sudo /etc/init.d/mysql start # 重启mysql
mysql -u root -p #输入刚才设置的密码root

配置QT

假设你已经装好qt并解决装完就出出现的LD错误
这里我装的是qt5.10.1

安装libmysqlclient-dev

sudo apt-get install libmysqlclient-dev
sudo apt-get install libqt5sql5-mysql

测试代码

include 
include 
include 
include 
include 
include 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    qDebug() << QCoreApplication::libraryPaths();
    qDebug() << QSqlDatabase::drivers()<"/home/outman/Qt5.10.1/5.10.1/gcc_64/plugins/sqldrivers/libqsqlmysql.so");
    QSqlDatabase db(QSqlDatabase::addDatabase("QMYSQL"));
    db.setHostName("127.0.0.1");
    db.setDatabaseName("student");
    db.setUserName("root");
    db.setPassword("root");
    if (!db.open()) /*测试数据库是否链接成功*/
    {
       QMessageBox::critical(0, QObject::tr("Databse Error"), db.lastError().text());
       qDebug() << db.lastError() << endl;
    }
    else
    {
       qDebug() << "Ok" << endl;
    }
    return 0;

问题2:
不出意外会出现

QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QPSQL QPSQL7
db open err: “Driver not loaded Driver not loaded”

解决方案:编译码源,把下面路径改成自己的qt源目录

cd /home/outman/Qt5.10.1/5.10.1/Src/qtbase/src/plugins/sqldrivers/mysql
sudo /home/outman/Qt5.10.1/5.10.1/gcc_64/bin/qmake   “INCLUDEPATH+=/usr/include/mysql” “LIBS+=-L/usr/lib/x86_64-linux-gnu/ -lmysqlclient” mysql.pro

会发现缺少qtsqldrivers-config.pri文件

cd..  #进入上一层sqldrivers目录
sudo /home/outman/Qt5.10.1/5.10.1/gcc_64/bin/qmake -- MYSQL_PREFIX=/usr/include/mysql
sudo make
sudo make install
make sub-mysql

就得到qtsqldrivers-config.pri了
再次执行

cd /home/outman/Qt5.10.1/5.10.1/Src/qtbase/src/plugins/sqldrivers/mysql
sudo /home/outman/Qt5.10.1/5.10.1/gcc_64/bin/qmake   “INCLUDEPATH+=/usr/include/mysql” “LIBS+=-L/usr/lib/x86_64-linux-gnu/ -lmysqlclient” mysql.pro
sudo make
sudo make install

把mysql目录下的子目录/plugins/sqldrivers/libqsqlmysql.so拷贝到qt目录

cd ../plugins/sqldrivers/
sudo cp libqsqlmysql.so /home/outman/Qt5.10.1/5.10.1/gcc_64/plugins/sqldrivers

可以用ldd看下Qt5.10.1/5.10.1/gcc_64/plugins/sqldrivers/libqsqlmysql.so 还少什么
再次运行测试程序得到

("/home/outman/Qt5.10.1/5.10.1/gcc_64/plugins", "/home/outman/build-untitled-Desktop_Qt_5_10_1_GCC_64bit-Debug")
("QSQLITE", "QMYSQL", "QMYSQL3", "QPSQL", "QPSQL7") 

Ok 

END

你可能感兴趣的:(qt管理系统)