又忘记密码啦?教你几种更改密码的方式
前言:
在日常使用数据库的过程中,难免会遇到需要修改账号密码的情景,比如密码太简单需要修改、密码过期需要修改、忘记密码需要修改等。本篇文章将会介绍需要修改密码的场景及修改密码的几种方式。
1.忘记 root 密码
忘记 root 密码的场景还是比较常见的,特别是自己搭的测试环境经过好久没用过时,很容易记不得当时设置的密码。这个时候一般常用的方法是跳过权限验证,然后更改 root 密码,之后再启用权限验证。以 MySQL 5.7 版本为例简单讲下主要过程:
首先修改配置文件,在[mysqld]部分加上一句:skip-grant-tables ,加上此参数的目的是跳过权限验证。然后重启数据库,数据库再次启动后,我们就可以不用密码直接登录数据库修改密码了。
- # skip-grant-tables 模式下修改root密码
- [root@host ~]# mysql
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 16
- Server version: 5.7.23-log MySQL Community Server (GPL)
- Copyright (c) 2000, 2018, 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> update mysql.user set authentication_string = password ('xxxxxx') where user = 'root' and host = 'localhost';
- Query OK, 0 rows affected, 1 warning (0.00 sec)
- Rows matched: 1 Changed: 0 Warnings: 1
- mysql> flush privileges;
- Query OK, 0 rows affected (0.01 sec)
修改完 root 密码后,再次去除 skip-grant-tables 参数,然后重启下数据库即可。
2.几种修改密码的方法
除去忘记密码,可能还有其他情景需要修改密码,这时候就可以采取普通方式修改密码了。还是以 MySQL 5.7 版本为例,介绍几种常用的修改密码的方法。
使用 alter user 修改
比如如果想更改 testuser 账号的密码,我们可以使用 root 账号登录,然后执行 alter user 命令更改 testuser 账号的密码。
- mysql> alter user 'testuser'@'%' identified by 'Password1';
- Query OK, 0 rows affected (0.01 sec)
- mysql> flush privileges;
- Query OK, 0 rows affected (0.00 sec)
使用 SET PASSWORD 命令
使用 SET PASSWORD 修改密码命令格式为 SET PASSWORD FOR 'username'@'host' = PASSWORD('newpass'); 同样是使用 root 账号可修改其他账号的密码。
- mysql> SET PASSWORD FOR 'testuser'@'%' = PASSWORD('Password2');
- Query OK, 0 rows affected, 1 warning (0.00 sec)
- mysql> flush privileges;
- Query OK, 0 rows affected (0.00 sec)
使用 mysqladmin 修改密码
使用 mysqladmin 命令修改账号密码格式为 mysqladmin -u用户名 -p旧密码 password 新密码
- [root@host ~]# mysqladmin -utestuser -pPassword2 password Password3
- mysqladmin: [Warning] Using a password on the command line interface can be insecure.
- Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
- [root@host ~]# mysql -utestuser -pPassword3
- 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 2388
- Server version: 5.7.23-log MySQL Community Server (GPL)
- Copyright (c) 2000, 2018, 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>
直接 update user 表
其实 MySQL 所以的账号信息都存储在 mysql.user 表里面,我们也可以直接通过 update user 表来修改密码。
- # 5.7及之后版本
- mysql> update mysql.user set authentication_string = password ('Password4') where user = 'testuser' and host = '%';
- Query OK, 1 row affected, 1 warning (0.06 sec)
- Rows matched: 1 Changed: 1 Warnings: 1
- mysql> flush privileges;
- Query OK, 0 rows affected (0.01 sec)
- # 5.6及之前版本
- update mysql.user set password=password('新密码') where user='用户名' and host='host';
3.设置 login-path 本地快捷登陆
为了防止密码暴露及忘记密码,我们还可以设置 login-path 来实现在本地不输密码快捷登录。
login-path 是 MySQL 5.6 开始支持的新特性。通过借助 mysql_config_editor 工具将登陆 MySQL 服务的认证信息加密保存在 .mylogin.cnf 文件(默认位于用户主目录)。MySQL 客户端工具可通过读取该加密文件连接 MySQL ,实现快捷登录。
假设我们想配置 root 账号在本地快捷登录,可以这么做:
- # 执行回车后需要输入一次root密码
- [root@host ~]# mysql_config_editor set --login-path=root -uroot -hlocalhost -p -P3306
- Enter password:
- # 配置完成后可以使用login-path登录
- [root@host ~]# mysql --login-path=root
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 2919
- Server version: 5.7.23-log MySQL Community Server (GPL)
- Copyright (c) 2000, 2018, 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>
总结:
本篇文章主要介绍了修改数据库账号密码的几种方法,基本涵盖了所有的场景。这里也提醒下各位,数据库账号最好限制ip段登录,密码尽量复杂些,最好能够定期修改,特别是重要的环境不能有半点马虎。年底了,安全才是王道。
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
华为鸿蒙OS手机Beta版正式发布,跨端和入口有多大吸引力?
“做操作系统的技术不难,难的是做生态。” 华为消费者 BG 软件部总裁王成录在活动开场时感慨道。 2020 年 12 月 16 日上午,华为在北京举办了鸿蒙 OS 2.0(HarmonyOS 2.0)手机开发者 Beta 版本发布活动,尽管低调,甚至也没有官方直播,但作为国产操作系统的一个新希望,还是吸引了不少开发者的关注。 早在 2019 年 8 月的华为开发者大会(HDC)上,华为消费者业务首席执行官余承东曾正式发布 HarmonyOS ,内核为 Linux 内核、HarmonyOS 微内核和 LiteOS,未来将摆脱 Linux 内核和 LiteOS,当时余承东表示,如果 Google 的安卓系统不向华为开放,自家手机将可随时启用该系统。 2020 年 9 月的 HDC 上,HarmonyOS 2.0 发布,推出了面对智能电视、智能穿戴和车载系统的 Beta 版,而智能手机的 Beta 版本则会在 2020 年 12 月推出,今天的发布活动,在业界看来算是如期而至,节奏并没有耽搁。 正如王成录开场提到的那样,重新打造一个生态对于一款操作系统来说是艰难的,这需要时间和技术的积淀。如...
- 下一篇
企业级容器云平台的落地与实践
随着IT行业的发展和变迁,IT应用的底层支持也从大型机、小型机、PC服务器、虚拟化技术,到如今的容器化。基于敏捷开发的持续迭代,持续部署,以及多样化的技术栈,传统的底层架构变得越来越冗杂,运维管理越来越力不从心,运维人员也逐步陷落在无尽的“救火”运维模式。 容器技术的出现,从根本上改变了这一切。而容器高效的编排与管理,才是让其风光无限的前提条件。Kubernetes经过多年的发展,已经成为行业的事实标准。而如何利用好Kubernetes,为企业的发展助力,成为很多企业,尤其是初创企业无法忽略的一道难题。 本文将介绍利用Amazon EKS打造企业级容器云平台,通过一系列的实践操作,让大家直观地了解AWS是如何帮助企业高效地部署、管理容器化应用。 1. 传统应用架构的容器化之路 说到“传统”二字,大家第一反应,就是“落后”,“守旧”,然而现实情况却是----绝大部分传统应用,依然在良好地运行在这些传统架构中。这些被服务的对象不会因为某些因素对整个系统资源的需求发生很大变化,也不会有频繁的系统功能的迭代开发需求。 但是随着互联网、大数据、AI等技术的发展,各行各业都在努力地从信息化向智能化...
相关文章
文章评论
共有0条评论来说两句吧...