MySQL 5.7 使用 SSL加密连接实例(2)

* 修改数据目录下生成的SSL文件所属用户与权限
[root@MySQL ~]# chown -v mysql.mysql /data/mysql_data/*.pem
changed ownership of `/data/mysql_data/ca-key.pem' to mysql:mysql
changed ownership of `/data/mysql_data/ca.pem' to mysql:mysql
changed ownership of `/data/mysql_data/client-cert.pem' to mysql:mysql
changed ownership of `/data/mysql_data/client-key.pem' to mysql:mysql
changed ownership of `/data/mysql_data/private_key.pem' to mysql:mysql
changed ownership of `/data/mysql_data/public_key.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

* 查看生成的SSL文件
[root@MySQL ~]# ls -l /data/mysql_data/*.pem
-rw------- 1 mysql mysql 1679 Jun 24 20:54 /data/mysql_data/ca-key.pem
-rw-r--r-- 1 mysql mysql 1074 Jun 24 20:54 /data/mysql_data/ca.pem
-rw-r--r-- 1 mysql mysql 1078 Jun 24 20:54 /data/mysql_data/client-cert.pem
-rw------- 1 mysql mysql 1675 Jun 24 20:54 /data/mysql_data/client-key.pem
-rw------- 1 mysql mysql 1675 Jun 24 20:54 /data/mysql_data/private_key.pem
-rw-r--r-- 1 mysql mysql  451 Jun 24 20:54 /data/mysql_data/public_key.pem
-rw-r--r-- 1 mysql mysql 1078 Jun 24 20:54 /data/mysql_data/server-cert.pem
-rw------- 1 mysql mysql 1675 Jun 24 20:54 /data/mysql_data/server-key.pem

* 重启 MySQL 服务
[root@MySQL ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS!

* 连接MySQL 查看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.03 sec)

6. SSL + 密码连接测试
    * 创建用户并指定 SSL 连接 [ MySQL 5.7后推荐使用create user 方式创建用户 ]
mysql> create user 'ssl_test'@'%' identified by '123' require SSL;
Query OK, 0 rows affected (0.00 sec)

* 通过密码连接测试 [ 默认采用SSL连接,需要指定不使用SSL连接 ]
[root@MySQL ~]# mysql -h 192.168.60.129 -ussl_test -p'123' --ssl=0
mysql: [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 + 密码 连接测试
      SSL: Cipher in use is DHE-RSA-AES256-SHA 表示通过SSL连接
[root@MySQL ~]# mysql -h 192.168.60.129 -ussl_test -p'123'  --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.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.7.18 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.7.18, for linux-glibc2.5 (x86_64) using  EditLine wrapper
 
Connection id:    12
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.7.18 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:        7 min 34 sec
 
Threads: 1  Questions: 29  Slow queries: 0  Opens: 112  Flush tables: 1  Open tables: 105  Queries per second avg: 0.063
--------------

7. SSL + 密码 + 密钥连接

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/9c15aeeb2cfe42ca6800ea60bfa5c6b8.html