一、背景:
使用MySQL5.6過程中,發現默認的加密插件為mysql_native_password。而sha256_password的安全程度要比mysql_native_password高,嘗試切換為sha256_password。
二、配置過程:
資料:
1、從MySQL官網查詢到服務器端sha256_password無需顯式加載,可以在MySQL配置文件中配置使能。
官網見:https://dev.mysql.com/doc/refman/5.6/en/sha256-pluggable-authentication.html
[mysqld]
default-authentication-plugin=sha256_password
2、據官網描述,要啟用插件,須通過ssl方式進行連接,也就是說需要配置相關證書。
實現過程:
1、下載MySQL安裝包,https://dev.mysql.com/downloads/mysql/5.6.html#downloads
】
2、安裝MySQL
下載的的MySQL是zip格式的,解壓到磁盤后,將my-default.ini另存為my.ini(此處看個人愛好,可不用),關於my.ini需要修改的地方如下:
1)basedir datadir port 需要根據自己使用情況配置。
2)配置默認啟用的加密插件。
3、簡單配置完mysql配置文件后,以管理員方式打開cmd,進入第二步解壓后的xxxmyql/bin目錄。
1)執行mysqld -install(tips:mysqld -remove 是卸載mysql),也可以執行服務名稱及配置文件路徑:mysqld install mysql5 --defaults-file="E:\mysql5.6\my.ini"
2)執行net start mysql5啟動MySQL服務(我的服務名稱為mysql5,上一步install時指定了服務名稱,如果install時沒指定默認就是MySQL)
3)輸入mysql -uroot -p連接數據庫。第一次進入沒有密碼,直接回車。
4)進入mysql數據庫,查詢user表的內容,發現默認使用的加密插件為mysql_native_password如下圖。
是不是my.ini配置的sha256_password沒有生效吶?創建一個用戶驗證下插件是否生效。執行:CREATE USER 'test01'@'localhost' IDENTIFIED BY 'password';發現默認插件是生效了的。但是默認的root為啥不是sha256_password呢,我猜想(只是猜想)
可能是假如root用戶默認為sha256_password,那么使用root連接的話,就需要配置相關證書,這樣使MySQL的安裝過程復雜且使用體驗降低。
使用新創建的用戶test01登錄數據庫,因為test01用戶使用了sha256_password,此時是登錄失敗的,提示身份驗證需要SSL加密。所以要使用了sha256_password插件的用戶是需要通過SSL加密的,也就是需要證書的。
5)補充一點:安裝完成后root用戶是沒有密碼的,要設置密碼可以執行mysqladmin -u root -p password,Enter password:直接回車,因為此時root時沒有密碼的,接着輸入及確認輸入自己的密碼。至此MySQL的安裝已經完成。
3.證書制作及使能
1)找一台安裝openssl的Linux的虛擬機,制作證書:(參考:https://segmentfault.com/a/1190000007819751)
a 生成一個 CA 私鑰:openssl genrsa 2048 > ca-key.pem
b 私鑰生成一個新的數字證書:openssl req -sha1 -new -x509 -nodes -days 3650 -key ca-key.pem > ca-cert.pem,執行過程中需要填寫一些內容,如國家、城市、郵箱等根據實際情況填寫。
c 創建服務側的私鑰及數字證書:openssl req -sha1 -newkey rsa:2048 -days 3650 -nodes -keyout server-key.pem > server-req.pem
此時會填寫一些內容如b步驟,其中有個密碼可以直接回車。
d 將生成的私鑰轉換為 RSA 私鑰文件格式:openssl rsa -in server-key.pem -out server-key.pem
e 使用CA 證書來生成一個服務器端的數字證書:openssl x509 -sha1 -req -in server-req.pem -days 3650 -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 > server-cert.pem
至此,服務端相關證書已創建成果,下面創建客戶端證書。
f 為客戶端生成一個私鑰和證書:openssl req -sha1 -newkey rsa:2048 -days 3650 -nodes -keyout client-key.pem > client-req.pem 需要填寫問題見步驟b
g 將生成的私鑰轉換為 RSA 私鑰文件格式:openssl rsa -in client-key.pem -out client-key.pem
h 為客戶端創建一個數字證書:openssl x509 -sha1 -req -in client-req.pem -days 3650 -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 > client-cert.pem
至此,客戶端和服務端證書全部創建完畢,生產八個文件如下:
2)配置證書
a 現在數據庫中是沒有開啟SSL的,執行命令查看:show variables like
'%ssl%'
;
b 開啟SSL方法:證書使能,在MySQL的配置文件my.ini中指定服務端證書路徑
ssl-ca=E:/mysql5.6/cert/ca-cert.pem
ssl-cert=E:/mysql5.6/cert/server-cert.pem
ssl-key=E:/mysql5.6/cert/server-key.pem
c 重啟MySQL服務,執行net stop mysql5停止服務,再執行net startmysql5開啟服務
d 再次執行show variables like
'%ssl%'
; 查看SSL已經開啟了
至此MySQL安裝及配置證書過程結束。
測試:
1、上面步驟中,在數據庫中創建了以sha256_password加密的test01用戶,密碼為password。此時我們用一般的方式連接肯定會報錯
2、使用開啟SSL、指定證書的方式連接就是成功的,且通過\s 可以看出SSL信息,命令:mysql --ssl-ca=E:\mysql5.6\cert\ca-cert.pem --ssl-cert=E:\mysql5.6\cert\client-cert.pem --ssl-key=E:\mysql5.6\cert\client-key.pem -u test01 -ppasswor
3、當前系統中的root用戶還是mysql_native_password的加密方式,如果要想將root的加密方式修改的話執行:
use mysql;
update user set plugin='sha256_password' where user='root';
結果如下:
現在以root用戶修改root用戶的密碼,執行:SET PASSWORD FOR 'root'@'localhost' = PASSWORD('1qaz@WSX');此時執行成功,再執行 FLUSH PRIVILEGES; password列已經被修改如下圖,退出客戶端重新連接為啥連不上了?
使用ssl方式連接失敗了,但是使用空密碼(直接回車)登錄是成功的
這是為什么呢,通過分析之前使用sha256_password創建的test01用戶發現:test01用戶的passwor字段為空,authentication_string字段是有值的;而此時的root的password是有密文的,但authentication_string字段沒有值。
所以我們能得出2點:
sha256_password加密的用戶,密碼其實是設置在authentication_string字段上的。
root登錄時,修改密碼插件后,執行SET PASSWORD FOR 'root'@'localhost'設置密碼時,當前CMD的session沒有實效,還是之前的加密插件在生效,修改的當然是password字段的值,而authentication_string字段的值依舊是空串。
最終使用空密碼登錄后再執行一次設置密碼的命令,SET PASSWORD FOR 'root'@'localhost' = PASSWORD('1qaz@WSX'); FLUSH PRIVILEGES;再退出,使用root及新密碼登錄就是成功的了。
附上我的my.ini

# For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html # *** DO NOT EDIT THIS FILE. It's a template which will be copied to the # *** default location during install, and will be replaced if you # *** upgrade to a newer version of MySQL. [mysqld] # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin # These are commonly set, remove the # and set as required. basedir = E:/mysql5.6 datadir = E:/mysql5.6/data port =3306 # server_id = ..... default-authentication-plugin=sha256_password ssl-ca=E:/mysql5.6/cert/ca-cert.pem ssl-cert=E:/mysql5.6/cert/server-cert.pem ssl-key=E:/mysql5.6/cert/server-key.pem # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2M sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES