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

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,高可用系列 2核4GB
简介:

1. 背景

   * 在生产环境下,安全总是无法忽视的问题,数据库安全则是重中之重,因为所有的数据都存放在数据库中

   * 当使用非加密方式连接MySQL数据库时,在网络中传输的所有信息都是明文的,可以被网络中所有人截取,敏感信息可能被泄露。在传送敏感信息(如密码)时,可以采用SSL连接的方式。


2. MySQL 连接方式

   * socket连接

   * TCP非SSL连接

   * 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.6安装前面篇章已做详细介绍 ]

        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
[root@MySQL mysql] # mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection  id  is 3
Server version: 5.6.36 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.6.36    |
+-----------+
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.00 sec)
 
mysql> show variables like  'port' ;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |
+---------------+-------+
1 row  in  set  (0.00 sec)
 
mysql> show variables like  'datadir' ;
+---------------+-------------------+
| Variable_name | Value             |
+---------------+-------------------+
| datadir       |  /data/mysql_data/  |
+---------------+-------------------+
1 row  in  set  (0.00 sec)


5. 通过openssl 制作生成 SSL 证书

   * 生成一个 CA 私钥

1
2
3
4
5
[root@MySQL ~] # openssl genrsa 2048 > ca-key.pem
Generating RSA private key, 2048 bit long modulus
.............................+++
....................+++
e is 65537 (0x10001)


   * 通过 CA 私钥生成数字证书

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@MySQL ~] # openssl req -new -x509 -nodes -days 3600 \
>          -key ca-key.pem -out ca.pem
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter  '.' , the field will be left blank.
-----
Country Name (2 letter code) [XX]:
State or Province Name (full name) []:
Locality Name (eg, city) [Default City]:
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's  hostname ) []:
Email Address []:


   * 创建 MySQL 服务器 私钥和请求证书

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
[root@MySQL ~] # openssl req -newkey rsa:2048 -days 3600 \
>          -nodes -keyout server-key.pem -out server-req.pem
Generating a 2048 bit RSA private key
.................................+++
.......................................................+++
writing new private key to  'server-key.pem'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter  '.' , the field will be left blank.
-----
Country Name (2 letter code) [XX]:
State or Province Name (full name) []:
Locality Name (eg, city) [Default City]:
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's  hostname ) []:
Email Address []:
 
Please enter the following  'extra'  attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:


   * 将生成的私钥转换为 RSA 私钥文件格式

1
2
[root@MySQL ~] # openssl rsa -in server-key.pem -out server-key.pem
writing RSA key


   * 用CA 证书来生成一个服务器端的数字证书

1
2
3
4
5
[root@MySQL ~] # openssl x509 -req -in server-req.pem -days 3600 \
>          -CA ca.pem -CAkey ca-key.pem -set_serial 01 -out server-cert.pem
Signature ok
subject= /C =XX /L =Default City /O =Default Company Ltd
Getting CA Private Key


   * 创建客户端的 RSA 私钥和数字证书

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
[root@MySQL ~] # openssl req -newkey rsa:2048 -days 3600 \
>          -nodes -keyout client-key.pem -out client-req.pem
Generating a 2048 bit RSA private key
..........................................................+++
.................+++
writing new private key to  'client-key.pem'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter  '.' , the field will be left blank.
-----
Country Name (2 letter code) [XX]:
State or Province Name (full name) []:
Locality Name (eg, city) [Default City]:
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's  hostname ) []:
Email Address []:
 
Please enter the following  'extra'  attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:


   * 将生成的私钥转换为 RSA 私钥文件格式

1
2
[root@MySQL ~] # openssl rsa -in client-key.pem -out client-key.pem
writing RSA key


   * 用CA 证书来生成一个客户端的数字证书

1
2
3
4
5
[root@MySQL ~] # openssl x509 -req -in client-req.pem -days 3600 \
>          -CA ca.pem -CAkey ca-key.pem -set_serial 01 -out client-cert.pem
Signature ok
subject= /C =XX /L =Default City /O =Default Company Ltd
Getting CA Private Key


   * 查看所有生成的SSL文件

1
2
3
4
5
6
7
8
9
[root@MySQL ~] # ls -l *.pem
-rw-r--r-- 1 root root 1675 Jun 24 14:16 ca-key.pem
-rw-r--r-- 1 root root 1220 Jun 24 14:19 ca.pem
-rw-r--r-- 1 root root 1090 Jun 24 14:29 client-cert.pem
-rw-r--r-- 1 root root 1679 Jun 24 14:28 client-key.pem
-rw-r--r-- 1 root root  952 Jun 24 14:28 client-req.pem
-rw-r--r-- 1 root root 1090 Jun 24 14:24 server-cert.pem
-rw-r--r-- 1 root root 1679 Jun 24 14:23 server-key.pem
-rw-r--r-- 1 root root  952 Jun 24 14:20 server-req.pem


6. MySQL 配置启动 SSL

   * 复制 CA 证书和服务端SSL文件至MySQL 数据目录

1
2
3
4
5
[root@MySQL ~] # cp ca.pem server-*.pem /data/mysql_data -v
`ca.pem ' -> `/data/mysql_data/ca.pem'
`server-cert.pem ' -> `/data/mysql_data/server-cert.pem'
`server-key.pem ' -> `/data/mysql_data/server-key.pem'
`server-req.pem ' -> `/data/mysql_data/server-req.pem'


   * 修改 MySQL 数据目录的CA 证书和服务端 SSL 文件所属用户与组

1
2
3
4
5
[root@MySQL ~] # chown -v mysql.mysql /data/mysql_data/{ca,server*}.pem
changed ownership of ` /data/mysql_data/ca .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
changed ownership of ` /data/mysql_data/server-req .pem' to mysql:mysql


   * 配置 MySQL 服务的配置文件 [/etc/my.cnf]

