如何在 Linux 上为 MySQL 服务器和客户端设置 SSL
来源:tutorialspoint
2023-08-26 20:40:17
0浏览
收藏
有志者,事竟成!如果你在学习数据库,那么本文《如何在 Linux 上为 MySQL 服务器和客户端设置 SSL》,就很适合你!文章讲解的知识点主要包括,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~
在本教程中,我将介绍如何使用 SSH 连接进行加密,建立与 MySQL 服务器的安全连接,从而使数据库中的数据安全,黑客无法窃取数据。 SSL用于验证SSL证书的方式,可以防范网络钓鱼攻击。这还将向您展示如何在 MySQL 服务器上启用 SSL。
启用 SSL 支持
连接到 MySQL 服务器并检查 MySQL 服务器的 SSL 状态
# mysql -u root -p mysql> show variables like '%ssl%'; Output: +---------------+----------+ | Variable_name | Value | +---------------+----------+ | have_openssl | DISABLED | | have_ssl | DISABLED | | ssl_ca | | | ssl_capath | | | ssl_cert | | | ssl_cipher | | | ssl_key | | +---------------+----------+ 7 rows in set (0.00 sec) mysql> \q Bye
为 MySQL 生成 SSL 证书
创建用于存储证书文件的目录
# mkdir /etc/certificates # cd /etc/certificates
生成服务器证书
# openssl genrsa 2048 > ca-key.pem Generating RSA private key, 2048 bit long modulus ...................................................................................+++ ..........+++ e is 65537 (0x10001) # openssl req -newkey rsa:2048 -days 1000 -nodes -keyout server-key.pem > server-req.pem Generating a 2048 bit RSA private key ..................+++ ..............................................................................................+++ writing new private key to 'server-key.pem' You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [XX]: State or Province Name (full name) []: Locality Name (eg, city) [Default City]: Organization Name (eg, company) [Default Company Ltd]: Organizational Unit Name (eg, section) []: Common Name (eg, your name or your server's hostname) []: Email Address []: Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []: # openssl x509 -req -in server-req.pem -days 1000 -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 > server-cert.pem Signature ok subject=/C=XX/L=Default City/O=Default Company Ltd Error opening CA Certificate ca-cert.pem 139991633303368:error:02001002:system library:fopen:No such file or directory:bss_file.c:398:fopen('ca-cert.pem','r') 139991633303368:error:20074002:BIO routines:FILE_CTRL:system lib:bss_file.c:400: unable to load certificate Generating client certificates
# openssl req -newkey rsa:2048 -days 1000 -nodes -keyout client-key.pem > client-req.pem Generating a 2048 bit RSA private key ...............................................+++ .................+++ writing new private key to 'client-key.pem' ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [XX]: State or Province Name (full name) []: Locality Name (eg, city) [Default City]: Organization Name (eg, company) [Default Company Ltd]: Organizational Unit Name (eg, section) []: Common Name (eg, your name or your server's hostname) []: Email Address []: Please enter the following 'extra' attributes openssl x509 -req -in client-req.pem -days 1000 -CA ca-# cert.pem -CAkey ca-key.pem -set_serial 01 > client-cert.pem Signature ok subject=/C=XX/L=Default City/O=Default Company Ltd Error opening CA Certificate ca-cert.pem 140327140685640:error:02001002:system library:fopen:No such file or directory:bss_file.c:398:fopen('ca-cert.pem','r') 140327140685640:error:20074002:BIO routines:FILE_CTRL:system lib:bss_file.c:400: unable to load certificate to be sent with your certificate request A challenge password []: An optional company name []:
现在打开 my.cnf 文件并添加证书
# vi /etc/my.cnf [mysqld] ssl-ca=/etc/certificates/cacert.pem ssl-cert=/etc/certificates/server-cert.pem ssl-key=/etc/certificates/server-key.pem
重启MySQL服务器并检查证书状态
#service mysqld restart #mysql -uroot -p mysql>show variables like '%ssl%'; +---------------+-----------------------------------+ | Variable_name | Value | +---------------+-----------------------------------+ | have_openssl | YES | | have_ssl | YES | | ssl_ca |/etc/certificates/cacert.pem | | ssl_capath | | | ssl_cert | /etc/certificates/server-cert.pem | | ssl_cipher | | | ssl_key | /etc/certificates/server-key.pem | +---------------+-----------------------------------+ 7 rows in set (0.00 sec)
创建具有 SSL 访问权限的用户
mysql> GRANT ALL PRIVILEGES ON *.* TO ‘ssl_user’@’%’ IDENTIFIED BY ‘password’ REQUIRE SSL; mysql> FLUSH PRIVILEGES;
为 MySQL 客户端配置 SSL
从服务器端,我们需要将 client-cert.pem client-key.pem client-req.pem 从服务器复制到客户端。
# scp /etc/ certificates/client-cert.pem root@192.168.87.158:/etc/certificates # scp /etc/ certificates/client-key.pem root@192.168.87.158:/etc/certificates # scp /etc/ certificates/client-req.pem root@192.168.87.158:/etc/certificates
文件传输到客户端后,将连接到客户端并尝试使用 SSL 证书连接到 MySQL。
# mysql --ssl-ca=ca-cert.pem --ssl-cert=client-cert.pem --ssl-key=client-key.pem -h 192.168.87.156 -u ssluser -p Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.1.73 Source distribution Copyright (c) 2000, 2013, 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> status -------------- mysql Ver 14.14 Distrib 5.1.73, for redhat-linux-gnu (x86_64) using readline 5.1 Connection id: 3 Current database: Current user: root@localhost SSL: Clipher in use is DHE-RSA-AES256-SHA Current pager: stdout Using outfile: '' Using delimiter: ; Server version: 5.1.73 Source distribution Protocol version: 10 Connection: 192.168.87.158 via TCP/IP Server characterset: latin1 Db characterset: latin1 Client characterset: latin1 Conn. characterset: latin1 UNIX socket: /var/lib/mysql/mysql.sock Uptime: 11 min 13 sec Threads: 1 Questions: 8 Slow queries: 0 Opens: 15 Flush tables: 1 Open tables: 8 Queries per second avg: 0.11 -------------
稍后,在 /etc/my.cnf 文件中添加设置,以便永久连接到 MySQL 服务器时,我们应该使用 SSL 进行连接。
# vi /etc/my.cnf [client] ssl-ca=/etc/certificates/ client-cert.pem ssl-cert=/etc/certificates/client-cert.pem ssl-key=/etc/certificates/client-key.pem
完成此配置和设置后,您现在可以使用 SSL 密钥从客户端连接到 MySQL 服务器,以保护数据不被窃取,同时也可以保护数据免受黑客攻击。
终于介绍完啦!小伙伴们,这篇关于《如何在 Linux 上为 MySQL 服务器和客户端设置 SSL》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布数据库相关知识,快来关注吧!
版本声明
本文转载于:tutorialspoint 如有侵犯,请联系study_golang@163.com删除

