又忘记密码啦?教你几种更改密码的方式

数据库
在日常使用数据库的过程中,难免会遇到需要修改账号密码的情景,比如密码太简单需要修改、密码过期需要修改、忘记密码需要修改等。本篇文章将会介绍需要修改密码的场景及修改密码的几种方式。

 前言:

在日常使用数据库的过程中,难免会遇到需要修改账号密码的情景,比如密码太简单需要修改、密码过期需要修改、忘记密码需要修改等。本篇文章将会介绍需要修改密码的场景及修改密码的几种方式。

[[358336]]

1.忘记 root 密码

忘记 root 密码的场景还是比较常见的,特别是自己搭的测试环境经过好久没用过时,很容易记不得当时设置的密码。这个时候一般常用的方法是跳过权限验证,然后更改 root 密码,之后再启用权限验证。以 MySQL 5.7 版本为例简单讲下主要过程:

首先修改配置文件,在[mysqld]部分加上一句:skip-grant-tables ,加上此参数的目的是跳过权限验证。然后重启数据库,数据库再次启动后,我们就可以不用密码直接登录数据库修改密码了。

 

  1. # skip-grant-tables 模式下修改root密码 
  2. [root@host ~]# mysql 
  3. Welcome to the MySQL monitor.  Commands end with ; or \g. 
  4. Your MySQL connection id is 16 
  5. Server version: 5.7.23-log MySQL Community Server (GPL) 
  6.  
  7. Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. 
  8.  
  9. Oracle is a registered trademark of Oracle Corporation and/or its 
  10. affiliates. Other names may be trademarks of their respective 
  11. owners. 
  12.  
  13. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 
  14.  
  15. mysql> update mysql.user set authentication_string = password ('xxxxxx'where user = 'root' and host = 'localhost'
  16. Query OK, 0 rows affected, 1 warning (0.00 sec) 
  17. Rows matched: 1  Changed: 0  Warnings: 1 
  18.  
  19. mysql> flush privileges
  20. Query OK, 0 rows affected (0.01 sec) 

修改完 root 密码后,再次去除 skip-grant-tables 参数,然后重启下数据库即可。

2.几种修改密码的方法

除去忘记密码,可能还有其他情景需要修改密码,这时候就可以采取普通方式修改密码了。还是以 MySQL 5.7 版本为例,介绍几种常用的修改密码的方法。

使用 alter user 修改

比如如果想更改 testuser 账号的密码,我们可以使用 root 账号登录,然后执行 alter user 命令更改 testuser 账号的密码。

 

  1. mysql> alter user 'testuser'@'%' identified by 'Password1'
  2. Query OK, 0 rows affected (0.01 sec) 
  3.  
  4. mysql> flush privileges
  5. Query OK, 0 rows affected (0.00 sec) 

使用 SET PASSWORD 命令

使用 SET PASSWORD 修改密码命令格式为 SET PASSWORD FOR 'username'@'host' = PASSWORD('newpass'); 同样是使用 root 账号可修改其他账号的密码。

 

  1. mysql> SET PASSWORD FOR 'testuser'@'%' = PASSWORD('Password2'); 
  2. Query OK, 0 rows affected, 1 warning (0.00 sec) 
  3.  
  4. mysql> flush privileges
  5. Query OK, 0 rows affected (0.00 sec) 

使用 mysqladmin 修改密码

使用 mysqladmin 命令修改账号密码格式为 mysqladmin -u用户名 -p旧密码 password 新密码

 

  1. [root@host ~]# mysqladmin -utestuser -pPassword2 password Password3 
  2. mysqladmin: [Warning] Using a password on the command line interface can be insecure. 
  3. Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety. 
  4. [root@host ~]# mysql -utestuser -pPassword3 
  5. mysql: [Warning] Using a password on the command line interface can be insecure. 
  6. Welcome to the MySQL monitor.  Commands end with ; or \g. 
  7. Your MySQL connection id is 2388 
  8. Server version: 5.7.23-log MySQL Community Server (GPL) 
  9.  
  10. Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. 
  11.  
  12. Oracle is a registered trademark of Oracle Corporation and/or its 
  13. affiliates. Other names may be trademarks of their respective 
  14. owners. 
  15.  
  16. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 
  17.  
  18. mysql>  

直接 update user 表

其实 MySQL 所以的账号信息都存储在 mysql.user 表里面,我们也可以直接通过 update user 表来修改密码。

 

  1. # 5.7及之后版本 
  2. mysql> update mysql.user set authentication_string = password ('Password4'where user = 'testuser' and host = '%'
  3. Query OK, 1 row affected, 1 warning (0.06 sec) 
  4. Rows matched: 1  Changed: 1  Warnings: 1 
  5.  
  6. mysql> flush privileges
  7. Query OK, 0 rows affected (0.01 sec) 
  8.  
  9. # 5.6及之前版本 
  10. 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 账号在本地快捷登录,可以这么做:

 

  1. # 执行回车后需要输入一次root密码 
  2. [root@host ~]# mysql_config_editor set --login-path=root -uroot  -hlocalhost -p -P3306  
  3. Enter password:  
  4.  
  5. # 配置完成后可以使用login-path登录 
  6. [root@host ~]# mysql --login-path=root 
  7. Welcome to the MySQL monitor.  Commands end with ; or \g. 
  8. Your MySQL connection id is 2919 
  9. Server version: 5.7.23-log MySQL Community Server (GPL) 
  10.  
  11. Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. 
  12.  
  13. Oracle is a registered trademark of Oracle Corporation and/or its 
  14. affiliates. Other names may be trademarks of their respective 
  15. owners. 
  16.  
  17. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 
  18.  
  19. mysql>  

总结:

本篇文章主要介绍了修改数据库账号密码的几种方法,基本涵盖了所有的场景。这里也提醒下各位,数据库账号最好限制ip段登录,密码尽量复杂些,最好能够定期修改,特别是重要的环境不能有半点马虎。年底了,安全才是王道。

责任编辑:华轩 来源: MySQL技术
相关推荐

2011-07-13 17:28:53

sa密码SQL Server

2010-06-10 15:44:53

2010-05-26 17:21:14

MySQL root密

2010-06-13 13:10:09

MySQLROOT密码

2020-10-20 08:01:30

MySQL密码Windows

2015-05-26 08:46:58

密码LaZagne Pro密码检索

2017-04-13 12:20:43

Mysqlroot密码

2010-06-07 17:09:20

MySQLroot密码

2010-04-20 16:46:41

Oracle数据库密码

2010-06-04 17:11:07

MySQLroot密码

2010-06-04 16:03:37

MySQL root密

2010-10-14 10:09:33

MySQL root用

2010-05-25 16:37:47

MySQL忘记密码

2011-06-27 09:23:05

Ubuntu密码

2010-04-23 13:53:29

Oracle密码

2010-06-04 19:49:22

cnblogs

2010-05-13 18:18:58

MySQL root

2011-05-19 09:58:26

2010-04-20 08:56:53

2009-06-27 14:11:17

点赞
收藏

51CTO技术栈公众号