使用客户端证书登录MySQL

本文涉及的产品
云数据库 RDS MySQL,集群版 2核4GB 100GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,高可用版 2核4GB 50GB
简介: 使用客户端证书登录MySQL登录MySQL具有安全性高、不用输入密码的优点,这里说明生成证书和登录的过程。

使用客户端证书登录MySQL登录MySQL具有安全性高、不用输入密码的优点,这里说明生成证书和登录的过程。


实验环境是Linux上的的MySQL 8.0.31社区版。


生成证书

使用openssl req创建X.509证书,下面的命令创建有效期10年的私钥,使用man req可以查询这个命令的帮助。


oracle@yao mysql$ openssl req -newkey rsa:2048 -days 3650 -nodes -keyout yao1-key.pem -out yao1-req.pem
Generating a 2048 bit RSA private key
............................................................................................+++
......+++
writing new private key to 'yao1-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]:CN
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 []:



检查刚刚生成的证书:


oracle@yao mysql$ ll *.pem

-rw-r--r--. 1 oracle oinstall 1704 12月  8 18:57 yao1-key.pem

-rw-r--r--. 1 oracle oinstall  952 12月  8 18:57 yao1-req.pem



这两个文件是私钥和请求证书。


下面的命令生成文件名是yao1-cert.pem的公钥:


oracle@yao mysql$ sudo openssl x509 -req -in yao1-req.pem -days 3650 -CA /u01/mysql/ca.pem -CAkey /u01/mysql/ca-key.pem -set_serial 01 -out yao1-cert.pem
Signature ok
subject=/C=CN/L=Default City/O=Default Company Ltd
Getting CA Private Key



校验公钥的有效性:


oracle@yao mysql$ openssl verify -CAfile /u01/mysql/ca.pem /u01/mysql/server-cert.pem yao1-cert.pem 
/u01/mysql/server-cert.pem: OK
yao1-cert.pem: OK

创建只有证书没有密码的账号

相应命令如下:


mysql> CREATE USER yao1 REQUIRE SUBJECT '/C=CN/L=Default City/O=Default Company Ltd';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> select * from component;
+--------------+--------------------+-------------------------------------+
| component_id | component_group_id | component_urn                       |
+--------------+--------------------+-------------------------------------+
|            1 |                  1 | file://component_validate_password  |
|            2 |                  2 | file://component_log_filter_dragnet |
+--------------+--------------------+-------------------------------------+
2 rows in set (0.00 sec)
mysql> uninstall component 'file://component_validate_password';
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE USER yao1 REQUIRE SUBJECT '/C=CN/L=Default City/O=Default Company Ltd';
Query OK, 0 rows affected (0.00 sec)




需要卸载component_validate_password组件后才能创建无密码的账号。


使用证书登录mysql和mysqlsh

登录mysql,在输入密码处直接回车即可登录

oracle@yao mysql$  mysql -uyao1 --ssl-cert yao1-cert.pem --ssl-key yao1-key.pem -p --ssl_ca=/u01/mysql/ca.pem
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3685
Server version: 8.0.31 MySQL Community Server - GPL
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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.
You are enforcing ssl connection via unix socket. Please consider
switching ssl off as it does not make connection via unix socket
any more secure.
mysql> \s
--------------
mysql  Ver 8.0.31 for Linux on x86_64 (MySQL Community Server - GPL)
Connection id:          3685
Current database:
Current user:           yao1@localhost
SSL:                    Cipher in use is ECDHE-RSA-AES128-GCM-SHA256
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server version:         8.0.31 MySQL Community Server - GPL
Protocol version:       10
Connection:             Localhost via UNIX socket
Server characterset:    utf8mb4
Db     characterset:    utf8mb4
Client characterset:    utf8mb4
Conn.  characterset:    utf8mb4
UNIX socket:            /u01/mysql/mysql.sock
Binary data as:         Hexadecimal
Uptime:                 3 days 5 hours 6 min 47 sec
Threads: 5  Questions: 11001  Slow queries: 0  Opens: 4565  Flush tables: 3  Open tables: 319  Queries per second avg: 0.039
--------------
mysql>


登录到MySQL Shell,到密码处直接回车即可:

oracle@yao mysql$ mysqlsh mysql://yao1@127.0.0.1 --ssl-cert yao1-cert.pem --ssl-key yao1-key.pem
Please provide the password for 'yao1@127.0.0.1':
Save password for 'yao1@127.0.0.1'? [Y]es/[N]o/Ne[v]er (default No):
MySQL Shell 8.0.31
Copyright (c) 2016, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
Other names may be trademarks of their respective owners.
Type '\help' or '\?' for help; '\quit' to exit.
Creating a Classic session to 'yao1@127.0.0.1?ssl-cert=yao1-cert.pem&ssl-key=yao1-key.pem'
Fetching schema names for auto-completion... Press ^C to stop.
Your MySQL connection id is 3686
Server version: 8.0.31 MySQL Community Server - GPL
No default schema selected; type \use <schema> to set one.
 MySQL  127.0.0.1:3306 ssl  JS >
相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
1天前
|
关系型数据库 MySQL Linux
数据类型和运算符(MySQL服务器的安装,MySQL客户端,数据类型,运算符,MySQL的语法规范)
无论是对于初学者还是有经验的开发者,了解MySQL的安装、客户端使用、数据类型、运算符和语法规范都是至关重要的。这不仅有助于高效地管理和查询数据,而且对于设计和实现数据库解决方案来说是基础工作。通过深入学习和实践这些知识,您可以更好地发挥MySQL数据库的强大功能。
8 2
|
1天前
|
存储 分布式计算 DataWorks
DataWorks产品使用合集之在本地客户端一直无法连接ADB MySQL,是什么原因
DataWorks作为一站式的数据开发与治理平台,提供了从数据采集、清洗、开发、调度、服务化、质量监控到安全管理的全套解决方案,帮助企业构建高效、规范、安全的大数据处理体系。以下是对DataWorks产品使用合集的概述,涵盖数据处理的各个环节。
|
7天前
|
SQL 关系型数据库 MySQL
MySQL——如何查看MySQL登录日志
MySQL——如何查看MySQL登录日志
21 0
|
2月前
|
关系型数据库 MySQL 开发工具
免密登录mysql
免密登录mysql
|
2月前
|
SQL 关系型数据库 MySQL
MySQL数据库基础练习系列13、用户注册与登录系统
MySQL数据库基础练习系列13、用户注册与登录系统
25 1
|
1月前
|
Ubuntu 关系型数据库 MySQL
MySQL5.7在Ubuntu安装[单机软件],第一步登录root,sudo su -
MySQL5.7在Ubuntu安装[单机软件],第一步登录root,sudo su -
|
1月前
|
关系型数据库 MySQL 数据库
mysql 中登录报错:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)ERROR
mysql 中登录报错:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)ERROR
|
2月前
|
SQL 关系型数据库 MySQL
MySQL数据库——概述-MySQL的安装、启动与停止和客户端连接、关系型数据库(RDBMS)、数据模型
MySQL数据库——概述-MySQL的安装、启动与停止和客户端连接、关系型数据库(RDBMS)、数据模型
46 0
|
3月前
|
关系型数据库 MySQL 数据库
MySQL 启动 登录报错Job for mysqld.service failed because the control process exited with error code. See
MySQL 启动 登录报错Job for mysqld.service failed because the control process exited with error code. See
|
1天前
|
SQL 存储 关系型数据库
数据库-MySQL-01(一)
数据库-MySQL-01(一)
12 4