MySQL开启SSL加密

摘自:

https://www.docs4dev.com/docs/zh/mysql/5.7/reference/creating-ssl-files-using-openssl.html#creating-ssl-files-using-openssl-unix-command-line

https://www.docs4dev.com/docs/zh/mysql/5.7/reference/using-encrypted-connections.html

https://www.docs4dev.com/docs/zh/mysql/5.7/reference/create-user.html#create-user-tls

https://www.jianshu.com/p/3371740b7b02 数字证书详解 

 

示例 1:在 Unix 上从命令行创建 SSL 文件

以下示例显示了一组用于创建 MySQL 服务器和 Client 端证书以及密钥文件的命令。您将需要通过 openssl 命令来响应多个提示。要生成测试文件,可以按 Enter 键进入所有提示。要生成供生产使用的文件,应提供非空响应。

# Create clean environment
rm -rf newcerts
mkdir newcerts && cd newcerts

# Create CA certificate
openssl genrsa 2048 > ca-key.pem
openssl req -new -x509 -nodes -days 3600 \
        -key ca-key.pem -out ca.pem

# Create server certificate, remove passphrase, and sign it
# server-cert.pem = public key, server-key.pem = private key
openssl req -newkey rsa:2048 -days 3600 \
        -nodes -keyout server-key.pem -out server-req.pem
openssl rsa -in server-key.pem -out server-key.pem
openssl x509 -req -in server-req.pem -days 3600 \
        -CA ca.pem -CAkey ca-key.pem -set_serial 01 -out server-cert.pem

# Create client certificate, remove passphrase, and sign it
# client-cert.pem = public key, client-key.pem = private key
openssl req -newkey rsa:2048 -days 3600 \
        -nodes -keyout client-key.pem -out client-req.pem
openssl rsa -in client-key.pem -out client-key.pem
openssl x509 -req -in client-req.pem -days 3600 \
        -CA ca.pem -CAkey ca-key.pem -set_serial 01 -out client-cert.pem

生成证书后,请验证它们:

 

openssl verify -CAfile ca.pem server-cert.pem client-cert.pem

您应该看到这样的响应:

server-cert.pem: OK
client-cert.pem: OK

要查看证书的内容(例如,检查证书有效的日期范围),请像下面这样调用 openssl :

openssl x509 -text -in ca.pem
openssl x509 -text -in server-cert.pem
openssl x509 -text -in client-cert.pem

现在,您可以使用一组文件,如下所示:

  • ca.pem:使用它来设置服务器端的ssl_ca系统变量和 Client 端端的--ssl-ca选项。 (如果使用了 CA 证书,则双方必须相同.)

  • server-cert.pemserver-key.pem:使用它们来设置服务器端的ssl_certssl_key系统变量。

  • client-cert.pemclient-key.pem:将它们用作 Client 端上--ssl-cert--ssl-key选项的参数。

 

加密连接的服务器端启动配置

在服务器端,--ssl选项指定服务器允许但不需要加密连接。默认情况下启用此选项,因此无需显式指定。

要要求 Client 端使用加密连接进行连接,请启用require_secure_transport系统变量。参见将加密连接配置为强制性

服务器端的这些系统变量指定允许 Client 端构建加密连接时服务器使用的证书和密钥文件:

  • ssl_ca:证书颁发机构(CA)证书文件的路径名。 (ssl_capath类似,但指定了 CA 证书文件目录的路径名。)

  • ssl_cert:服务器公用密钥证书文件的路径名。可以将此证书发送到 Client 端,并根据其拥有的 CA 证书进行身份验证。

  • ssl_key:服务器私钥文件的路径名。

例如,要为服务器启用加密连接,请在my.cnf文件中以以下几行启动它,并根据需要更改文件名:

[mysqld]
ssl_ca=ca.pem
ssl_cert=server-cert.pem
ssl_key=server-key.pem

 

 以非SSL方式连接数据库

mysql -h172.16.252.128 -uroot -p123456 --skip-ssl

 

创建用户示例

1、创建一个用户,既可以加密登录,也可以非加密登录,默认会以加密的方式登录

create user require_none identified by '123456' REQUIRE NONE;

加密方式登录

[root@node1 mysql]# mysql -h172.16.252.128 -p123456 -urequire_none

mysql> \s
--------------
mysql Ver 14.14 Distrib 5.7.34, for Linux (x86_64) using EditLine wrapper

Connection id: 7
Current database:
Current user: require_none@172.16.252.128
SSL: Cipher in use is ECDHE-RSA-AES128-GCM-SHA256

 非加密方式登录

[root@node1 mysql]# mysql -h172.16.252.128 -p123456 -urequire_none --skip-ssl


mysql> \s
--------------
mysql  Ver 14.14 Distrib 5.7.34, for Linux (x86_64) using  EditLine wrapper

Connection id:          8
Current database:
Current user:           require_none@172.16.252.128
SSL:                    Not in use

 

2、创建一个用户,必须以加密的方式进行登录

create user require_ssl identified by '123456' REQUIRE ssl;

默认以加密方式登录,可以成功登录

