MySQL5.7和之前的用戶修改密碼方式:
mysql -uroot -e "Set password=password(‘123’);" mysql -uroot -p123.com -e "use mysql;update user set authentication_string=password('456') where user='root';" update mysql.user set authentication_string=password("123") where user='root';
以上三種方法在MySQL8.0以後版本中將不能使用,如果使用了將會導致在正確修改密碼是報如下錯誤:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123'; ERROR 1396 (HY000): Operation ALTER USER failed for 'root'@'localhost'
如遇上以上問題請使用update語句先清空authentication_string字段,然後再修改密碼即可
update user set authentication_string='' where user='root'; ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '你的密碼';
所以特別提醒童鞋們:
MySQL8.0後請使用alter修改用戶密碼,因爲在MySQL8.0以後的加密方式爲caching_sha2_password,如果使用update修改密碼會給user表中root用戶的authentication_string字段下設置newpassowrd值,當再使用alter user 'root'@'localhost' identified by 'newpassword'修改密碼時會一直報錯,必須清空後再修改,因爲authentication_string字段下只能是MySQL加密後的43位字符串密碼,其他的會報格式錯誤,所以在MySQL8.0以後能修改密碼的方法只能是:ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '你的密碼';