在命令提示符下怎樣更改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