mysql 更改密码 alter_MySQL修改账号密码方法大全「建议收藏」

mysql 更改密码 alter_MySQL修改账号密码方法大全「建议收藏」前言:在日常使用数据库的过程中,难免会遇到需要修改账号密码的情景,比如密码太简单需要修改、密码过期需要修改、忘记密码需要修改等。本篇文章将会介绍需要修改密码的场景及修改密码的几种方式。1.忘记root密码忘记root密码的场景还是比较常见的,特别是自己搭的测试环境经过好久没用过时,很容易记不得当时设置的密码。这个时候一般常用的方法是跳过权限验证,然后更改root密码,之后再启用权限验证…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

前言:

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/170171.html原文链接:https://javaforall.net

(0)
上一篇 2022年8月12日 下午11:36
下一篇 2022年8月12日 下午11:46


相关推荐

  • 常用的perl正则表达式

    常用的perl正则表达式^\d+$  //匹配非负整数(正整数+0) //匹配整数^\d+(\.\d+)?$  //匹配非负浮点数(正浮点数+0) ^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$  //匹配正浮点数 ^((-\d+(\.\d+)?)|(0+(\.0+)?))$  //匹配

    2022年5月30日
    35
  • SWOT分析和PEST分析[通俗易懂]

    SWOT分析和PEST分析[通俗易懂]1.SWOT分析定义:SWOT(StrengthsWeaknessOpportunityThreats)分析法,又称态势分析法或优劣势分析法,用来确定企业自身的竞争优势(strength)、竞争劣势(weakness)、机会(opportunity)和威胁(threat),从而将公司的战略与公司内部资源、外部环境有机地结合起来。如何用:首先要明白使用SWOT的目的,为什么要用它,用它的目的

    2022年6月14日
    66
  • iframe参数详解

    iframe参数详解iframe参数详解iframe runat=”server” src=”youpage’surl”width=”750″height=”30″frameborder=”no”border=”0″marginwidth=”0″marginheight=”0″scrolling=”no”allowtransparency=”yes”>i

    2022年7月12日
    35
  • RFC2616-HTTP1.1-Header Field Definitions(头字段规定部分—单词注释版)[通俗易懂]

    partof HypertextTransferProtocol–HTTP/1.1RFC2616Fielding,etal.14 HeaderFieldD

    2022年3月25日
    38
  • 猫眼java开发暑期实习_实习生面试–猫眼娱乐

    猫眼java开发暑期实习_实习生面试–猫眼娱乐2020 6 16 下午 3 点上来写个 python 读取 txt 文件中的单词 单词以空格分开 并统计单词的个数我看你的简历里有 softmax 损失 可以说说 softmax 与 sigmoid 的原理和应用场景吗 正确答案 softmax 用于多分类问题 sigmoid 用于二分类问题 softmax 是求 e 的输出值次方 并对输出归一化 使得最终的和为 1 sigmoid 只是将输出值映射到 0 1 之间我对你这个直走机器

    2025年10月26日
    5
  • Pycharm设置manage repositories多个来源

    Pycharm设置manage repositories多个来源Pycharm 设置 managereposi 多个来源或换安装源在 settings gt Project test1 gt PythonInterp gt 第一个 原路径 https pypi python org simple 第二个 清华安装源 https pypi tuna tsinghua edu cn simple 第三个 阿里安装源 https mirrors aliyun com pypi simple

    2026年3月27日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号