mysql修改密码


在mysql使用过程中,我们可能经常会对mysql的密码进行修改,以下整理几种修改密码的方式

一、修改密码

注意:以下整理了几种修改密码的方式,由于mysql版本不同,所以有些方式可能不适用,可以换种方式执行。

如果您使用的是旧版本的MySQL,可能需要使用SET PASSWORD语句来修改密码。但是,在MySQL 8中,该语句已被弃用,而且在未来的版本中可能会被删除。因此,建议使用ALTER USER语句来修改密码。

方式一:用SET PASSWORD命令

注意:此种方式需要先登录一个账号,然后才能修改

首先登录MySQL。
格式:mysql> set password for '用户名'@'localhost' = password('新密码');
例子:mysql> set password for 'root'@'localhost' = password('123');

示例:

D:\App\mysql\MySQL Server 8.0\Files\bin>mysql -uroot -p1234

mysql> set password for 'root'@'localhost' = password('123');

如果这个命令执行后不能修改可能存在两种情况:

  1. mysql版本是8或8以上,对于mysql8来说set password命令已经过时了,所以执行不成功。
  2. 需要将localhost换成%,原因可参考下面【修改密码可能遇到的问题】章节

方式二:用mysqladmin

这种方式可以不登录账号即可执行,一般安装完mysql后在bin目录下就有mysqladmin命令, windows下需要在mysql安装目录 bin目录下打开cmd窗口,因为只有那里有mysqladmin.exe文件

格式:mysqladmin -u用户名 -p旧密码 password 新密码
例子:mysqladmin -uroot -p123456 password 123

示例:

D:\App\mysql\MySQL Server 8.0\Files\bin>mysqladmin -uroot -p1234 password 4321
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.

D:\App\mysql\MySQL Server 8.0\Files\bin>mysql -uroot -p4321
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 62
Server version: 8.0.34 MySQL Community Server - GPL

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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>

在这里插入图片描述

方式三:使用alter user语句

mysql8.0开始方式一SET PASSWORD语句可能就失效了,需要用这种方式

步骤一:先登录mysql
步骤二:ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';

其中,localhost为主机名,如果要修改远程用户的密码,需要将localhost替换为远程IP地址或%

示例:

D:\App\mysql\MySQL Server 8.0\Files\bin>mysql -uroot -p1234
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 65
Server version: 8.0.34 MySQL Community Server - GPL

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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> ALTER USER 'root'@'localhost' IDENTIFIED BY '4321';
Query OK, 0 rows affected (0.01 sec)

mysql>

二、修改密码可能遇到的问题

ERROR 1396 (HY000): Operation ALTER USER

报错如下:

mysql> ALTER USER 'root'@'%' IDENTIFIED BY '4321';
ERROR 1396 (HY000): Operation ALTER USER failed for 'root'@'%'
mysql>

报错原因:
这种情况一般是@后面的’%'造成的,可以改成localhost试试

root>mysql -uroot -p
mysql> use mysql;
mysql> select user,host from user;

+------+-----------+

| user | host |

+------+-----------+

| root | localhost |

| abc  |     %     |

| admin | %        |

+------+-----------+

注意我的 root,host是’localhost’, 所以将上面的语句修改一下即可。

ERROR 1064 (42000)

报错如下:

mysql> set password for root@localhost = password('1234');
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'password('1234')' at line 1

报错原因:
我在mysql8.0中执行了set password命令,但是mysql8.0这个命令已经过时了,所以导致了这个报错,换成alter user方式修改密码即可。

  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值