具体操作
在5.7之前修改密码:(使用update修改user表)
use mysql;
update user set authentication_string=password('123456') where host = 'localhost' and user='root';
在5.7修改密码:(废除了password字段,需要使用authentication_string)
use mysql;
update user set password=password('123456') where host='localhost' and user ='root'
在8.0中已经不能使用password函数和set…语句,只能使用
use mysql;
alter user 'root'@'localhost' identified with mysql_native_password by '123456';
## 如果遇报错,先执行flush privileges
原因
mysql5.7.9版本之后废弃了password字段和password()函数,且默认加密方式不采用mysql_native_password.
在mysql8.0以上版本中caching_sha2_password和sha256_password认证插件比mysql_native_password插件提供的密码加密更安全,并且前者加密性能更好。由于caching_sha2_password这样优秀的安全和性能特征,让他作为mysql8.0的默认首选认证插件,而并不是mysql_native_password.
所以mysql8.0默认是caching_sha2_password加密,5.7.9版本后的默认是mysql_native_password.