>  기사  >  데이터 베이스  >  SSL 연결 튜토리얼 예시

SSL 연결 튜토리얼 예시

零下一度
零下一度원래의
2017-06-30 15:25:191857검색

1. Background

* 프로덕션 환경에서 보안은 항상 무시할 수 없는 문제이며, 데이터베이스 보안이 최우선입니다. 가장 중요한 것은 모든 데이터가 데이터베이스에 저장되기 때문입니다

* 암호화되지 않은 방법을 사용하여 MySQL 데이터베이스에 연결하는 경우 네트워크에서 전송되는 모든 정보는 일반 텍스트로 전송되므로 네트워크의 모든 사람이 가로챌 수 있습니다. 민감한 정보가 유출될 수 있습니다. 민감한 정보(예: 비밀번호)를 전송할 때 SSL 연결을 사용할 수 있습니다.

* 버전이 5.7.6 미만인 경우

MySQL 5.6 SSL 구성

방법을 따르세요.

2.

MySQL 연결 방법 *

소켓 연결

ㅋㅋㅋ                       

  * SSL + 비밀번호 + 키 연결

3. SSL 소개

*

SSL은 컴퓨터 네트워크에서 보안 통신을 위한 암호화 프로토콜인 SSL/TLS를 의미합니다. 사용자의 전송이 SSL을 통하지 않는다고 가정하면 네트워크에서 일반 텍스트로 전송되며 이는 숨은 동기를 가진 사람들에게 기회를 제공합니다. 따라서 이제 Facebook, Twitter, YouTube, Taobao 등과 같은 많은 웹사이트에서는 기본적으로 SSL이 활성화되어 있습니다.

4. 환경 [SeLinux 끄기]

*

시스템 환경

[root@MySQL ~]# cat /etc/redhat-release [root@MySQL ~]# cat /etc/redhat-release 
CentOS release 6.9 (Final)
[root@MySQL ~]# uname -r
 
2.6.32-696.3.2.el6.x86_64
 
[root@MySQL ~]# getenforce 
Disabled

MySQL 环境 [ MySQL 5.7安装前面篇章已做详细介绍 ]

 have_openssl 与 have_ssl 值都为DISABLED表示ssl未开启

[root@MySQL ~]# mysql -p'123'
mysql: [Warning] Using a password on the command line interface can be insecure.
 
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 6
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.
CentOS 릴리스 6.9 (최종)
[root@MySQL ~] # uname -r
 
2.6.32-696.3.2.el6.x86_64🎜
 🎜
[root@MySQL ~]# getenforce 🎜
비활성화됨🎜🎜<p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; 클리어: 둘 다; 높이: 자동; 색상: #2c2c2c; 글꼴 계열: '宋体', 'Arial Narrow ', arial, serif; 글꼴 크기: 28px; 공백: 일반;">* <span style="padding: 0px; 글꼴 계열: '微软雅黑' '마이크로소프트 야헤이'; 글꼴 크기: 16px;">MySQL 环境 [ <span style="padding: 0px; 여백: 0px; 글꼴 크기: 16px;">MySQL 5.7안전</span><span style="padding: 0px; 여백: 0px; 글꼴 계열: '微软雅黑', 'Microsoft YaHei'; 글꼴 크기: 16px;">앞면篇章已做详细介绍]</span></span></p> <p style="padding: 0px; 여백 상단: 0px; 여백 하단: 0px; 명확함: 둘 다; 높이: 자동; 색상: #2c2c2c; 글꼴 모음: '宋体', 'Arial Narrow', arial, serif; 글꼴 크기: 14px; 줄 높이: 28px; 공백: 일반;"><span yahei><span style="패딩: 0px; 여백: 0px; 글꼴 계열: '微软雅黑', 'Microsoft YaHei';"> <span style="padding: 0px; 여백: 0px; 글꼴 계열: '微软雅黑', 'Microsoft YaHei'; 글꼴 크기: 16px;"><strong style="패딩: 0px; 여백: 0px; 색상: #2c2c2c; 글꼴 계열: '微软雅黑', 'Microsoft YaHei'; 글꼴 크기: 14px; 줄 높이: 28px; 공백: 일반;">have_openssl 与 have_ssl 值都为DISABLED表示ssl未开启</strong></span></span></span></p>🎜🎜<code class="bash plain"> [root@MySQL ~]# mysql -p'123'🎜
mysql: [경고] 명령 라인 인터페이스에서 비밀번호를 사용하면 안전하지 않을 수 있습니다.🎜 🎜
MySQL 모니터에 오신 것을 환영합니다.   명령은 ; 또는 g로 끝납니다.🎜MySQL 연결id 은 6🎜
서버 버전: 5.7.18 MySQL 커뮤니티 서버(GPL)🎜
 🎜
