一、以安全模式登录

# Stop MySQL
sudo service mysql stop

# Make MySQL service directory.
sudo mkdir -p /var/run/mysqld

# Give MySQL user permission to write to the service directory.
sudo chown mysql:mysql /var/run/mysqld

# Start MySQL manually, without permission checks or networking.
sudo mysqld_safe --skip-grant-tables &

# Log in without a password.
mysql -u root

二、修改密码

UPDATE mysql.user SET authentication_string=PASSWORD('NEW-PASSWORD'), plugin='mysql_native_password' WHERE User='root' ;
EXIT;

三、重启数据库

# Turn off MySQL.
sudo mysqladmin -S /var/run/mysqld/mysqld.sock shutdown

# Start the MySQL service normally.
sudo service mysql start