怎样修改密码(又忘记密码啦?)

怎样修改密码(又忘记密码啦?)

如何更改密码(又忘记密码了?)

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

1.忘记根密码

忘记root密码的场景还是比较常见的,尤其是自己搭建的测试环境长时间不用的时候,很容易忘记当时设置的密码。这时常用的方法是跳过权限验证,然后更改root密码,再启用权限验证。以MySQL版为例简单描述一下主要流程:

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

# skip-grant-tables 模式下修改root密码[root@host ~]# mysqlWelcome to the MySQL monitor.  Commands end with ; or \\g.Your MySQL connection id is 16Server 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 itsaffiliates. Other names may be trademarks of their respectiveowners.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: 1mysql> flush privileges;Query OK, 0 rows affected (0.01 sec)

修改root密码后,再次删除skip-grant-tables参数,然后重新启动数据库。

2.更改密码的几种方法

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

使用更改用户来修改

例如,如果您想更改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 for \' username \' @ \' host \' = PASSWORD(\' new pass \');此外,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 username -p旧密码密码新密码。

[root@host ~]# mysqladmin -utestuser -pPassword2 password Password3mysqladmin: [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 -pPassword3mysql: [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 2388Server 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 itsaffiliates. Other names may be trademarks of their respectiveowners.Type \'help;\' or \'\\h\' for help. Type \'\\c\' to clear the current input statement.mysql>

直接更新用户表

其实MySQL的所有账号信息都存储在mysql.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

以上就是由优质生活领域创作者 嘉文社百科网小编 整理编辑的,如果觉得有帮助欢迎收藏转发~