Mysql关系型数据库管理系统
MySQL是一个开放源码的小型关联式数据库管理系统,开发者为瑞典MySQL AB公司。MySQL被广泛地应用在Internet上的中小型网站中。由于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,许多中小型网站为了降低网站总体拥有成本而选择了MySQL作为网站数据库。
本文给大家分享的是如何配置MySQL支持SSL连接方式的方法以及在docker中配置的具体案例,有需要的小伙伴可以参考下
查看是否支持 SSL
首先在 MySQL 上执行如下命令, 查询是否 MySQL 支持 SSL:
mysql> SHOW VARIABLES LIKE 'have_ssl'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | have_ssl | YES | +---------------+-------+ 1 row in set (0.02 sec)
当 have_ssl 为 YES 时, 表示此时 MySQL 服务已经支持 SSL 了. 如果是 DESABLE, 则需要在启动 MySQL 服务时, 使能 SSL 功能.
使用 OpenSSL 创建 SSL 证书和私钥
首先我们需要使用 openssl 来创建服务器端的证书和私钥. 我使用的 openssl 版本为:
>>> /usr/local/Cellar/openssl/1.0.2j/bin/openssl version OpenSSL 1.0.2j 26 Sep 2016
新建一个 ~/temp/cert 目录, 用于存放生成的证书和私钥
mkdir ~/temp/cert cd ~/temp/cert
创建 CA 私钥和 CA 证书
然后, 我们先来生成一个 CA 私钥:
openssl genrsa 2048 > ca-key.pem
当有了一个 CA 私钥, 我们接下来就可以使用这个私钥生成一个新的数字证书:
openssl req -sha1 -new -x509 -nodes -days 3650 -key ca-key.pem > ca-cert.pem
执行这个命令时, 会需要填写一些问题, 随便填写就可以了. 例如:
>>> openssl req -sha1 -new -x509 -nodes -days 3650 -key ca-key.pem > ca-cert.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) [AU]:CN State or Province Name (full name) [Some-State]:Beijing Locality Name (eg, city) []:Beijing Organization Name (eg, company) [Internet Widgits Pty Ltd]:xys Organizational Unit Name (eg, section) []:xys Common Name (e.g. server FQDN or YOUR name) []:xys Email Address []:yongshun1228@gmail.com
执行上述命令后, 我们就有了一个 CA 私钥和一个 CA 证书.
创建服务器端的 RSA 私钥和数字证书
接着, 我们需要创建服务器端的私钥和一个证书请求文件, 命令如下:
openssl req -sha1 -newkey rsa:2048 -days 3650 -nodes -keyout server-key.pem > server-req.pem
上面这个命令会生成一个新的私钥(server-key.pem), 同时会使用这个新私钥来生成一个证书请求文件(server-req.pem).
上面这个命令同样需要回答几个问题, 随便填写即可. 不过需要注意的是, A challenge password 这一项需要为空.
即:
>>> openssl req -sha1 -newkey rsa:2048 -days 3650 -nodes -keyout server-key.pem > 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) [AU]:CN State or Province Name (full name) [Some-State]:Beijing Locality Name (eg, city) []:Beijing Organization Name (eg, company) [Internet Widgits Pty Ltd]:xys Organizational Unit Name (eg, section) []:xys Common Name (e.g. server FQDN or YOUR name) []:xys Email Address []:yongshun1228@gmail.com Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []:
下一步, 我们需要将生成的私钥转换为 RSA 私钥文件格式:
openssl rsa -in server-key.pem -out server-key.pem
最后一步, 我们需要使用原先生成的 CA 证书来生成一个服务器端的数字证书:
openssl x509 -sha1 -req -in server-req.pem -days 3650 -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 > server-cert.pem
上面的命令会创建以服务器端的数字证书文件.
创建客户端的 RSA 私钥和数字证书
和服务器端所执行的命令类似, 我们也需要为客户端生成一个私钥和证书请求文件, 命令如下:
openssl req -sha1 -newkey rsa:2048 -days 3650 -nodes -keyout client-key.pem > client-req.pem
同样地, 我们需要将生成的私钥转换为 RSA 私钥文件格式:
openssl rsa -in client-key.pem -out client-key.pem
最后, 我们也需要为客户端创建一个数字证书:
openssl x509 -sha1 -req -in client-req.pem -days 3650 -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 > client-cert.pem
使用工具创建证书与私钥
前面我们介绍了如何使用 OpenSSL 来创建 SSL 连接的私钥和证书文件, 现在我们来看一个更简单的方法.
在 MySQL 5.7 中, 提供了一个名为 mysql_ssl_rsa_setup 的工具, 通过它, 我们可以很方便地创建 SSL 连接所需要的各种文件:
mkdir ~/temp/cert cd ~/temp/cert mysql_ssl_rsa_setup --datadir ./
上面的命令中, --datadir 表示生成的文件的目录.
当执行了上述命令后, 也会生成八个文件:
ca-key.pem ca.pem client-cert.pem client-key.pem private_key.pem public_key.pem server-cert.pem server-key.pem
这些文件和我们使用 OpenSSL 所创建的那八个文件的作用是一样的, 这里就不再详述了.
SSL 配置
在前面的步骤中, 我们已经生成了8个文件, 分别是:
ca-cert.pem: CA 证书, 用于生成服务器端/客户端的数字证书.
ca-key.pem: CA 私钥, 用于生成服务器端/客户端的数字证书.
server-key.pem: 服务器端的 RSA 私钥
server-req.pem: 服务器端的证书请求文件, 用于生成服务器端的数字证书.
server-cert.pem: 服务器端的数字证书.
client-key.pem: 客户端的 RSA 私钥
client-req.pem: 客户端的证书请求文件, 用于生成客户端的数字证书.
client-cert.pem: 客户端的数字证书.
接下来我们就需要分别配置服务器端和客户端.
服务器端配置
服务器端需要用到三个文件, 分别是: CA 证书, 服务器端的 RSA 私钥, 服务器端的数字证书, 我们需要在 [mysqld] 配置域下添加如下内容:
[mysqld] ssl-ca=/etc/mysql/ca-cert.pem ssl-cert=/etc/mysql/server-cert.pem ssl-key=/etc/mysql/server-key.pem
接着我们还可以更改 bind-address, 使 MySQL 服务可以接收来自所有 ip 地址的客户端, 即:
bind-address = *
当配置好后, 我们需要重启 MySQL 服务, 使能配置.
最后一步, 我们添加一个需要使用 SSL 才可以登录的帐号, 来验证一下我们所配置的 SSL 是否生效:
GRANT ALL PRIVILEGES ON *.* TO 'ssl_test'@'%' IDENTIFIED BY 'ssl_test' REQUIRE SSL; FLUSH PRIVILEGES;
当配置好后, 使用 root 登录 MySQL, 执行 show variables like '%ssl%' 语句会有如下输出:
mysql> show variables like '%ssl%'; +---------------+-----------------+ | Variable_name | Value | +---------------+-----------------+ | have_openssl | YES | | have_ssl | YES | | ssl_ca | ca.pem | | ssl_capath | | | ssl_cert | server-cert.pem | | ssl_cipher | | | ssl_crl | | | ssl_crlpath | | | ssl_key | server-key.pem | +---------------+-----------------+ 9 rows in set (0.01 sec)
客户端配置
客户端配置相对简单一些. 首先我们需要拷贝 ca-cert.pem, client-c