mysql 密码sha256_MySQL5.6启用sha256_password插件
一、背景:
使用MySQL5.6過程中,發(fā)現(xiàn)默認的加密插件為mysql_native_password。而sha256_password的安全程度要比mysql_native_password高,嘗試切換為sha256_password。
二、配置過程:
資料:
1、從MySQL官網(wǎng)查詢到服務器端sha256_password無需顯式加載,可以在MySQL配置文件中配置使能。
[mysqld]
default-authentication-plugin=sha256_password
2、據(jù)官網(wǎng)描述,要啟用插件,須通過ssl方式進行連接,也就是說需要配置相關證書。
實現(xiàn)過程:
】
2、安裝MySQL
下載的的MySQL是zip格式的,解壓到磁盤后,將my-default.ini另存為my.ini(此處看個人愛好,可不用),關于my.ini需要修改的地方如下:
1)basedir?datadir?port?需要根據(jù)自己使用情況配置。
2)配置默認啟用的加密插件。
3、簡單配置完mysql配置文件后,以管理員方式打開cmd,進入第二步解壓后的xxxmyql/bin目錄。
1)執(zhí)行mysqld -install(tips:mysqld -remove 是卸載mysql),也可以執(zhí)行服務名稱及配置文件路徑:mysqld install mysql5 --defaults-file="E:\mysql5.6\my.ini"
2)執(zhí)行net start mysql5啟動MySQL服務(我的服務名稱為mysql5,上一步install時指定了服務名稱,如果install時沒指定默認就是MySQL)
3)輸入mysql -uroot -p連接數(shù)據(jù)庫。第一次進入沒有密碼,直接回車。
4)進入mysql數(shù)據(jù)庫,查詢user表的內(nèi)容,發(fā)現(xiàn)默認使用的加密插件為mysql_native_password如下圖。
是不是my.ini配置的sha256_password沒有生效吶?創(chuàng)建一個用戶驗證下插件是否生效。執(zhí)行:CREATE USER 'test01'@'localhost' IDENTIFIED BY 'password';發(fā)現(xiàn)默認插件是生效了的。但是默認的root為啥不是sha256_password呢,我猜想(只是猜想)
可能是假如root用戶默認為sha256_password,那么使用root連接的話,就需要配置相關證書,這樣使MySQL的安裝過程復雜且使用體驗降低。
使用新創(chuàng)建的用戶test01登錄數(shù)據(jù)庫,因為test01用戶使用了sha256_password,此時是登錄失敗的,提示身份驗證需要SSL加密。所以要使用了sha256_password插件的用戶是需要通過SSL加密的,也就是需要證書的。
5)補充一點:安裝完成后root用戶是沒有密碼的,要設置密碼可以執(zhí)行mysqladmin -u root -p password,Enter password:直接回車,因為此時root時沒有密碼的,接著輸入及確認輸入自己的密碼。至此MySQL的安裝已經(jīng)完成。
3.證書制作及使能
a?生成一個 CA 私鑰:openssl genrsa 2048 > ca-key.pem
b?私鑰生成一個新的數(shù)字證書:openssl req -sha1 -new -x509 -nodes -days 3650 -key ca-key.pem > ca-cert.pem,執(zhí)行過程中需要填寫一些內(nèi)容,如國家、城市、郵箱等根據(jù)實際情況填寫。
c?創(chuàng)建服務側(cè)的私鑰及數(shù)字證書:openssl req -sha1 -newkey rsa:2048 -days 3650 -nodes -keyout server-key.pem > server-req.pem
此時會填寫一些內(nèi)容如b步驟,其中有個密碼可以直接回車。
d?將生成的私鑰轉(zhuǎn)換為 RSA 私鑰文件格式:openssl rsa -in server-key.pem -out server-key.pem
e? 使用CA 證書來生成一個服務器端的數(shù)字證書:openssl x509 -sha1 -req -in server-req.pem -days 3650 -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 > server-cert.pem
至此,服務端相關證書已創(chuàng)建成果,下面創(chuàng)建客戶端證書。
f?為客戶端生成一個私鑰和證書:openssl req -sha1 -newkey rsa:2048 -days 3650 -nodes -keyout client-key.pem > client-req.pem? 需要填寫問題見步驟b
g?將生成的私鑰轉(zhuǎn)換為 RSA 私鑰文件格式:openssl rsa -in client-key.pem -out client-key.pem
h?為客戶端創(chuàng)建一個數(shù)字證書:openssl x509 -sha1 -req -in client-req.pem -days 3650 -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 > client-cert.pem
至此,客戶端和服務端證書全部創(chuàng)建完畢,生產(chǎn)八個文件如下:
2)配置證書
a?現(xiàn)在數(shù)據(jù)庫中是沒有開啟SSL的,執(zhí)行命令查看: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服務,執(zhí)行net stop mysql5停止服務,再執(zhí)行net?startmysql5開啟服務
d?再次執(zhí)行show variables like'%ssl%';?查看SSL已經(jīng)開啟了
至此MySQL安裝及配置證書過程結(jié)束。
測試:
1、上面步驟中,在數(shù)據(jù)庫中創(chuàng)建了以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、當前系統(tǒng)中的root用戶還是mysql_native_password的加密方式,如果要想將root的加密方式修改的話執(zhí)行:
use?mysql;
update user set plugin='sha256_password' where user='root';
結(jié)果如下:
現(xiàn)在以root用戶修改root用戶的密碼,執(zhí)行:SET PASSWORD FOR 'root'@'localhost' = PASSWORD('1qaz@WSX');此時執(zhí)行成功,再執(zhí)行 FLUSH PRIVILEGES;? ?password列已經(jīng)被修改如下圖,退出客戶端重新連接為啥連不上了?
使用ssl方式連接失敗了,但是使用空密碼(直接回車)登錄是成功的
這是為什么呢,通過分析之前使用sha256_password創(chuàng)建的test01用戶發(fā)現(xiàn):test01用戶的passwor字段為空,authentication_string字段是有值的;而此時的root的password是有密文的,但authentication_string字段沒有值。
所以我們能得出2點:
sha256_password加密的用戶,密碼其實是設置在authentication_string字段上的。
root登錄時,修改密碼插件后,執(zhí)行SET PASSWORD FOR 'root'@'localhost'設置密碼時,當前CMD的session沒有實效,還是之前的加密插件在生效,修改的當然是password字段的值,而authentication_string字段的值依舊是空串。
最終使用空密碼登錄后再執(zhí)行一次設置密碼的命令,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 ifyou
#***upgrade to a newer version of MySQL.
[mysqld]
# Remove leading # andset to the amount of RAM forthe most important data
# cachein 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 commonlyset, remove the # and set asrequired.
basedir= E:/mysql5.6datadir= 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 # toset options mainly useful forreporting servers.
# The server defaults are fasterfortransactions and fast SELECTs.
# Adjust sizesasneeded, 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
View Code
總結(jié)
以上是生活随笔為你收集整理的mysql 密码sha256_MySQL5.6启用sha256_password插件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 爱来的刚好剧情介绍
- 下一篇: accsess转成mysql语句_acc