设为首页 收藏本站
查看: 681|回复: 0

[经验分享] MySQL 5.7--------SSL连接最佳实战

[复制链接]

尚未签到

发表于 2018-9-28 10:31:39 | 显示全部楼层 |阅读模式
  1. 背景
  * 在生产环境下,安全总是无法忽视的问题,数据库安全则是重中之重,因为所有的数据都存放在数据库中
  * 当使用非加密方式连接MySQL数据库时,在网络中传输的所有信息都是明文的,可以被网络中所有人截取,敏感信息可能被泄露。在传送敏感信息(如密码)时,可以采用SSL连接的方式。
  *  版本小于5.7.6时按照 MySQL 5.6 SSL配置的方式进行。
  2. MySQL 连接方式
  * socket连接
  * TCP非SSL连接
  * SSL安全连接
  * SSL + 密码连接 [version > MySQL 5.7.5]
  * SSL + 密码 + 密钥连接
  3. SSL 简介
  * SSL指的是SSL/TLS,其是一种为了在计算机网络进行安全通信的加密协议。假设用户的传输不是通过SSL的方式,那么其在网络中以明文的方式进行传输,而这给别有用心的人带来了可乘之机。所以,现在很多网站其实默认已经开启了SSL功能,比如Facebook、Twtter、YouTube、淘宝等。
DSC0000.jpg

  4. 环境 [ 关闭SeLinux ]
  * system 环境
[root@MySQL ~]# cat /etc/redhat-release  
CentOS release 6.9 (Final)
  
[root@MySQL ~]# uname -r
  

  
2.6.32-696.3.2.el6.x86_64
  

  
[root@MySQL ~]# getenforce
  
Disabled
  * MySQL 环境 [ MySQL 5.7安装前面篇章已做详细介绍 ]
  have_openssl 与 have_ssl 值都为DISABLED表示ssl未开启
[root@MySQL ~]# mysql -p'123'  
mysql: [Warning] Using a password on the command line interface can be insecure.
  

  
Welcome to the MySQL monitor.  Commands end with ; or \g.
  
Your MySQL connection id is 6
  
Server version: 5.7.18 MySQL Community Server (GPL)
  

  
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
  

  
Oracle is a registered trademark of Oracle Corporation and/or its
  
affiliates. Other names may be trademarks of their respective
  
owners.
  

  
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  

  
mysql> select version();
  
+-----------+
  
| version() |
  
+-----------+
  
| 5.7.18    |
  
+-----------+
  
1 row in set (0.00 sec)
  

  
mysql> show variables like 'have%ssl%';
  
+---------------+----------+
  
| Variable_name | Value    |
  
+---------------+----------+
  
| have_openssl  | DISABLED |
  
| have_ssl      | DISABLED |
  
+---------------+----------+
  
2 rows in set (0.02 sec)
  

  
mysql> show variables like 'port';
  
+---------------+-------+
  
| Variable_name | Value |
  
+---------------+-------+
  
| port          | 3306  |
  
+---------------+-------+
  
1 row in set (0.01 sec)
  

  
mysql> show variables like 'datadir';
  
+---------------+-------------------+
  
| Variable_name | Value             |
  
+---------------+-------------------+
  
| datadir       | /data/mysql_data/ |
  
+---------------+-------------------+
  
1 row in set (0.01 sec)
  5. SSL配置
  *  利用自带工具生成SSL相关文件
[root@MySQL ~]# /usr/local/mysql/bin/mysql_ssl_rsa_setup --datadir=/data/mysql_data  
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'
  
-----
  * 查看生成的SSL文件
[root@MySQL ~]# ls -l /data/mysql_data/*.pem  
-rw------- 1 root root 1679 Jun 24 20:54 /data/mysql_data/ca-key.pem
  
-rw-r--r-- 1 root root 1074 Jun 24 20:54 /data/mysql_data/ca.pem
  
-rw-r--r-- 1 root root 1078 Jun 24 20:54 /data/mysql_data/client-cert.pem
  
-rw------- 1 root root 1675 Jun 24 20:54 /data/mysql_data/client-key.pem
  