[root@node1 mysql]# mysql -h172.16.252.128 -p123456 -urequire_ssl


mysql> \s
--------------
mysql  Ver 14.14 Distrib 5.7.34, for Linux (x86_64) using  EditLine wrapper

Connection id:          10
Current database:
Current user:           require_ssl@172.16.252.128
SSL:                    Cipher in use is ECDHE-RSA-AES128-GCM-SHA256

 

以非加密方式登录,报错登录失败。

[root@node1 mysql]# mysql -h172.16.252.128 -p123456 -urequire_ssl --skip-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.
ERROR 1045 (28000): Access denied for user 'require_ssl'@'172.16.252.128' (using password: YES)

 

 

3、必须指定证书进行登录,--ssl-key和--ssl-cert

create user require_x509 identified by '123456' REQUIRE x509;

 

默认以加密方式登录,登录失败

[root@node1 mysql]# mysql -h172.16.252.128 -p123456 -urequire_x509
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'require_x509'@'172.16.252.128' (using password: YES)

 

指定证书进行登录

[root@node1 mysql]# mysql -h172.16.252.128 -p123456 -urequire_x509 --ssl-key=/var/lib/mysql/client-key.pem --ssl-cert=/var/lib/mysql/client-cert.pem

mysql> \s
--------------
mysql  Ver 14.14 Distrib 5.7.34, for Linux (x86_64) using  EditLine wrapper

Connection id:          14
Current database:
Current user:           require_x509@172.16.252.128
SSL:                    Cipher in use is ECDHE-RSA-AES128-GCM-SHA256

 

 4、指定证书并且证书发布者内容和根证书要匹配

这个参考了官方文档,但官网写的也有些问题,不太准 。

https://dev.mysql.com/doc/refman/5.7/en/create-user.html#create-user-tls

 

4.1 首先查看ca证书或server-cert 或 client-cert证书内容。

可以看到issuer 这部分内容,这部分是证书发行者相关信息,在创建证书时我们都填写过,我这里是随便写的。

 

[root@node1 mysql]# openssl x509 -text -in client-cert.pem
Certificate:
    Data:
        Version: 1 (0x0)
        Serial Number: 1 (0x1)
    Signature Algorithm: sha256WithRSAEncryption
        Issuer: C=ss, ST=de, L=fg, O=de, OU=eg, CN=cc/emailAddress=dd@ww.com
        Validity
            Not Before: Aug 30 09:49:09 2021 GMT
            Not After : Jul  9 09:49:09 2031 GMT
        Subject: C=eg, ST=eg, L=eg, O=eg, OU=eg, CN=eg/emailAddress=eg@qq.com
        Subject Public Key Info:
            Public Key Algorithm: rsaEncryption
                Public-Key: (2048 bit)

 

4.2参考官方文档,我只指定了证书的部分内容,但是登录时一直报错密码不对,

create user require_issuer identified by '123456' REQUIRE ISSUER 'C=ss/ST=de/L=fg/O=de/CN=cc/emailAddress=dd@ww.com';

通过在网上搜索,原来在错误日志中,可以看到证书不匹配的内容,我在创建用户时没有指定OU部分内容

 2021-08-31T09:12:51.314475Z 6 [Note] X509 issuer mismatch: should be '/C=ss/ST=de/L=fg/O=de/CN=cc/emailAddress=dd@ww.com' but is '/C=ss/ST=de/L=fg/O=de/OU=eg/CN=cc/emailAddress=dd@ww.com'

 重新创建用户,再次登录就正常了。

mysql> create user require_issuer identified by '123456' REQUIRE ISSUER '/C=ss/ST=de/L=fg/O=de/OU=eg/CN=cc/emailAddress=dd@ww.com';
Query OK, 0 rows affected (0.00 sec)

 

  

 4、指定证书登录并且证书subject信息要与建用户时指定的一样,否则无法登录。

先查看 client-crt 证书 subject 信息

[root@node1 mysql]# openssl x509 -text -in client-cert.pem
Certificate:
    Data:
        Version: 1 (0x0)
        Serial Number: 1 (0x1)
    Signature Algorithm: sha256WithRSAEncryption
        Issuer: C=ss, ST=de, L=fg, O=de, OU=eg, CN=cc/emailAddress=dd@ww.com
        Validity
            Not Before: Aug 30 09:49:09 2021 GMT
            Not After : Jul  9 09:49:09 2031 GMT
        Subject: C=eg, ST=eg, L=eg, O=eg, OU=eg, CN=eg/emailAddress=eg@qq.com
        Subject Public Key Info:
            Public Key Algorithm: rsaEncryption
                Public-Key: (2048 bit)
                Modulus:

创建用户

mysql> create user require_subject identified by '123456' REQUIRE subject '/C=eg/ST=eg/L=eg/O=eg/OU=eg/CN=eg/emailAddress=eg@qq.com';
Query OK, 0 rows affected (0.00 sec)

 

 

 

 

 

 

 

 

 

 

 

 

 
 

posted on 2021-08-30 18:35  柴米油盐酱醋  阅读(1601)  评论(0编辑  收藏  举报

导航