【MySQL】ubuntu16.04安装mysql,然后源码编译Qt5.12.4版本的libqsqlmysql.so

一、Ubuntu16.04.5 安装 MySQL

1、使用apt命令安装

sudo apt install mysql-server

安装过程会提示,推荐设置MySQL的root用户密码(注意:这里root不是指ubuntu系统的root)

	While not mandatory, it is highly recommended that you set a password for the MySQL administrative "root" user.
	If this field is left blank, the password will not be changed.
	Net password for the MySQL "root" user:

2、安全设置

使用mysql_secure_installation配置mysql安全选项

$ sudo mysql_secure_installation 

Securing the MySQL server deployment.

Enter password for user root: 

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: y	// 配置验证密码强度的插件

There are three levels of password validation policy:	//密码验证策略有三个级别:

// 长度大于8
LOW    Length >= 8	
// 长度大于8,密码由数字、大小写字母和特殊字符组成
MEDIUM Length >= 8, numeric, mixed case, and special characters	
// 长度大于8,密码由数字、大小写字母和特殊字符组成,并且任意连续4个及以上的字母不能是字典中的单词
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file 

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1
Using existing password for root.

Estimated strength of the password: 50 
Change the password for root ? (Press y|Y for Yes, any other key for No) : n 
// 是否更改root密码,我在安装MySQL时,root密码够复杂,这里不再更改

 ... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y	//删除匿名用户
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y	// 不允许root用户通过网络连接数据库
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y // 删除test数据库,一般在正式部署之前删除它
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y // 上述更改立即生效
Success.

All done! 

3、登陆mysql

mysql -u root -p
Enter password:  //输入安装时设置的 mysql 的 root 密码

4、安装MySQL客户端

sudo apt install mysql-workbench	// 界面客户端
(1)运行mysql-workbench时报错:
~$ mysql-workbench 
/usr/lib/mysql-workbench/mysql-workbench-bin: symbol lookup error: /usr/lib/libgdal.so.1: 
	undefined symbol: sqlite3_column_table_name
(2)解决方法

我使用源码安装的sqlite3,进入sqlite3源码目录,执行卸载命令,再运行 mysql-workbench 就能正常启动了

.../sqlite-autoconf-3300100$ sudo make uninstall

注意以下是执行卸载命令时的打印信息

 ( cd '/usr/local/bin' && rm -f sqlite3 )
 ( cd '/usr/local/include' && rm -f sqlite3.h sqlite3ext.h )
 /bin/bash ./libtool   --mode=uninstall rm -f '/usr/local/lib/libsqlite3.la'
libtool: uninstall: rm -f /usr/local/lib/libsqlite3.la /usr/local/lib/libsqlite3.so.0.8.6 /usr/local/lib/libsqlite3.so.0 /usr/local/lib/libsqlite3.so /usr/local/lib/libsqlite3.a
 ( cd '/usr/local/share/man/man1' && rm -f sqlite3.1 )
 ( cd '/usr/local/lib/pkgconfig' && rm -f sqlite3.pc )

忘记当初为什么要用源码编译sqlite3?使用 sqlite-autoconf-3300100 进行源码编译,这个sqlite3的版本是3.30.1;
最后再用命令重新安装sqlite3:sudo apt install sqlite3; 版本是3.11.0

找到升级sqlite3的原因了:
查看以前sqlite3的笔记:【数据库】sqlite3数据库备份、导出方法汇总
其中有记录,使用SQL语句“VACUUM INTO”对sqlite数据库进行备份,要求:SQLite 版本至少3.27.0 (2019-02-07)

二、源码编译libqsqlmysql.so

1、Qt5.12.4及以后版本没有 libqsqlmysql.so

在Qt中使用“QMYSQL”,报错:QSqlDatabase: QMYSQL driver not loaded

2、 下载 MySQL 开发套件(头文件)

sudo apt install libmysqlclient-dev	

3、 去 Qt5.12.4 源码中编译 libqsqlmysql.so 插件

插件源码在: Qt安装目录/Qt5.12.4/5.12.4/Src/qtbase/src/plugins/sqldrivers/mysql

(1)进入mysql插件源码目录
cd Qt安装目录/Qt5.12.4/5.12.4/Src/qtbase/src/plugins/sqldrivers/mysql
(2)修改 mysql.pro 添加 mysql 头文件路径和链接库

编辑 mysql.pro

TARGET = qsqlmysql

HEADERS += $$PWD/qsql_mysql_p.h
SOURCES += $$PWD/qsql_mysql.cpp $$PWD/main.cpp

#注释掉QMAKE_USE
#QMAKE_USE += mysql

OTHER_FILES += mysql.json

PLUGIN_CLASS_NAME = QMYSQLDriverPlugin
include(../qsqldriverbase.pri)

# 编译生成库的路径设置为当前目录下lib中,一定要在include(../qsqldriverbase.pri)下面定义,
# 否则会被覆盖,安装到 ../plugins/sqldrivers/  中
DESTDIR = $$PWD/lib

# 添加mysql头文件和库
INCLUDEPATH += /usr/include/mysql
LIBS += -lmysqlclient
(3)开始编译

执行 qmake mysql.pro 报错:

Cannot read  /5.12.4/Src/qtbase/src/plugins/sqldrivers/qtsqldrivers-config.pri:-1: 
	error: No such file or directory

进入上级目录 sqldrivers 中,执行 qmake sqldrivers.pro ;将会生成qtsqldrivers-config.pri;
再次进入mysql目录中,执行 qmake mysql.pro ;不再报错
编译:make ;将会在lib中生成库:libqsqlmysql.so libqsqlmysql.so.debug

(4) 安装到Qt中

将 libqsqlmysql.so 拷贝到Qt数据库插件所在目录中:

cp libqsqlmysql.so Qt安装目录/Qt5.12.4/5.12.4/gcc_64/plugins/sqldrivers/

Just Do It !

你可能感兴趣的:(Qt,数据库SQL)