当前位置:   article > 正文

MySQL数据库SSL连接测试_mysql ssl

mysql ssl

环境信息:Centos7 + MySQL 5.7.21

在该环境上进行SSL连接测试。

MySQL Server 配置:

vi /etc/my.cnf

  1. [mysql]
  2. default-character-set = utf8mb4
  3. [client]
  4. default-character-set = utf8mb4
  5. # ssl_mode = [ DISABLED | PREFERRED(default if not set) | REQUIRED | VERIFY_CA | VERIFY_IDENTITY ]
  6. # ssl_mode 为客户端可选参数,如果客户端连接时指定了则生效的是客户端连接时指定的参数,如果客户端连接未指定则使用服务端的[client]中的配置参数,如果未配置默认为PREFRED。
  7. # DISABLED: 非SSL连接。
  8. # PREFERRED: SSL/no-ssl。如果不配置、默认就是该行为。先尝试SSL连接、如果失败则尝试非SSL连接。
  9. # REQUIRED: SSL连接。仅尝试SSL连接,SSL连接失败则失败、不会尝试非SSL连接。
  10. # VERIFY_CA: SSL连接,双向认证。校验ca证书、但不校验证书中的server host name。
  11. # VERIFY_IDENTITY: SSL连接,双向认证。校验ca证书、同时校验证书中的server host name。
  12. #ssl_mode = PREFERRED
  13. ssl_mode = VERIFY_CA
  14. [mysqld]
  15. datadir=/var/lib/mysql
  16. socket=/var/lib/mysql/mysql.sock
  17. validate_password=off
  18. # for ssl enable
  19. # 服务器端配置强制SSL连接、禁止非SSL连接
  20. require_secure_transport = ON
  21. # 下面3个参数如果不配置或者值配置成ca.pem, server-cert.pem, server-key.pem,不加路径,则MySQL默认行为是读取数据目录下的三个文件
  22. ssl-ca=/var/lib/mysql/ca.pem
  23. ssl-cert=/var/lib/mysql/server-cert.pem
  24. ssl-key=/var/lib/mysql/server-key.pem
  25. #for mysqlbinlog enable
  26. server-id=1
  27. log-bin=/var/lib/mysql/mysql-bin
  28. binlog_format=row
  29. expire_logs_days=15
  30. max_binlog_size=500m
  31. #utf8mb4
  32. character-set-client-handshake=false
  33. character-set-server=utf8mb4
  34. collation-server=utf8mb4_unicode_ci
  35. init_connect='SET NAMES utf8mb4'
  36. # for blob input, default value is 4194304 (4M) if not set
  37. #max_allowed_packet = 100M
  38. # for other performance settings
  39. max_connections = 2000
  40. max_connect_errors = 500
  41. back_log = 1000
  42. thread_cache_size = 64
  43. table_open_cache_instances = 500
  44. ###show global status like '%open%table%'; Open_tables / Opened_tables 85%~95%
  45. table_open_cache = 10000
  46. table_definition_cache = 2000
  47. innodb_thread_concurrency = 0
  48. max_tmp_tables = 100
  49. ## common sql commands
  50. ## -- show engine innodb status \G;
  51. ## -- show global variables like '%thread%';
  52. ## -- show global status like '%thread%';
  53. ## -- show global status like '%open%table%';
  54. ## -- show variables like '%table%';
  55. # Disablinr symbolic-links is recommended to prevent assorted security risks
  56. symbolic-links=0
  57. log-error=/var/log/mysqld.log
  58. pid-file=/var/run/mysqld/mysqld.pid

编写测试脚本:

