使用客户端证书登录MySQL

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介: 生成证书使用openssl req创建X.509证书,下面的命令创建有效期10年的私钥,使用man req可以查询这个命令的帮助。


01

生成证书


使用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.pemGenerating 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 incorporatedinto 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 blankFor some fields there will be a default value,If you enter '.', the field will be left blank.-----Country Name (2 letter code) [XX]:CNState 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' attributesto be sent with your certificate requestA 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.pemSignature oksubject=/C=CN/L=Default City/O=Default Company LtdGetting 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: OKyao1-cert.pem: OK

02

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


相应命令如下:




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组件后才能创建无密码的账号。

03

使用证书登录mysql和mysqlsh


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





oracle@yao mysql$  mysql -uyao1 --ssl-cert yao1-cert.pem --ssl-key yao1-key.pem -p --ssl_ca=/u01/mysql/ca.pemEnter password:Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 3685Server 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 itsaffiliates. Other names may be trademarks of their respectiveowners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
You are enforcing ssl connection via unix socket. Please considerswitching ssl off as it does not make connection via unix socketany more secure.mysql> \s--------------mysql  Ver 8.0.31 for Linux on x86_64 (MySQL Community Server - GPL)
Connection id:          3685Current database:Current user:           yao1@localhostSSL:                    Cipher in use is ECDHE-RSA-AES128-GCM-SHA256Current pager:          stdoutUsing outfile:          ''Using delimiter:        ;Server version:         8.0.31 MySQL Community Server - GPLProtocol version:       10Connection:             Localhost via UNIX socketServer characterset:    utf8mb4Db     characterset:    utf8mb4Client characterset:    utf8mb4Conn.  characterset:    utf8mb4UNIX socket:            /u01/mysql/mysql.sockBinary data as:         HexadecimalUptime:                 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.pemPlease 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 3686Server version: 8.0.31 MySQL Community Server - GPLNo default schema selected; type \use <schema> to set one. MySQL  127.0.0.1:3306 ssl  JS >


相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
2月前
|
SQL 关系型数据库 MySQL
【MySQL】— —熟练掌握用SQL语句实现数据库和基本表的创建。熟练掌握MySQL的安装、客户端登录方法;熟练掌握MySQL的编码、数据类型等基础知识;掌握实体完整性的定义和维护方法、掌握参照完整性
【MySQL】— —熟练掌握用SQL语句实现数据库和基本表的创建。熟练掌握MySQL的安装、客户端登录方法;熟练掌握MySQL的编码、数据类型等基础知识;掌握实体完整性的定义和维护方法、掌握参照完整性
110 1
|
2月前
|
关系型数据库 MySQL 数据库
rds安装数据库客户端工具
安装阿里云RDS的数据库客户端涉及在本地安装对应类型(如MySQL、PostgreSQL)的客户端工具。对于MySQL,可选择MySQL Command-Line Client或图形化工具如Navicat,安装后输入RDS实例的连接参数进行连接。对于PostgreSQL,可以使用`psql`命令行工具或图形化客户端如PgAdmin。首先从阿里云控制台获取连接信息,然后按照官方文档安装客户端,最后配置客户端连接以确保遵循安全指引。
113 1
|
7天前
|
关系型数据库 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
|
14天前
|
NoSQL 关系型数据库 Redis
数据管理DMS产品使用合集之要通过 DMS 登录到 RDS、DRDS 或 Redis,我该怎么操作
阿里云数据管理DMS提供了全面的数据管理、数据库运维、数据安全、数据迁移与同步等功能,助力企业高效、安全地进行数据库管理和运维工作。以下是DMS产品使用合集的详细介绍。
|
17天前
|
安全 关系型数据库 MySQL
node实战——后端koa结合jwt连接mysql实现权限登录(node后端就业储备知识)
node实战——后端koa结合jwt连接mysql实现权限登录(node后端就业储备知识)
23 3
|
28天前
|
PHP
web简易开发——通过php与HTML+css+mysql实现用户的登录,注册
web简易开发——通过php与HTML+css+mysql实现用户的登录,注册
|
2月前
|
关系型数据库 MySQL Linux
MySQL启动与登录命令详解
【2月更文挑战第27天】
125 1
MySQL启动与登录命令详解
|
2月前
|
缓存 关系型数据库 MySQL
MySQL登录时出现Access denied for user ‘root‘@‘localhost‘ (using password: YES)无法打开的解决方法
MySQL登录时出现Access denied for user ‘root‘@‘localhost‘ (using password: YES)无法打开的解决方法
|
4月前
|
SQL 关系型数据库 MySQL
免费MySQL数据库客户端推荐
免费MySQL数据库客户端推荐
76 0
|
4月前
|
SQL 关系型数据库 MySQL
MySQL数据库免费客户端简介
MySQL数据库免费客户端简介
42 0

推荐镜像

更多