先停止 MySQL 服务,然后使用安全模式

1
sudo mysqld_safe --skip-grant-tables

新开一个终端

1
mysql -u root
1
2
3
4
UPDATE mysql.user 
SET authentication_string = PASSWORD('MyNewPass'), password_expired = 'N'
WHERE User = 'root' AND Host = 'localhost';
FLUSH PRIVILEGES;

参考链接: http://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html