1. 下载Mysql免安装包
Mysql官网地址–> MySQL Community Server -->
下载Windows (x86,64bit)ZIP
2. 配置Mysql数据库
2.1 将文件复制到需要安装的目录
我这准备安装在
D:\uninstall\mysql-8.0.29-winx64
目录
2.2 配置my.ini文件
在D:\uninstall\mysql-8.0.29-winx64\bin
目录创建my.ini文件,内容如下:
注意:basedir=D:\\uninstall\\mysql-8.0.29-winx64
这个目录是mysql文件的目录(此处需根据文件路径更改)
[mysql]# 设置mysql客户端默认字符集default-character-set=utf8 [mysqld]#设置3306端口port = 3306 # 设置mysql的安装目录basedir=D:\\uninstall\\mysql-8.0.29-winx64# 允许最大连接数max_connections=200# 服务端使用的字符集默认为8比特编码的latin1字符集character-set-server=utf8# 创建新表时将使用的默认存储引擎default-storage-engine=INNODB
3. 初始化及启动mysql服务
以管理员的方式打开cmd命令窗口(直接打开cmd运行,可能会报错),并且进入到mysql安装目录的bin目录下。cd D:\uninstall\mysql-8.0.29-winx64\bin.\mysqld -install
初始化
.\mysqld --initialize
启动mysql服务
net start mysql
4. 测试是否能连接到mysql数据库
此处会让输入密码
Enter password:
, 密码为初始化服务时自动生成,存在于"D:\uninstall\mysql-8.0.29-winx64\data\xxxx.err"
文件中,打开文件查找password,可以看到初始密码为:-cmRwxffH1Pc
.\mysql -uroot -p
出现如下界面代码mysql安装成功:
注:
修改数据密码root:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';
5. 使用Qt编译mysql的驱动
找到qt源代码目录下得mysql.pro,使用Qt Creator打开,构建报错
修改qsqldriverbase/qsqldriverbase.pri
注释第4行的:
include($$shadowed($$PWD)/qtsqldrivers-config.pri)
添加:include(./configure.pri)
QT = core core-private sql-private# For QMAKE_USE in the parent projects.# include($$shadowed($$PWD)/qtsqldrivers-config.pri)include(./configure.pri)PLUGIN_TYPE = sqldriversload(qt_plugin)DEFINES += QT_NO_CAST_TO_ASCII QT_NO_CAST_FROM_ASCII
修改mysql.pro文件
(1) 注释:# QMAKE_USE += mysql
(2) 添加如下内容:
LIBS += D:\uninstall\mysql-8.0.29-winx64\lib\libmysql.libINCLUDEPATH += D:\uninstall\mysql-8.0.29-winx64\includeDEPENDPATH += D:\uninstall\mysql-8.0.29-winx64\includeDESTDIR=../mysql/driver
修改后mysql.pro文件内容如下:
TARGET = qsqlmysqlHEADERS += $$PWD/qsql_mysql_p.hSOURCES += $$PWD/qsql_mysql.cpp $$PWD/main.cpp# QMAKE_USE += mysqlOTHER_FILES += mysql.jsonPLUGIN_CLASS_NAME = QMYSQLDriverPlugininclude(../qsqldriverbase.pri)LIBS += D:\uninstall\mysql-8.0.29-winx64\lib\libmysql.libINCLUDEPATH += D:\uninstall\mysql-8.0.29-winx64\includeDEPENDPATH += D:\uninstall\mysql-8.0.29-winx64\includeDESTDIR=../mysql/driver
6.构建生成
生成的文件在mysql.pro同级目录的driver文件夹中
7.配置Qt开发环境
(1) 拷贝qsqlmysql.dll和qsqlmysqld.dll到D:\install\Qt\5.15.2\msvc_64\plugins\sqldrivers
目录下
(2) 拷贝D:\uninstall\mysql-8.0.29-winx64\lib
目录下的libmysql.dll,到D:\install\Qt\5.15.2\msvc_64\bin
目录
8.使用Qt Demo测试是否成功配置mysql
需要先使用终端连接到mysql数据库:
use mysqllcreate database test;show databases;
确定存在一个test的数据库即可
新建一个console的qt项目,需改.pro文件:
QT += core gui sql
,添加sql模块
main文件内容如下:
#include <QCoreApplication>#include <QDebug>#include <QSqlDatabase>#include <QSqlQuery>#include <QStringList>int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);// 输出可用数据库qDebug() << "Available drivers:";QStringList drivers = QSqlDatabase::drivers();foreach (QString driver, drivers)qDebug() << driver;// 打开MySQLQSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");db.setHostName("localhost");db.setDatabaseName("test");db.setUserName("root");db.setPassword("root");if (!db.open())qDebug() << "Failed to connect to mysql";elseqDebug() << "Success to connect to mysql";QSqlQuery query(db);//注意这里varchar一定要指定长度,不然会出错query.exec("create table Stu(id int primary key,name varchar(20))");query.exec("insert into Stu values(1,'student1')");query.exec("insert into Stu values(2,'student2')");query.exec("insert into Stu values(3,'student3')");query.exec("select id,name from Stu where id >= 2");while (query.next()) {int value0 = query.value(0).toInt();QString value1 = query.value(1).toString();qDebug() << value0 << value1;}return a.exec();}
输出内容如下代表成功:
如果觉得《免安装版mysql配置及使用Qt编译驱动》对你有帮助,请点赞、收藏,并留下你的观点哦!