-rw------- 1 root root 1675 Jun 24 20:54 /data/mysql_data/private_key.pem
  
-rw-r--r-- 1 root root  451 Jun 24 20:54 /data/mysql_data/public_key.pem
  
-rw-r--r-- 1 root root 1078 Jun 24 20:54 /data/mysql_data/server-cert.pem
  
-rw------- 1 root root 1675 Jun 24 20:54 /data/mysql_data/server-key.pem
  * 修改数据目录下生成的SSL文件所属用户与权限
[root@MySQL ~]# chown -v mysql.mysql /data/mysql_data/*.pem  
changed ownership of `/data/mysql_data/ca-key.pem' to mysql:mysql
  
changed ownership of `/data/mysql_data/ca.pem' to mysql:mysql
  
changed ownership of `/data/mysql_data/client-cert.pem' to mysql:mysql
  
changed ownership of `/data/mysql_data/client-key.pem' to mysql:mysql
  
changed ownership of `/data/mysql_data/private_key.pem' to mysql:mysql
  
changed ownership of `/data/mysql_data/public_key.pem' to mysql:mysql
  
changed ownership of `/data/mysql_data/server-cert.pem' to mysql:mysql
  
changed ownership of `/data/mysql_data/server-key.pem' to mysql:mysql
  * 查看生成的SSL文件
[root@MySQL ~]# ls -l /data/mysql_data/*.pem  
-rw------- 1 mysql mysql 1679 Jun 24 20:54 /data/mysql_data/ca-key.pem
  
-rw-r--r-- 1 mysql mysql 1074 Jun 24 20:54 /data/mysql_data/ca.pem
  
-rw-r--r-- 1 mysql mysql 1078 Jun 24 20:54 /data/mysql_data/client-cert.pem
  
-rw------- 1 mysql mysql 1675 Jun 24 20:54 /data/mysql_data/client-key.pem
  
-rw------- 1 mysql mysql 1675 Jun 24 20:54 /data/mysql_data/private_key.pem
  
-rw-r--r-- 1 mysql mysql  451 Jun 24 20:54 /data/mysql_data/public_key.pem
  
-rw-r--r-- 1 mysql mysql 1078 Jun 24 20:54 /data/mysql_data/server-cert.pem
  
-rw------- 1 mysql mysql 1675 Jun 24 20:54 /data/mysql_data/server-key.pem
  * 重启 MySQL 服务
[root@MySQL ~]# /etc/init.d/mysqld restart  
Shutting down MySQL.. SUCCESS!
  
Starting MySQL. SUCCESS!
  * 连接MySQL 查看SSL开启状态
  have_openssl 与 have_ssl 值都为YES表示ssl开启成功
mysql> show variables like 'have%ssl%';  
+---------------+-------+
  
| Variable_name | Value |
  
+---------------+-------+
  
| have_openssl  | YES   |
  
| have_ssl      | YES   |
  
+---------------+-------+
  
2 rows in set (0.03 sec)
  6. SSL + 密码连接测试
  * 创建用户并指定 SSL 连接 [ MySQL 5.7后推荐使用create user 方式创建用户 ]
mysql> create user 'ssl_test'@'%' identified by '123' require SSL;  
Query OK, 0 rows affected (0.00 sec)
  * 通过密码连接测试 [ 默认采用SSL连接,需要指定不使用SSL连接 ]
[root@MySQL ~]# mysql -h 192.168.60.129 -ussl_test -p'123' --ssl=0  
mysql: [Warning] Using a password on the command line interface can be insecure.
  
ERROR 1045 (28000): Access denied for user 'ssl_test'@'192.168.60.129' (using password: YES)
  * 通过 SSL + 密码 连接测试
  SSL: Cipher in use is DHE-RSA-AES256-SHA 表示通过SSL连接
[root@MySQL ~]# mysql -h 192.168.60.129 -ussl_test -p'123'  --ssl  
mysql: [Warning] Using a password on the command line interface can be insecure.
  
WARNING: --ssl is deprecated and will be removed in a future version. Use --ssl-mode instead.
  
Welcome to the MySQL monitor.  Commands end with ; or \g.
  
