MySQL 使用使用 SSL 连接配置详解连接配置详解
本文给大家分享的是如何配置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