1、以安全模式登陸
# 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
2、修改密碼
UPDATE mysql.user SET authentication_string=PASSWORD('NEW-PASSWORD'), plugin='mysql_native_password' WHERE User='root' ;
EXIT;
3、重啓數據庫
# Turn off MySQL.
sudo mysqladmin -S /var/run/mysqld/mysqld.sock shutdown
# Start the MySQL service normally.
sudo service mysql start