手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

MySQL 5.6使用SSL加密连接实战

[日期:2017-07-01] 来源:Linux社区  作者:lisea [字体: ]

1. 背景
  * 在生产环境下,安全总是无法忽视的问题,数据库安全则是重中之重,因为所有的数据都存放在数据库中
  * 当使用非加密方式连接MySQL数据库时,在网络中传输的所有信息都是明文的,可以被网络中所有人截取,敏感信息可能被泄露。在传送敏感信息(如密码)时,可以采用SSL连接的方式。

2. MySQL 连接方式
  * socket连接
  * TCP非SSL连接
  * SSL安全连接

3. SSL 简介
  * SSL指的是SSL/TLS,其是一种为了在计算机网络进行安全通信的加密协议。假设用户的传输不是通过SSL的方式,那么其在网络中以明文的方式进行传输,而这给别有用心的人带来了可乘之机。所以,现在很多网站其实默认已经开启了SSL功能,比如Facebook、Twtter、YouTube、淘宝等。


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.6安装前面篇章已做详细介绍 ]
        have_openssl 与 have_ssl 值都为DISABLED表示ssl未开启
[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 私钥
[root@MySQL ~]# openssl genrsa 2048 > ca-key.pem
Generating RSA private key, 2048 bit long modulus
.............................+++
....................+++
e is 65537 (0x10001)

 

  * 通过 CA 私钥生成数字证书
[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 服务器 私钥和请求证书
[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 私钥文件格式
[root@MySQL ~]# openssl rsa -in server-key.pem -out server-key.pem
writing RSA key

 

  * 用CA 证书来生成一个服务器端的数字证书
[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 私钥和数字证书
[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 私钥文件格式
[root@MySQL ~]# openssl rsa -in client-key.pem -out client-key.pem
writing RSA key

 

  * 用CA 证书来生成一个客户端的数字证书
[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文件
[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 数据目录
[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 文件所属用户与组
[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]
[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服务
[root@MySQL ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS!

 

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

 

6. SSL连接测试
  * 创建用户并指定 SSL 连接
mysql> grant all on *.* to 'ssl_test'@'%' identified by '123' require SSL;
Query OK, 0 rows affected (0.00 sec)

 

  * 通过密码连接测试
[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 + 密码连接测试
[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

本文永久更新链接地址http://www.linuxidc.com/Linux/2017-07/145338.htm

linux
相关资讯       SSL加密连接MySQL 5.6 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款