Your MySQL connection id is 12
  
Server version: 5.7.18 MySQL Community Server (GPL)
  

  
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
  

  
Oracle is a registered trademark of Oracle Corporation and/or its
  
affiliates. Other names may be trademarks of their respective
  
owners.
  

  
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  

  
mysql> \s
  
--------------
  
mysql  Ver 14.14 Distrib 5.7.18, for linux-glibc2.5 (x86_64) using  EditLine wrapper
  

  
Connection id:12
  
Current database:
  
Current user:ssl_test@192.168.60.129
  
SSL:Cipher in use is DHE-RSA-AES256-SHA
  
Current pager:stdout
  
Using outfile:''
  
Using delimiter:;
  
Server version:5.7.18 MySQL Community Server (GPL)
  
Protocol version:10
  
Connection:192.168.60.129 via TCP/IP
  
Server characterset:latin1
  
Db     characterset:latin1
  
Client characterset:utf8
  
Conn.  characterset:utf8
  
TCP port:3306
  
Uptime:7 min 34 sec
  

  
Threads: 1  Questions: 29  Slow queries: 0  Opens: 112  Flush tables: 1  Open tables: 105  Queries per second avg: 0.063
  
--------------
  7. SSL + 密码 + 密钥连接
  * 创建用户并指定 X509 [ SSL+密钥 ] 连接 [ MySQL 5.7后推荐使用create user 方式创建用户 ]
mysql> create user 'X509_test'@'%' identified by '123' require X509;  
Query OK, 0 rows affected (0.00 sec)
  * 通过密码连接测试
[root@MySQL ~]# mysql -h 192.168.60.129 -uX509_test -p'123' --ssl=0  
mysql: [Warning] Using a password on the command line interface can be insecure.
  
ERROR 1045 (28000): Access denied for user 'X509_test'@'192.168.60.129' (using password: YES)
  * 通过 SSL +密码 连接测试
[root@MySQL ~]# mysql -h 192.168.60.129 -uX509_test -p'123' --ssl  
mysql: [Warning] Using a password on the command line interface can be insecure.
  
ERROR 1045 (28000): Access denied for user 'X509_test'@'192.168.60.129' (using password: YES)
  * 通过 SSL + 密码+密钥连接测试
  SSL: Cipher in use is DHE-RSA-AES256-SHA 表示通过SSL连接
[root@MySQL ~]# mysql -h 192.168.60.129 -uX509_test -p'123' --ssl-cert=/data/mysql_data/client-cert.pem --ssl-key=/data/mysql_data/client-key.pem  
mysql: [Warning] Using a password on the command line interface can be insecure.
  
Welcome to the MySQL monitor.  Commands end with ; or \g.
  
Your MySQL connection id is 21
  
Server version: 5.7.18 MySQL Community Server (GPL)
  

  
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
  

  
Oracle is a registered trademark of Oracle Corporation and/or its
  
affiliates. Other names may be trademarks of their respective
  
owners.
  

  
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  

  
mysql> \s
  
--------------
  
mysql  Ver 14.14 Distrib 5.7.18, for linux-glibc2.5 (x86_64) using  EditLine wrapper
  

  
Connection id:21
  
Current database:
  
Current user:X509_test@192.168.60.129
  
SSL:Cipher in use is DHE-RSA-AES256-SHA
  
Current pager:stdout
  
Using outfile:''
  
Using delimiter:;
  
Server version:5.7.18 MySQL Community Server (GPL)
  
Protocol version:10
  
Connection:192.168.60.129 via TCP/IP
  
Server characterset:latin1
  
Db     characterset:latin1
  
Client characterset:utf8
  
Conn.  characterset:utf8
  
TCP port:3306
  
Uptime:18 min 27 sec
  

  
Threads: 1  Questions: 40  Slow queries: 0  Opens: 118  Flush tables: 1  Open tables: 111  Queries per second avg: 0.036
  
--------------
  8. 总结
  以需求驱动技术,技术本身没有优略之分,只有业务之分。



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-603157-1-1.html 上篇帖子: MySQL 常用SQL优化 下篇帖子: Linux下安装mysql数据库
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表