当前位置:首页 > 实用技巧 >

忘记了密码如何重新设置密码(密码忘记了如何重新设置密码)

来源:原点资讯(www.yd166.com)时间:2023-10-31 10:15:20作者:YD166手机阅读>>

前言:

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

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段登录,密码尽量复杂些,最好能够定期修改,特别是重要的环境不能有半点马虎。年底了,安全才是王道。

忘记了密码如何重新设置密码,密码忘记了如何重新设置密码(1)

栏目热文

可以自做食品到淘宝里卖吗(在淘宝上出售自制食品需要什么)

可以自做食品到淘宝里卖吗(在淘宝上出售自制食品需要什么)

目前在网上卖东西的人挺多的,尤其是食品这一块。那么,在淘宝开零食店需要食品经营许可证吗?食品经营许可证怎么办理?不办食品...

2023-10-31 10:18:55查看全文 >>

如何在淘宝上卖自制食品(在淘宝上出售自制食品需要什么)

如何在淘宝上卖自制食品(在淘宝上出售自制食品需要什么)

近日,非遗工坊•山东手造精品专区在好客山东淘宝、京东旗舰店上线。专区汇聚全省200余家非遗工坊产品近600款,涵盖纺染织...

2023-10-31 10:40:39查看全文 >>

淘宝网上卖自制食品需要什么手续(淘宝卖自制食品需要什么证)

淘宝网上卖自制食品需要什么手续(淘宝卖自制食品需要什么证)

自制食品被指“三无产品”,遭买家十倍索赔!法院如何判决?近日碑林法院审结一起网络购物引起的合同纠纷自制食品售卖,被指三无...

2023-10-31 10:18:09查看全文 >>

淘宝卖自制食品需要什么证(淘宝卖手工食品需要证件吗)

淘宝卖自制食品需要什么证(淘宝卖手工食品需要证件吗)

食品经营许可证1、三证合一:食品流通许可证、餐饮服务许可证、食品卫生许可证现合并为食品经营许可证2、食品经营许可证分类:...

2023-10-31 10:30:15查看全文 >>

在淘宝上卖自制食品需要什么手续(淘宝卖自制食品需要什么条件)

在淘宝上卖自制食品需要什么手续(淘宝卖自制食品需要什么条件)

开个店需要办理哪些证件?如果你要开一家收入好的小超市都需要哪些证件呢?开店之前首先要选择一个好的地理位置,看看房租是否合...

2023-10-31 10:20:11查看全文 >>

忘记密码怎么重置密码(如何忘记了密码但不用重置密码)

忘记密码怎么重置密码(如何忘记了密码但不用重置密码)

相信大家都注意到这么一个细节,手机每次重启后,或超过一定时间未使用传统密码解锁(图案、数字、混合密码),系统会强制用户通...

2023-10-31 10:39:37查看全文 >>

忘记密码如何重新设置密码(忘记密码的情况下如何重置密码)

忘记密码如何重新设置密码(忘记密码的情况下如何重置密码)

最近有小伙伴咨询:居家办税使用电子税务局时,登录密码/申报密码忘记,怎么办?别着急,跟随申税小微一起看看如何解决吧!01...

2023-10-31 10:21:55查看全文 >>

忘记密码问题怎么重置(忘记密码怎么办如何重置密码)

忘记密码问题怎么重置(忘记密码怎么办如何重置密码)

忘记密码怎么办?如果你用的是Windows 10系统,一不小心忘记了系统密码,那应该怎么办呢?这要分2种情况Window...

2023-10-31 10:49:48查看全文 >>

忘了密码怎么重置密码(忘记密码的情况下如何重置密码)

忘了密码怎么重置密码(忘记密码的情况下如何重置密码)

[手机屏幕锁密码忘记了怎么办]如果您使用的是华为手机的话,您可进行如下操作:场景一:Magic UI 2.X & EMU...

2023-10-31 10:35:06查看全文 >>

怎样才能修改忘记密码的问题(怎么修改忘记密码后的问题设置)

怎样才能修改忘记密码的问题(怎么修改忘记密码后的问题设置)

大家好,我是璐璐,一个喜欢拖着心爱的拉杆箱去诗和远方的妹纸!好多的小伙伴,都发来问题问我:好多小伙伴真的很马虎哦!为了帮...

2023-10-31 10:28:17查看全文 >>

文档排行