在命令提示符下怎样更改mysql的root管理员密码?
>mysql -u root -p Enter password: ****** mysql> use mysql; mysql> update user set password=password('new_password') where user='root';
如果是mysql5.x下, 通过这种方法就可以直接修改密码了。至于在CMD下能否登陆MySQL,就要在Windows环境变量PATH中添加“C:\Program Files\MySQL\MySQL Server 5.0\bin;”(请改为你自己的安装路径)了。
MYSQL8之前的版本, 修改root密码命令
cmd下切换到 MySQL 安装目录
例
d:/mysql/bin #前提:mysql用户root密码为空. >mysql -u root mysql mysql>update user set password=password('新密码') where user='root'; #回显 Query OK, 0 rows affected (0.00 sec) Rows matched: 2 Changed: 0 Warnings: 0 mysql>FLUSH PRIVILEGES; #回显 Query OK, 0 rows affected (0.00 sec) mysql>quit 退出 sql
注意每个命令后都要加上一个分号 ";"
mysql 才开始执行该行命令
而第二个指令会让已载入记忆体的 mysql 系统资料库更新
MySql 8.x版本下重置用户密码
新版mysql默认废除了password()函数, 改成了另外的玩法, 首先你要把user表里的authentication_string置空,然后通过alter语法修改密码。
update user set authentication_string='' where user='root'; mysql>ALTER user 'root'@'localhost' IDENTIFIED BY '新密码';
这个时候如果你是在安全模式强行重置密码的, 可能会遇到报错:
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
解决方法是使用下述语句刷一下权限再重置密码:
flush privileges; mysql>ALTER user 'root'@'localhost' IDENTIFIED BY '新密码';
如果语句仍然执行失败, 试试前边的'localhost'有可能是'%', 这是根据你当前对root账号的IP适配范围来决定的。
重启MySQL
# 停止MYSQL net stop mysql # 启动MYSQL服务 net start mysql