저작권(c) 2000, 2017, Oracle 및/또는  그 계열사. 모든 권리 보유.🎜
🎜
Oracle은 Oracle Corporation의 등록 상표이며 /또는 해당🎜
계열사. 다른 이름은 해당🎜
소유자🎜
 🎜
입력 'help;' 또는 'h' for 도움말. 'c' to clear 현재 입력 문🎜🎜 🎜
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.18    |
+-----------+
1 row in set (0.00 sec)
 
mysql> show variables like 'have%ssl%';
+---------------+----------+
| Variable_name | Value    |
+---------------+----------+
| have_openssl  | DISABLED |
| have_ssl      | DISABLED |
+---------------+----------+
2 rows in set (0.02 sec)
 
mysql> show variables like 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |
+---------------+-------+
1 row in set (0.01 sec)
 
mysql> show variables like 'datadir';
+---------------+-------------------+
| Variable_name | Value             |
+---------------+-------------------+
| datadir       | /data/mysql_data/ |
+---------------+-------------------+
1 row in set (0.01 sec)

5. SSL配置

   *  利用自带工具生成SSL상关文件

 

[root@MySQL ~]# /usr/local/mysql/bin/mysql_ssl_rsa_setup --datadir=/data/mysql_data[root@MySQL ~]# /usr/local/mysql/bin/mysql_ssl_rsa_setup --datadir=/data/mysql_data
Generating a 2048 bit RSA private key
..........................................................................+++
.....+++
writing new private key to 'ca-key.pem'
-----
Generating a 2048 bit RSA private key
.......................................................................................................................................................................+++
...+++
writing new private key to 'server-key.pem'
-----
Generating a 2048 bit RSA private key
.....................+++
...........................................+++
writing new private key to 'client-key.pem'
-----
 * 查看生成的SSL文件
[root@MySQL ~]# ls -l /data/mysql_data/*.pem
-rw------- 1 root root 1679 Jun 24 20:54 /data/mysql_data/ca-key.pem
-rw-r--r-- 1 root root 1074 Jun 24 20:54 /data/mysql_data/ca.pem
-rw-r--r-- 1 root root 1078 Jun 24 20:54 /data/mysql_data/client-cert.pem
-rw------- 1 root root 1675 Jun 24 20:54 /data/mysql_data/client-key.pem
생성 중 2048비트 RSA 개인 키
.............. ................................................. .....+++🎜
.....+++🎜
새 개인 키 쓰기'ca-key.pem'🎜
------🎜
2048비트 RSA 생성 개인 키🎜
....................... ................................................. ................................................. ............................................++++🎜
...+++🎜
'server-key.pem'🎜
------🎜
2048비트 RSA 개인 키 생성🎜
.....................+++🎜
.................................. .......++++🎜
새 개인 키 쓰기'client-key.pem'🎜
------🎜
🎜 * 查看生成的SSL文件🎜🎜🎜🎜
🎜🎜[root@MySQL ~]# ls -l /data/mysql_data/*.pem🎜
-rw--- ---- 1 루트 루트 1679 6월 24일 20:54 /data/mysql_data/ca-key.pem🎜<div class="line number3 index2 alt2"> <code class="bash plain">-rw-r--r-- 1 root root 1074 6월 24일 20:54 /data/mysql_data/ca.pem🎜
-rw-r--r-- 1 루트 루트 1078 6월 24일 20:54 /data/mysql_data/client-cert.pem🎜
-rw------- 1 root root 1675 6월 24일 20:54  code><code class="bash plain">/data/mysql_data/client-key.pem🎜
-rw------- 1 root root 1675 6월 24일 20:54 /data/mysql_data/private_key.pem-rw------- 1 root root 1675 Jun 24 20:54 /data/mysql_data/private_key.pem
-rw-r--r-- 1 root root  451 Jun 24 20:54 /data/mysql_data/public_key.pem
-rw-r--r-- 1 root root 1078 Jun 24 20:54 /data/mysql_data/server-cert.pem
-rw------- 1 root root 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)
-rw-r--r-- 1 루트 루트  451 6월 24일 20:54 /data/mysql_data/public_key.pem
-rw-r--r-- 1 root root 1078 6월 24일 20:54 /data/mysql_data/ 서버 인증서.pem🎜
-rw---- -- 1 루트 루트 1675 6월 24일 20:54 /data/mysql_data/server-key.pem🎜
* 중복 MySQL 服务🎜
[root@MySQL ~]# /etc/init.d/mysqld 다시 시작🎜
MySQL을 종료하는 중입니다.. 성공했습니다! 🎜
MySQL을 시작합니다. 성공했습니다!🎜
  * 连接MySQL 查看SSL开启状态

      