- 上一篇
- 解决golang报错:use of unexported field 'x' outside of struct

- 下一篇
- 如何在MySQL中查找包含两个特定列的所有表?
查看更多
最新文章
-
- 数据库 · MySQL | 3小时前 |
- MySQL数据恢复方法与工具推荐
- 384浏览 收藏
-
- 数据库 · MySQL | 2天前 |
- 如何检测电脑是否安装MySQL的5种方法
- 278浏览 收藏
-
- 数据库 · MySQL | 2天前 |
- MySQL分区表查询优化技巧
- 126浏览 收藏
-
- 数据库 · MySQL | 2天前 |
- MySQL建库语句与字符集设置教程
- 414浏览 收藏
-
- 数据库 · MySQL | 4天前 |
- MySQL中AS别名用法详解
- 320浏览 收藏
-
- 数据库 · MySQL | 4天前 |
- MySQL创建带主键的表实例
- 247浏览 收藏
-
- 数据库 · MySQL | 5天前 |
- 主外键关系怎么建立?
- 149浏览 收藏
-
- 数据库 · MySQL | 5天前 |
- MySQL中IF函数使用详解
- 392浏览 收藏
-
- 数据库 · MySQL | 6天前 |
- MySQL中IF函数使用详解
- 268浏览 收藏
-
- 数据库 · MySQL | 6天前 |
- MySQL入门:核心概念与操作全解析
- 162浏览 收藏
-
- 数据库 · MySQL | 6天前 |
- MySQL事务是什么?如何保证数据一致性?
- 349浏览 收藏
-
- 数据库 · MySQL | 6天前 |
- MySQL数据分片实现方法及常见方案解析
- 363浏览 收藏
查看更多
课程推荐
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 499次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 484次学习
查看更多
AI推荐
-
- PandaWiki开源知识库
- PandaWiki是一款AI大模型驱动的开源知识库搭建系统,助您快速构建产品/技术文档、FAQ、博客。提供AI创作、问答、搜索能力,支持富文本编辑、多格式导出,并可轻松集成与多来源内容导入。
- 30次使用
-
- AI Mermaid流程图
- SEO AI Mermaid 流程图工具:基于 Mermaid 语法,AI 辅助,自然语言生成流程图,提升可视化创作效率,适用于开发者、产品经理、教育工作者。
- 842次使用
-
- 搜获客【笔记生成器】
- 搜获客笔记生成器,国内首个聚焦小红书医美垂类的AI文案工具。1500万爆款文案库,行业专属算法,助您高效创作合规、引流的医美笔记,提升运营效率,引爆小红书流量!
- 859次使用
-
- iTerms
- iTerms是一款专业的一站式法律AI工作台,提供AI合同审查、AI合同起草及AI法律问答服务。通过智能问答、深度思考与联网检索,助您高效检索法律法规与司法判例,告别传统模板,实现合同一键起草与在线编辑,大幅提升法律事务处理效率。
- 877次使用
-
- TokenPony
- TokenPony是讯盟科技旗下的AI大模型聚合API平台。通过统一接口接入DeepSeek、Kimi、Qwen等主流模型,支持1024K超长上下文,实现零配置、免部署、极速响应与高性价比的AI应用开发,助力专业用户轻松构建智能服务。
- 944次使用
查看更多
相关文章
-
- MySQL主从切换的超详细步骤
- 2023-01-01 501浏览
-
- Mysql-普通索引的 change buffer
- 2023-01-25 501浏览
-
- MySQL高级进阶sql语句总结大全
- 2022-12-31 501浏览
-
- Mysql报错:message from server: * is blocked because of many
- 2023-02-24 501浏览
-
- 腾讯云大佬亲码“redis深度笔记”,不讲一句废话,全是精华
- 2023-02-22 501浏览