有哪些更改数据库密码的方式

介绍

这篇文章主要讲解了”有哪些更改数据库密码的方式”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习”有哪些更改数据库密码的方式”吧!

<强> 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版本为例,介绍几种常用的修改密码的方法。

<强>使用改变用户修改

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

 mysql>, alter  user  & # 39; testuser& # 39; @ # 39; % & # 39;, identified  by  & # 39;密码# 39;;,Query 好吧,,0,rows  affected (0.01,秒),,mysql>, flush 特权;,Query 好吧,,0,rows  affected  (0.00, sec) 

<强>使用设置密码命令

使用设置密码修改密码命令格式为& # 39;设置密码用户名# 39;@ # 39;主机# 39;=,密码(& # 39;newpass& # 39;);同样是使用 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>

有哪些更改数据库密码的方式