mysql&gt ; 'have%ssl%';🎜
+---------------+-------+🎜
| 변수_이름 | 값 |🎜
+---------------+------ -+🎜
| have_openssl  | 예   |🎜🎜| have_ssl      | 예   |🎜
+---------------+------ -+🎜
2 행  설정 (0.03초)🎜

6. SSL + 비밀번호 연결 테스트

* 사용자 생성 및 SSL 연결 지정 [Create user 사용을 권장합니다. 사용자 ]

mysql> 사용자 생성 'ssl_test'@'%' 식별자 '123' <code class="bash plain">SSL 필요;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
쿼리 확인, 0개 행이 영향을 받음 (0.00초)
🎜 * 비밀번호 연결을 통해 테스트[🎜SSL 연결은 기본적으로 사용되며, SSL 연결을 사용하지 않도록 지정해야 합니다🎜]🎜🎜🎜
🎜🎜 [root@MySQL ~]# mysql -h 192.168.60.129 -ussl_test -p'123' --ssl=0🎜
mysql: [경고] 명령에 비밀번호 사용 라인 인터페이스는 안전하지 않을 수 있습니다.🎜
ERROR 1045 (28000): 액세스가 거부되었습니다 code><code class="bash 키워드">for 사용자 'ssl_test'@ code><code class="bash string">'192.168.60.129' (비밀번호 사용: YES)🎜
🎜🎜 * SSL + 비밀번호 연결을 통해 테스트🎜🎜🎜

🎜 SSL: 사용 중인 암호는 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 + 密码 + 密钥连接

 

    * 创建用户并指定 X509 [ SSL+密钥 ] 连接 [ MySQL 5.7后推荐使用create user 方式创建用户 ]

 

mysql> create user 'X509_test'@'%' identified by '123' require X509;
Query OK, 0 rows affected (0.00 sec)
* 通过密码连接测试
[root@MySQL ~]# mysql -h 192.168.60.129 -uX509_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 'X509_test'@'192.168.60.129' (using password: YES)
* communica SSL +密码 连接测试
[root@MySQL ~]# mysql -h 192.168. 60.129 -uX509_test -p'123' --ssl[root@MySQL ~]# mysql -h 192.168.60.129 -uX509_test -p'123' --ssl
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'X509_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 -uX509_test -p'123' --ssl-cert=/data/mysql_data/client-cert.pem --ssl-key=/data/mysql_data/client-key.pem 
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 21
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.
mysql: [경고] 에서 비밀번호 사용 명령 라인 인터페이스는 안전하지 않을 수 있습니다.
오류 1045(28000): <code class="bash plain">사용자'X509_test'에 대한 액세스  거부됨 @'192.168.60.129' (비밀번호 사용: 예)> 높이: 자동; 색상: #2c2c2c; 글꼴 크기: 28px;">
🎜    SSL: 사용 중인 암호는 DHE-RSA-AES256-SHA 表示通过SSL连接🎜🎜🎜🎜

 

🎜🎜[root@MySQL ~]# mysql -h 192.168.60.129 - uX509_test -p'123' --ssl-cert=/data/mysql_data/client-cert.pem --ssl-key=/data/mysql_data/client-key.pem 🎜
mysql: [경고] 명령 에 비밀번호 사용 line 인터페이스는 안전하지 않을 수 있습니다.🎜🎜MySQL 모니터에 오신 것을 환영합니다.   명령은 ; 또는 g로 끝납니다.🎜
MySQL 연결 id 은 21🎜
서버 버전: 5.7.18 MySQL 커뮤니티 서버(GPL)🎜
🎜
저작권(c) 2000, 2017, Oracle 및/또는그 계열사입니다. 모든 권리 보유.🎜
🎜
Oracle은 Oracle Corporation의 등록 상표이며 /또는 해당🎜
계열사. 다른 이름은 해당🎜
소유자의 상표일 수 있습니다.🎜🎜 🎜
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:     21
Current database: 
Current user:      X509_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:         18 min 27 sec
 
Threads: 1  Questions: 40  Slow queries: 0  Opens: 118  Flush tables: 1  Open tables: 111  Queries per second avg: 0.036
--------------

위 내용은 SSL 연결 튜토리얼 예시의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:MySQL에서 중복 데이터를 제거하는 방법은 무엇입니까?다음 기사:MySQL에서 중복 데이터를 제거하는 방법은 무엇입니까?

관련 기사

더보기