MySQL5.6启用sha256_password插件

1、背景:html

  使用MySQL5.6过程当中,发现默认的加密插件为mysql_native_password。而sha256_password的安全程度要比mysql_native_password高,尝试切换为sha256_password。node

2、配置过程:mysql

  资料:sql

  一、从MySQL官网查询到服务器端sha256_password无需显式加载,能够在MySQL配置文件中配置使能。数据库

     官网见:https://dev.mysql.com/doc/refman/5.6/en/sha256-pluggable-authentication.htmlsegmentfault

     [mysqld] 安全

       default-authentication-plugin=sha256_passwordbash

  二、据官网描述,要启用插件,须经过ssl方式进行链接,也就是说须要配置相关证书。服务器

    实现过程:session

  一、下载MySQL安装包,https://dev.mysql.com/downloads/mysql/5.6.html#downloads

  

  

  二、安装MySQL

  下载的的MySQL是zip格式的,解压到磁盘后,将my-default.ini另存为my.ini(此处看我的爱好,可不用),关于my.ini须要修改的地方以下:

  1)basedir datadir port 须要根据本身使用状况配置。

  2)配置默认启用的加密插件。

  

  三、简单配置完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的虚拟机,制做证书:(参考:http://www.javashuo.com/article/p-meanlkoi-ma.html

    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安装及配置证书过程结束。

  测试:

  一、上面步骤中,在数据库中建立了以sha256_password加密的test01用户,密码为password。此时咱们用通常的方式链接确定会报错

  

 

  二、使用开启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

  

  三、当前系统中的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
View Code
相关文章
相关标签/搜索