方法1: 用SET PASSWORD命令
首先登录MySQL。
例子:mysql> set password for root@localhost = password('123');
方法2:用mysqladmin
例子:mysqladmin -uroot -p123456 password 123
方法3:用UPDATE直接编辑user表
对于5.7以下版本:
mysql> use mysql;
mysql> update user set password=password('123') where user='root' and host='localhost';
mysql> flush privileges;
对于5.7及以上版本:
mysql> use mysql;
mysql> update user set authentication_string=password('123') where user='root' and host='localhost';
mysql> flush privileges;
方法4:忘记root密码
my.cnf [mysqld] 添加skip-grant-tables 参数,然后重启。
重置完密码后记得把参数去掉。