服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|

服务器之家 - 数据库 - Mysql - MySQL修改账号密码方法大全(小结)

MySQL修改账号密码方法大全(小结)

2021-03-22 21:27kunjian Mysql

这篇文章主要介绍了MySQL修改账号密码方法大全(小结),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

前言:

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

1.忘记 root 密码

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 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 账号的密码。

?
1
2
3
4
5
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 账号可修改其他账号的密码。

?
1
2
3
4
5
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 新密码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[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 表来修改密码。

?
1
2
3
4
5
6
7
8
9
10
# 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 账号在本地快捷登录,可以这么做:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 执行回车后需要输入一次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段登录,密码尽量复杂些,最好能够定期修改,特别是重要的环境不能有半点马虎。年底了,安全才是王道。

到此这篇关于MySQL修改账号密码方法大全(小结)的文章就介绍到这了,更多相关MySQL修改账号密码内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://segmentfault.com/a/1190000038511566

延伸 · 阅读

精彩推荐
  • MysqlMysql 查询数据库容量大小的方法步骤

    Mysql 查询数据库容量大小的方法步骤

    这篇文章主要介绍了Mysql 查询数据库容量大小的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋...

    Jevic7262021-01-21
  • MysqlWindows系统下MySQL无法启动的万能解决方法

    Windows系统下MySQL无法启动的万能解决方法

    这篇文章主要给大家介绍了关于Windows系统下MySQL无法启动的万能解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习...

    从删库到跑路11122021-03-16
  • MysqlMySQL性能分析及explain的使用说明

    MySQL性能分析及explain的使用说明

    本文我们主要介绍了MySQL性能分析以及explain的使用,包括:组合索引、慢查询分析、MYISAM和INNODB的锁定、MYSQL的事务配置项等,希望能够对您有所帮助。 ...

    MYSQL教程网4212019-11-23
  • Mysql清空mysql 查询缓存的可行方法

    清空mysql 查询缓存的可行方法

    mysql对同一条sql进行了缓存,在第二次运行时, 瞬间就完成了,若要清除缓存,可通过下面的方法来实现 ...

    whsnow5392020-04-14
  • MysqlMySQL数据库使用mysqldump导出数据详解

    MySQL数据库使用mysqldump导出数据详解

    mysqldump是mysql用于转存储数据库的实用程序。它主要产生一个SQL脚本,其中包含从头重新创建数据库所必需的命令CREATE TABLE INSERT等。接下来通过本文给大家...

    pursuer.chen3352020-06-03
  • Mysqlmysql清除log-bin日志的方法

    mysql清除log-bin日志的方法

    这篇文章主要介绍了mysql清除log-bin日志的方法,同时介绍了log-bin日志的作用,需要的朋友可以参考下 ...

    junjie3912020-04-06
  • Mysqlmysql-8.0.15-winx64 使用zip包进行安装及服务启动后立即关闭问题

    mysql-8.0.15-winx64 使用zip包进行安装及服务启动后立即关闭问题

    这篇文章主要介绍了mysql 使用zip包进行安装以及服务启动后立即关闭问题 ,本实例使用的mysql版本为mysql-8.0.15-winx64,需要的朋友可以参考下...

    cj杨3552020-09-23
  • MysqlMySQL 子查询和分组查询

    MySQL 子查询和分组查询

    这篇文章主要介绍了MySQL 子查询和分组查询的相关资料,帮助大家更好的理解MySQL查询的相关知识,感兴趣的朋友可以了解下...

    翁智华5722021-03-07