1 连接数据库判断是否支持SSL

mysql> SHOW VARIABLES LIKE 'have_ssl';

+---------------+-------+ | Variable_name | Value |

+---------------+-------+ | have_ssl  | YES |

+---------------+-------+ 1 row in set (0.02 sec)

2.判断当前数据库是否已经开启SSL

show variables like '%ssl%'

openresty执行mysql mysql have openssl_mysql

have_openssl和have_ssl字段显示DISABLE,表示当前mysql拥有ssl的功能,但是ssl功能未默认启动。

3. 检查当前数据库的连接状态

   \s

openresty执行mysql mysql have openssl_openresty执行mysql_02

上面显示的SSL: Not in use说明,当前连接没有在SSL安全连接中 
关闭当前MySQL会话:

----------------------------------- 生成秘钥和证书---------------------------------------------------

MySQL 5.7及更高版本提供了一个名为mysql_ssl_rsa_setup的实用程序,我们使用root权限生成相关的请求文件和证书对,为了能让mysql有权限去使用,命令中应该使用选项和参数--uid=mysql来指定uid。

 1: 命令

sudo  ./mysql_ssl_rsa_setup --uid=mysql

   注意:1.mysql_ssl_rsa_setup 路径:  

   我机器的路径为:

openresty执行mysql mysql have openssl_安全_03

  1. 查看生成文件的路径:

  方式1:

  注意:这里生成的文件路径一般情况在在当前路径下面 所有用find 名利来查找

sudo find / -name '*.pem' -ls

 我们的目标文件是:

openresty执行mysql mysql have openssl_安全_04

(可以使用一下命令 提高查询命中率)

client-key.pem’

openresty执行mysql mysql have openssl_openresty执行mysql_05

Ca.pem: 客户端证书,

client-key.pem: 客户端的 RSA 私钥

client-cert.pem: 客户端的数字证书.

方式2 采用一下方式生成

openresty执行mysql mysql have openssl_openresty执行mysql_06

这种方式弊端查询不太方便如图:

openresty执行mysql mysql have openssl_mysql_07

当然也可以执行 mysql_ssl_rsa_setup 文件的时候不加 --uid=mysql 当时需要修改生成文件的权限,比较麻烦建议加上

  1. 配置my.cnf

 这个有命令查询my.cnf位置,当时找到的现在没有找到 一般情况下是在一下位置:

 提供以下命令:我本机用不起,可以尝试下

[root@qht131 /]# mysqld --help --verbose | grep /etc/my.cnf

/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf

读取顺序:也就是先读取/etc/my.cnf,再读取/etc/mysql/my.cnf,接着读取安装目录下面的my.cnf

没有的话可以拷贝或者新建一个:

添加以下 :如图

openresty执行mysql mysql have openssl_安全_08

[mysqld]

ssl-ca=/usr/local/mysql/data/ca.pem

ssl-cert=/usr/local/mysql/data/client-cert.pem

ssl-key=/usr/local/mysql/data/client-key.pem

[mysql]

ssl-ca=/usr/local/mysql/data/ca.pem

ssl-cert=/usr/local/mysql/data/client-cert.pem

ssl-key=/usr/local/mysql/data/client-key.pem

  1. 查询是否修改成功:

openresty执行mysql mysql have openssl_安全_09

  1. 重启数据库:可以通过命令 也可以通过程序
  2. Mysql 创建用户,仅限SSL 连接 自行设置:

连接模式:常用的模式:

openresty执行mysql mysql have openssl_mysql_10

openresty执行mysql mysql have openssl_System_11

 

openresty执行mysql mysql have openssl_openresty执行mysql_12

JAVA 代码部分:

官网:https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-using-ssl.html

签名请采用官网:

测试代码:

package org.properssl;
import java.sql.*;
 import java.util.Properties;public class MySQLDemo {
    // MySQL 8.0 以下版本 - JDBC 驱动名及数据库 URL
     static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
     static   String DB_URL = "jdbc:mysql://192.168.101.133:3306/face";    // MySQL 8.0 以上版本 - JDBC 驱动名及数据库 URL
     //static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
     //static final String DB_URL = "jdbc:mysql://localhost:3306/RUNOOB?useSSL=false&serverTimezone=UTC";     // 数据库的用户名与密码,需要根据自己的设置
     static final String USER = "ejar";
     static final String PASS = "password";    public static void main(String[] args) {
         Connection conn = null;
         Statement stmt = null;
         try{
             // 注册 JDBC 驱动
             Class.forName(JDBC_DRIVER);            // 打开链接
             System.out.println("连接数据库...");
 //直接在客户端代码中设置系统属性:
 //            System.setProperty("javax.net.ssl.trustStore", "/Users/hengyang/idea_workspaces/ssl/java-jdbc-mariadb/src/test/resources/truststore");
 //            System.setProperty("javax.net.ssl.trustStorePassword", "mypassword");
 //            System.setProperty("javax.net.ssl.keyStore", "/Users/hengyang/idea_workspaces/ssl/java-jdbc-mariadb/src/test/resources/keystore");
 //            System.setProperty("javax.net.ssl.keyStorePassword", "mypassword");
 //            Properties info = new Properties();
 //            info.setProperty("useSSL", "true");
 //            info.setProperty("trustStoreType", "JKS");
 //            info.setProperty("user", USER);
 //            info.setProperty("password", PASS);
 //            conn =DriverManager.getConnection(DB_URL, info);            //使用Java命令行参数:
            //url
             Properties info = new Properties();
             info.setProperty("user", USER);
             info.setProperty("password", PASS);
             info.setProperty("useSSL", "true");
             info.setProperty("trustCertificateKeyStoreUrl", "file:///" + "/Users/hengyang/idea_workspaces/ssl/java-jdbc-mariadb/src/test/resources/truststore");
             info.setProperty("trustCertificateKeyStorePassword", "mypassword");
             info.setProperty("trustCertificateKeyStoreType", "JKS");
             info.setProperty("clientCertificateKeyStoreUrl", "file:///" + "/Users/hengyang/idea_workspaces/ssl/java-jdbc-mariadb/src/test/resources/keystore");
             info.setProperty("clientCertificateKeyStorePassword", "mypassword");
             info.setProperty("clientCertificateKeyStoreType", "JKS");
             conn =DriverManager.getConnection(DB_URL, info);            // 执行查询
             System.out.println(" 实例化Statement对象...");
             stmt = conn.createStatement();
             String sql;
             sql = "SELECT id FROM ad_admin";
             ResultSet rs = stmt.executeQuery(sql);            // 展开结果集数据库
             while(rs.next()){
                 // 通过字段检索
                 int id  = rs.getInt("id");
                 // 输出数据
                 System.out.print("ID: " + id);
                 System.out.print("\n");
             }
             // 完成后关闭
             rs.close();
             stmt.close();
             conn.close();
         }catch(SQLException se){
             // 处理 JDBC 错误
             se.printStackTrace();
         }catch(Exception e){
             // 处理 Class.forName 错误
             e.printStackTrace();
         }finally{
             // 关闭资源
             try{
                 if(stmt!=null) stmt.close();
             }catch(SQLException se2){
             }// 什么都不做
             try{
                 if(conn!=null) conn.close();
             }catch(SQLException se){
                 se.printStackTrace();
             }
         }
         System.out.println("Goodbye!");
     }
 }

代码中只有1,3中方式,

第二种:加载到JAVM 中:

openresty执行mysql mysql have openssl_System_13