1
2
3
4
[mysqld]
ssl-ca= /data/mysql_data/ca .pem
ssl-cert= /data/mysql_data/server-cert .pem
ssl-key= /data/mysql_data/server-key .pem


   * 重启MySQL服务

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


   * 登陆查看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.01 sec)


6. SSL连接测试

   * 创建用户并指定 SSL 连接

1
2
mysql> grant all on *.* to  'ssl_test' @ '%'  identified by  '123'  require SSL;
Query OK, 0 rows affected (0.00 sec)


   * 通过密码连接测试

1
2
3
[root@MySQL ~] # mysql -h 192.168.60.129 -ussl_test -p'123'
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 + 密码连接测试

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
[root@MySQL ~] # mysql -h 192.168.60.129 -ussl_test  --ssl-cert=client-cert.pem --ssl-key=client-key.pem 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection  id  is 9
Server version: 5.6.36 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.6.36,  for  linux-glibc2.5 (x86_64) using  EditLine wrapper
 
Connection  id :     20
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.6.36 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:         16 min 38 sec
 
Threads: 1  Questions: 35  Slow queries: 0  Opens: 67  Flush tables: 1  Open tables: 60  Queries per second avg: 0.035



7. 总结


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




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




相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
15天前
|
网络安全 数据库
YashanDB HA节点间SSL连接配置
本指南介绍HA内部节点链路的SSL连接配置,包括客户端监听与HA节点自身监听两种方式。需使用OpenSSL工具生成证书,具体步骤参考数据库服务端SSL连接配置文档。此外,还需在数据库中开启HA的SSL连接开关并设置证书路径(仅支持绝对路径,长度≤254字节),最后重启数据库以完成配置。确保服务器已安装所需工具,详细操作请查阅相关文档。
YashanDB HA节点间SSL连接配置
|
1月前
|
关系型数据库 MySQL Java
【YashanDB知识库】原生mysql驱动配置连接崖山数据库
【YashanDB知识库】原生mysql驱动配置连接崖山数据库
【YashanDB知识库】原生mysql驱动配置连接崖山数据库
|
15天前
|
安全 网络安全 数据库
YashanDB分布式节点间SSL连接配置
本文介绍YashanDB分布式节点间SSL连接配置方法,确保通信安全。需统一为整个集群配置SSL,使用相同根证书签名的服务器证书,否则可能导致连接失败或数据库无法启动。文章详细说明了使用OpenSSL生成根证书、服务器私钥、证书及DH文件的步骤,并指导如何将证书分发至各节点。最后,通过配置数据库参数(如`din_ssl_enable`)并重启集群完成设置。注意,证书过期需重新生成以保障安全性。
|
15天前
|
安全 Linux 网络安全
YashanDB数据库服务端SSL连接配置
YashanDB支持通过SSL连接确保数据传输安全,需在服务端生成根证书、服务器证书及DH文件,并将根证书提供给客户端以完成身份验证。服务端配置包括使用OpenSSL工具生成证书、设置SSL参数并重启数据库;客户端则需下载根证书并正确配置环境变量与`yasc_env.ini`文件。注意:启用SSL后,所有客户端必须持有根证书才能连接,且SSL与密码认证独立运行。
|
2天前
|
存储 Oracle 关系型数据库
MySQL 8.4 配置SSL组复制(八个步骤)
MySQL 8.4 配置SSL组复制(八个步骤)
22 0
|
1月前
|
运维 安全 网络安全
【运维实战分享】轻松搞定 SSL 证书管理,告别证书繁琐操作
Spug证书平台的最大亮点之一就是其极为简化的证书申请流程,无论是新手还是经验丰富的运维专家,都可以在几分钟内轻松完成证书的申请,通过微信扫码直接登录申请,无需复杂注册,整个过程既方便又快捷。
100 17
|
3月前
|
关系型数据库 MySQL 数据库连接
数据库连接工具连接mysql提示:“Host ‘172.23.0.1‘ is not allowed to connect to this MySQL server“
docker-compose部署mysql8服务后,连接时提示不允许连接问题解决
|
2月前
|
关系型数据库 MySQL 网络安全
如何排查和解决PHP连接数据库MYSQL失败写锁的问题
通过本文的介绍,您可以系统地了解如何排查和解决PHP连接MySQL数据库失败及写锁问题。通过检查配置、确保服务启动、调整防火墙设置和用户权限,以及识别和解决长时间运行的事务和死锁问题,可以有效地保障应用的稳定运行。
184 25
|
2月前
|
关系型数据库 MySQL 数据库连接
Unity连接Mysql数据库 增 删 改 查
在 Unity 中连接 MySQL 数据库,需使用 MySQL Connector/NET 作为数据库连接驱动,通过提供服务器地址、端口、用户名和密码等信息建立 TCP/IP 连接。代码示例展示了如何创建连接对象并执行增删改查操作,确保数据交互的实现。测试代码中,通过 `MySqlConnection` 类连接数据库,并使用 `MySqlCommand` 执行 SQL 语句,实现数据的查询、插入、删除和更新功能。
|
4月前
|
SQL 关系型数据库 MySQL
【MySQL基础篇】多表查询(隐式/显式内连接、左/右外连接、自连接查询、联合查询、标量/列/行/表子查询)
本文详细介绍了MySQL中的多表查询,包括多表关系、隐式/显式内连接、左/右外连接、自连接查询、联合查询、标量/列/行/表子查询及其实现方式,一文全面读懂多表联查!
【MySQL基础篇】多表查询(隐式/显式内连接、左/右外连接、自连接查询、联合查询、标量/列/行/表子查询)
下一篇
oss创建bucket