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

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

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,如需转载请自行联系原作者


相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
30天前
|
分布式计算 关系型数据库 数据处理
Dataphin常见问题之没有建表的权限如何解决
Dataphin是阿里云提供的一站式数据处理服务,旨在帮助企业构建一体化的智能数据处理平台。Dataphin整合了数据建模、数据处理、数据开发、数据服务等多个功能,支持企业更高效地进行数据治理和分析。
|
26天前
|
SQL 关系型数据库 MySQL
【MySQL技术专题】「问题实战系列」深入探索和分析MySQL数据库的数据备份和恢复实战开发指南(8.0版本升级篇)
【MySQL技术专题】「问题实战系列」深入探索和分析MySQL数据库的数据备份和恢复实战开发指南(8.0版本升级篇)
95 0
|
1月前
|
SQL 关系型数据库 MySQL
阿里云MySQL数据库价格、购买、创建账号密码和连接数据库教程
阿里云数据库使用指南:购买MySQL、SQL Server等RDS实例,选择配置和地区,完成支付。创建数据库和账号,设置权限。通过DMS登录数据库,使用账号密码访问。同地域VPC内的ECS需将IP加入白名单以实现内网连接。参考链接提供详细步骤。
369 3
|
1天前
|
关系型数据库 MySQL 数据安全/隐私保护
使用Navicate连接Mysql过程详解
使用Navicate连接Mysql过程详解
5 0
|
2天前
|
关系型数据库 MySQL 中间件
【MySQL实战笔记】07 | 行锁功过:怎么减少行锁对性能的影响?-02 死锁和死锁检测
【4月更文挑战第19天】在高并发环境下,死锁发生在多个线程间循环等待资源时,导致无限期等待。MySQL中,死锁可通过`innodb_lock_wait_timeout`参数设置超时或`innodb_deadlock_detect`开启死锁检测来解决。默认的50s超时可能不适用于在线服务,而频繁检测会消耗大量CPU。应对热点行更新引发的性能问题,可以暂时关闭死锁检测(风险是产生大量超时),控制并发度,或通过分散记录减少锁冲突,例如将数据分拆到多行以降低死锁概率。
18 1
|
5天前
|
SQL 关系型数据库 MySQL
Python与MySQL数据库交互:面试实战
【4月更文挑战第16天】本文介绍了Python与MySQL交互的面试重点,包括使用`mysql-connector-python`或`pymysql`连接数据库、执行SQL查询、异常处理、防止SQL注入、事务管理和ORM框架。易错点包括忘记关闭连接、忽视异常处理、硬编码SQL、忽略事务及过度依赖低效查询。通过理解这些问题和提供策略,可提升面试表现。
25 6
|
9天前
|
SQL 关系型数据库 MySQL
DQL语言之连接查询(mysql)
DQL语言之连接查询(mysql)
|
12天前
|
存储 关系型数据库 MySQL
【MySQL实战笔记】 04 | 深入浅出索引(上)-02
【4月更文挑战第9天】InnoDB数据库使用B+树作为索引模型,其中主键索引的叶子节点存储完整行数据,非主键索引则存储主键值。主键查询只需搜索一棵树,而非主键查询需两次搜索,因此推荐使用主键查询以提高效率。在插入新值时,B+树需要维护有序性,可能导致数据页分裂影响性能。自增主键在插入时可避免数据挪动和页分裂,且占用存储空间小,通常更为理想。然而,如果场景仅需唯一索引,可直接设为主键以减少查询步骤。
13 1
【MySQL实战笔记】 04 | 深入浅出索引(上)-02
|
12天前
|
关系型数据库 MySQL 数据安全/隐私保护
MySQL 安装及连接
MySQL 安装及连接
33 0
|
14天前
|
存储 SQL 关系型数据库
【MySQL实战笔记】03.事务隔离:为什么你改了我还看不见?-02
【4月更文挑战第7天】数据库通过视图实现事务隔离,不同隔离级别如读未提交、读已提交、可重复读和串行化采用不同策略。以可重复读为例,MySQL使用多版本并发控制(MVCC),每个事务有其独立的视图。回滚日志在无更早视图时被删除。长事务可能导致大量存储占用,应避免。事务启动可显式用`begin`或设置`autocommit=0`,但后者可能意外开启长事务。建议使用`autocommit=1`并显式管理事务,若需减少交互,可使用`commit work and chain`。
29 5