MySQL 5.7--------SSL连接最佳实战

本文涉及的产品
RDS AI 助手,专业版
RDS MySQL DuckDB 分析主实例,集群系列 4核8GB
RDS MySQL DuckDB 分析主实例,基础系列 4核8GB
简介:

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、淘宝等。

wKiom1lRImDQaiP7AAByLO28Dk8429.jpg


4. 环境 [ 关闭SeLinux ]

   * system 环境

1
2
3
4
5
6
7
8
[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未开启

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
[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相关文件 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[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文件

1
2
3
4
5
6
7
8
9
[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文件所属用户与权限

1
2
3
4
5
6
7
8
9
[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文件

1
2
3
4
5
6
7
8
9
[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 服务

1
2
3
[root@MySQL ~] # /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS!


   * 连接MySQL 查看SSL开启状态

     have_openssl 与 have_ssl 值都为YES表示ssl开启成功

1
2
3
4
5
6
7
8
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 方式创建用户 ]

1
2
mysql> create user  'ssl_test' @ '%'  identified by  '123'  require SSL;
Query OK, 0 rows affected (0.00 sec)


    * 通过密码连接测试 [ 默认采用SSL连接,需要指定不使用SSL连接 ]

1
2
3
[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连接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
[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 方式创建用户 ]

1
2
mysql> create user  'X509_test' @ '%'  identified by  '123'  require X509;
Query OK, 0 rows affected (0.00 sec)


    * 通过密码连接测试

1
2
3
[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 +密码 连接测试

1
2
3
[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连接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
[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. 总结


以需求驱动技术,技术本身没有优略之分,只有业务之分。


      本文转自asd1123509133 51CTO博客,原文链接:http://blog.51cto.com/lisea/1942229,如需转载请自行联系原作者


相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。   相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情: https://www.aliyun.com/product/rds/mysql 
相关文章
|
10月前
|
负载均衡 算法 关系型数据库
大数据大厂之MySQL数据库课程设计:揭秘MySQL集群架构负载均衡核心算法:从理论到Java代码实战,让你的数据库性能飙升!
本文聚焦 MySQL 集群架构中的负载均衡算法,阐述其重要性。详细介绍轮询、加权轮询、最少连接、加权最少连接、随机、源地址哈希等常用算法,分析各自优缺点及适用场景。并提供 Java 语言代码实现示例,助力直观理解。文章结构清晰,语言通俗易懂,对理解和应用负载均衡算法具有实用价值和参考价值。
大数据大厂之MySQL数据库课程设计:揭秘MySQL集群架构负载均衡核心算法:从理论到Java代码实战,让你的数据库性能飙升!
|
11月前
|
网络安全 数据库
YashanDB HA节点间SSL连接配置
本指南介绍HA内部节点链路的SSL连接配置,包括客户端监听与HA节点自身监听两种方式。需使用OpenSSL工具生成证书,具体步骤参考数据库服务端SSL连接配置文档。此外,还需在数据库中开启HA的SSL连接开关并设置证书路径(仅支持绝对路径,长度≤254字节),最后重启数据库以完成配置。确保服务器已安装所需工具,详细操作请查阅相关文档。
YashanDB HA节点间SSL连接配置
|
6月前
|
SQL Java 关系型数据库
Java连接MySQL数据库环境设置指南
请注意,在实际部署时应该避免将敏感信息(如用户名和密码)硬编码在源码文件里面;应该使用配置文件或者环境变量等更为安全可靠地方式管理这些信息。此外,在处理大量数据时考虑使用PreparedStatement而不是Statement可以提高性能并防止SQL注入攻击;同时也要注意正确处理异常情况,并且确保所有打开过得资源都被正确关闭释放掉以防止内存泄漏等问题发生。
252 13
|
6月前
|
SQL 关系型数据库 MySQL
MySQL数据库连接过多(Too many connections)错误处理策略
综上所述,“Too many connections”错误处理策略涉及从具体参数配置到代码层面再到系统与架构设计全方位考量与改进。每项措施都需根据具体环境进行定制化调整,并且在执行任何变更前建议先行测试评估可能带来影响。
1472 11
|
6月前
|
SQL 关系型数据库 MySQL
排除通过IP访问MySQL时出现的连接错误问题
以上步骤涵盖了大多数遇到远程连接 MySQL 数据库时出现故障情形下所需采取措施,在执行每个步骤后都应该重新尝试建立链接以验证是否已经解决问题,在多数情形下按照以上顺序执行将能够有效地排除并修复大多数基本链接相关故障。
444 3
|
6月前
|
SQL 监控 关系型数据库
查寻MySQL或SQL Server的连接数,并配置超时时间和最大连接量
以上步骤提供了直观、实用且易于理解且执行的指导方针来监管和优化数据库服务器配置。务必记得,在做任何重要变更前备份相关配置文件,并确保理解每个参数对系统性能可能产生影响后再做出调节。
615 11
|
7月前
|
存储 关系型数据库 MySQL
修复.net Framework4.x连接MYSQL时遇到utf8mb3字符集不支持错误方案。
通过上述步骤大多数情况下能够解决由于UTF-encoding相关错误所带来影响,在实施过程当中要注意备份重要信息以防止意外发生造成无法挽回损失,并且逐一排查确认具体原因以采取针对性措施解除障碍。
400 12
|
11月前
|
安全 网络安全 数据库
YashanDB分布式节点间SSL连接配置
本文介绍YashanDB分布式节点间SSL连接配置方法,确保通信安全。需统一为整个集群配置SSL,使用相同根证书签名的服务器证书,否则可能导致连接失败或数据库无法启动。文章详细说明了使用OpenSSL生成根证书、服务器私钥、证书及DH文件的步骤,并指导如何将证书分发至各节点。最后,通过配置数据库参数(如`din_ssl_enable`)并重启集群完成设置。注意,证书过期需重新生成以保障安全性。
|
11月前
|
安全 Linux 网络安全
YashanDB数据库服务端SSL连接配置
YashanDB支持通过SSL连接确保数据传输安全,需在服务端生成根证书、服务器证书及DH文件,并将根证书提供给客户端以完成身份验证。服务端配置包括使用OpenSSL工具生成证书、设置SSL参数并重启数据库;客户端则需下载根证书并正确配置环境变量与`yasc_env.ini`文件。注意:启用SSL后,所有客户端必须持有根证书才能连接,且SSL与密码认证独立运行。
|
11月前
|
存储 Oracle 关系型数据库
MySQL 8.4 配置SSL组复制(八个步骤)
MySQL 8.4 配置SSL组复制(八个步骤)
745 0

推荐镜像

更多