进入mysqld.cnf文件在mysqld下添加skip-grant-tables
> vim /etc/mysql/mysql.conf.d/mysqld.cnf
保存退出重启mysql服务
> sudo service mysql restart;
登陆mysql注意在输入密码是直接回车进入mysql
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'newpassword'; # newpassword:你的新密码
退出mysql 去刚刚的mysqld.cnf把skip-grant-tables注释掉重启mysql服务
sudo service mysql restart;