【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 !

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章