1.SSL含义
SSL(Secure Sockets Layer 安全套接层),及其继任者传输层安全(Transport Layer Security,TLS)是为网络通信提供安全及数据完整性的一种安全协议。TLS与SSL在传输层对网络连接进行加密。
2.mysql5.7SSL配置和使用
注意:这种方法只使用5.7,mysql5.6也支持ssl加密连接,但是配置过程比较复杂,需要用到openssl命令来创建各类共秘钥。
我的测试环境默认没有启用SSL,状态为disabled
mysql> show variables like ‘%ssl%‘;
+---------------+----------+
| Variable_name | Value |
+---------------+----------+
| have_openssl | DISABLED |
| have_ssl | DISABLED |
| ssl_ca | |
| ssl_capath | |
| ssl_cert | |
| ssl_cipher | |
| ssl_crl | |
| ssl_crlpath | |
| ssl_key | |
+---------------+----------+
可以通过执行命令mysql_ssl_rsa_setup创建pem文件:
[[email protected] ~]# cd /usr/local/mysql/bin/
[[email protected] bin]# ./mysql_ssl_rsa_setup
Generating a 2048 bit RSA private key
.................................................................................................+++
................................+++
writing new private key to ‘ca-key.pem‘
-----
Generating a 2048 bit RSA private key
......................................+++
.+++
writing new private key to ‘server-key.pem‘
-----
Generating a 2048 bit RSA private key
........................................................................................................................................+++
......+++
writing new private key to ‘client-key.pem‘
-----
运行完命令mysql_ssl_rsa_setup后会发现数据目录下多出了一些以pem结尾的文件,而这些文件就是开启SSL连接所需要的文件:
[[email protected] data]# ll *.pem
-rw------- 1 root root 1679 Nov 26 05:56 ca-key.pem
-rw-r--r-- 1 root root 1074 Nov 26 05:56 ca.pem
-rw-r--r-- 1 root root 1078 Nov 26 05:56 client-cert.pem
-rw------- 1 root root 1679 Nov 26 05:56 client-key.pem
-rw------- 1 root root 1679 Nov 26 05:56 private_key.pem
-rw-r--r-- 1 root root 451 Nov 26 05:56 public_key.pem
-rw-r--r-- 1 root root 1078 Nov 26 05:56 server-cert.pem
-rw------- 1 root root 1675 Nov 26 05:56 server-key.pem
新创建的文件属于root用户,需要改变所有者和所属组,然后重启服务:
[[email protected] data]# chown -R mysql:mysql data/
[[email protected] ~]# /etc/init.d/mysqld restart
mysql> show variables like ‘%ssl%‘;
+---------------+-----------------+
| Variable_name | Value |
+---------------+-----------------+
| have_openssl | YES |
| have_ssl | YES |
| ssl_ca | ca.pem |
| ssl_capath | |
| ssl_cert | server-cert.pem |
| ssl_cipher | |
| ssl_crl | |
| ssl_crlpath | |
| ssl_key | server-key.pem |
+---------------+-----------------+
指定IP,通过网络登陆测试:
[[email protected] ~]# mysql -uroot -p147258 -h192.168.91.5
mysql>\s
--------------
mysql Ver 14.14 Distrib 5.7.14, for linux-glibc2.5 (x86_64) using EditLine wrapper
Connection id: 10
Current database:
Current user: [email protected]
SSL: Cipher in use is DHE-RSA-AES256-SHA
......
本地客户端登陆,没指定IP,默认是不需要SSL加密:
[[email protected] ~]# mysql -uroot -p147258 -hlocalhost
mysql>\s
--------------
mysql Ver 14.14 Distrib 5.7.14, for linux-glibc2.5 (x86_64) using EditLine wrapper
Connection id: 12
Current database:
Current user: [email protected]
SSL: Not in use
......
mysql5.7用户连接默认是使用ssl加密的,也可以用--ssl=0(mysql5.7也可以用--ssl-mode=dibaled)强制用户不使用ssl加密:
[[email protected] ~]# mysql -ucdhu4 -p147258 -h192.168.91.5 --ssl=0
或者:
[[email protected] ~]# mysql -ucdhu4 -p147258 -h192.168.91.5 --ssl-mode=disabled
mysql>\s
--------------
mysql Ver 14.14 Distrib 5.7.14, for linux-glibc2.5 (x86_64) using EditLine wrapper
Connection id: 18
Current database:
Current user: [email protected]
SSL: Not in use
......
若在创建用户时,希望该用户每次必须通过SSL方式,则需在通过REQUIRE SSL来进行设置:
mysql>alter user [email protected]‘%‘ require ssl;
此时指定ssl=0(或者ssl_mode=disabled)则会报错1045:
[[email protected] ~]# mysql -ucdhu5 -p147258 -h192.168.91.5 --ssl=0
ERROR 1045 (28000): Access denied for user ‘cdhu5‘@‘Darren1‘ (using password: YES)
3.ssl加密连接对性能的影响
开启ssl加密连接是性能必然会下降,性能开销在25%左右, 另外,由于SSL开销较大的环节在建立连接,所以短链接的开销可能会更大,因此推荐使用长连接或者连接池的方式来减小SSL所带来的额外开销,不过好在MySQL的应用习惯大部分也是长连接的方式。