vi cmd.sh

  1. #!/bin/sh
  2. # ssl-test-qftools
  3. SQL_SSL="
  4. # check if server start with --ssl:\n
  5. # -- show variables like '%have_ssl%';\n
  6. \n
  7. # check if server force accpet ssl session request ( deny no-ssl requests ):\n
  8. # -- show variables like 'require_secure_transport';\n
  9. \n
  10. # client connected and check current session ssl status:\n
  11. # -- show session status like 'Ssl_cipher';\n
  12. \n
  13. # check ssl configs:\n
  14. # -- show variables like '%ssl%';\n
  15. "
  16. # java keystore.jks generate cmd: keytool -importcert -trustcacerts -file ca.pem -keystore keystore216.jks -storepass 1q2w3e
  17. HOST=192.168.1.216
  18. PORT=3306
  19. USER=root
  20. DIR=./ssl_1.216
  21. # for MySQL old Versin
  22. #$SSLOPT=--ssl
  23. #$SSLOPT="--ssl --ssl-verify-server-cert"
  24. # for MySQL new Version
  25. SSLOPT_DISABLED="--ssl-mode=DISABLED"
  26. SSLOPT_PREFERRED="--ssl-mode=PREFERRED"
  27. SSLOPT_REQUIRED="--ssl-mode=REQUIRED"
  28. # ssl && also perform verification against the server CA certificate but no against the server host name in its certificate.
  29. SSLOPT_CA="--ssl-mode=VERIFY_CA"
  30. # ssl && also perform verification against the server CA certificate and (with VERIFY_IDENTITY) against the server host name in its certificate.
  31. SSLOPT_IDENTITY="--ssl-mode=VERIFY_IDENTITY"
  32. CA=$DIR/ca.pem
  33. CERT=$DIR/client-cert.pem
  34. KEY=$DIR/client-key.pem
  35. # 客户端连接时不指定相关参数、由服务器配置决定最终行为
  36. CMD0="mysql -h$HOST -P$PORT -u$USER -p "
  37. # 客户端连接指定配置
  38. # no-ssl
  39. CMD1="mysql -h$HOST -P$PORT -u$USER -p $SSLOPT_DISABLED"
  40. # ssl/no-ssl 先尝试SSL连接、如果失败则尝试非SSL连接
  41. CMD2="mysql -h$HOST -P$PORT -u$USER -p $SSLOPT_PREFERRED"
  42. # ssl单向,SSL连接失败则失败、不会尝试非SSL连接
  43. CMD3="mysql -h$HOST -P$PORT -u$USER -p $SSLOPT_REQUIRED"
  44. # ssl单向、虽然多余传ca证书、客户端证书和秘钥(实际上无需传),实际效果还是单向。MySQL Server my.cnf [client] 中如果配置为 ssl_mode = VERIFY_CA ,连接时客户端如果指定ssl_mode参数则会覆盖,以客户端配置优先
  45. CMD4="mysql -h$HOST -P$PORT -u$USER -p $SSLOPT_REQUIRED --ssl-ca=$CA --ssl-cert=$CERT --ssl-key=$KEY"
  46. # ssl双向认证,不验证hostname
  47. CMD5="mysql -h$HOST -P$PORT -u$USER -p $SSLOPT_CA --ssl-ca=$CA --ssl-cert=$CERT --ssl-key=$KEY"
  48. # ssl双向认证 && 验证hostname
  49. CMD6="mysql -h$HOST -P$PORT -u$USER -p $SSLOPT_IDENTITY --ssl-ca=$CA --ssl-cert=$CERT --ssl-key=$KEY"
  50. if [ ! $# -eq 1 ] || [ "$1" == "-h" ];then
  51. echo ""
  52. echo "Please user: $0 {type}"
  53. echo " type: 0 | 1 | 2 | 3 | 4 | 5 | 6"
  54. echo " type 0 for run: $CMD0"
  55. echo " type 1 for run: $CMD1"
  56. echo " type 2 for run: $CMD2"
  57. echo " type 3 for run: $CMD3"
  58. echo " type 4 for run: $CMD4"
  59. echo " type 5 for run: $CMD5"
  60. echo " type 6 for run: $CMD6"
  61. echo -e "\n---------------------\n"
  62. echo "About sql for show ssl infos after mysql connected: "
  63. echo -e $SQL_SSL
  64. exit
  65. fi
  66. inputType=$1
  67. if [ -z inputType ];then
  68. inputType=1
  69. fi
  70. echo "inputType = $inputType"
  71. if [ $inputType -eq 0 ];then
  72. echo $CMD0
  73. $CMD0
  74. elif [ $inputType -eq 1 ];then
  75. echo $CMD1
  76. $CMD1
  77. elif [ $inputType -eq 2 ];then
  78. echo $CMD2
  79. $CMD2
  80. elif [ $inputType -eq 3 ];then
  81. echo $CMD3
  82. $CMD3
  83. elif [ $inputType -eq 4 ];then
  84. echo $CMD4
  85. $CMD4
  86. elif [ $inputType -eq 5 ];then
  87. echo $CMD5
  88. $CMD5
  89. elif [ $inputType -eq 6 ];then
  90. echo $CMD6
  91. $CMD6
  92. else
  93. echo "ERROR: inputType unsupport!"
  94. exit
  95. fi

从MySQL Server服务器拷贝证书和Key到客户端机器上,然后客户端机器上执行相关测试:
[root@localhost ssl_test]# ls -l
total 4
-rwxr--r-- 1 root root 3165 Apr 25 01:40 cmd.sh
drwxr-xr-x 2 root root   62 Apr 24 21:45 ssl_1.216
[root@localhost ssl_test]# ls -l ssl_1.216/
total 12
-rw-r--r-- 1 root root 1107 Apr 24 21:45 ca.pem
-rw-r--r-- 1 root root 1107 Apr 24 21:45 client-cert.pem
-rw------- 1 root root 1679 Apr 24 21:45 client-key.pem


SSL连接测试失败的情况:
./cmd.sh 0
./cmd.sh 1
./cmd.sh 6

SSL连接失败的执行详细信息:
[root@localhost ssl_test]# ./cmd.sh 0
inputType = 0
mysql -h192.168.1.216 -P3306 -uroot -p
Enter password: 
ERROR 2026 (HY000): SSL connection error: CA certificate is required if ssl-mode is VERIFY_CA or VERIFY_IDENTITY
[root@localhost ssl_test]# 

[root@localhost ssl_test]# ./cmd.sh 1
inputType = 1
mysql -h192.168.1.216 -P3306 -uroot -p --ssl-mode=DISABLED
Enter password: 
ERROR 3159 (HY000): Connections using insecure transport are prohibited while --require_secure_transport=ON.
[root@localhost ssl_test]# 

[root@localhost ssl_test]# ./cmd.sh 6
inputType = 6
mysql -h192.168.1.216 -P3306 -uroot -p --ssl-mode=VERIFY_IDENTITY --ssl-ca=./ssl_1.216/ca.pem --ssl-cert=./ssl_1.216/client-cert.pem --ssl-key=./ssl_1.216/client-key.pem
Enter password: 
ERROR 2026 (HY000): SSL connection error: SSL certificate validation failure
[root@localhost ssl_test]# 

SSL连接测试成功的情况:
./cmd.sh 2
./cmd.sh 3
./cmd.sh 4
./cmd.sh 5
 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/854859
推荐阅读
相关标签
  

闽ICP备14008679号