You may need to change the password when using the MySQL database. What are the common methods for changing the MySQL database? For MySQL 5.7.6 and later, you are advised to run the ALTER USER command to change the password. Log in to the MySQL server:
mysql -u root -p
Enter the current root password. Change password:
ALTER USER 'username'@'host' IDENTIFIED BY 'new_password';
username is the username you want to change the password for, host is the host name (usually localhost, or it can be a specific IP address), and new_password is the new password you want to set.
Update permissions:
FLUSH PRIVILEGES;
In MySQL 5.7.5 and earlier, you can also log in to the MySQL server with the SET PASSWORD command:
mysql -u root -p
Change password:
SET PASSWORD FOR 'username'@'host' = PASSWORD('new_password');
or
SET PASSWORD FOR 'username'@'host' = '* Encrypted password ';
Update:
FLUSH PRIVILEGES;
mysqladmin can also be used:
mysqladmin -u root -p password new_password
After you enter the current root password, the system prompts you to enter a new password.
Also, you can set in the my.cnf configuration file, edit the mysql configuration file my.cnf is generally located in the /etc/mysql./ directory, add:
validate_user_plugins=1
validate_user_plugin=mysql_native_password
To restart MySQL:
sudo service mysql restart
or
sudo systemctl restart mysql
After changing the password, ensure that all application/database connections are updated to use the new password. Changing the MySQL service requires certain permissions, and you need to learn how to restore/reset the password to prevent unexpected events.
If you forget the root password for MySQL, you can use safe mode. To stop the MySQL service:
sudo systemctl stop mysql
or
sudo service mysql stop
Start MySQL services in safe mode, allowing empty password login:
sudo mysqld_safe --skip-grant-tables &
Log in to MySQL:
mysql -u root
Select MySQL database:
USE mysql;
Update the permission table to allow root to log in from any host and set a new password:
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password' WITH GRANT OPTION;
Exit:
Quit
To exit the MySQL service in safe mode:
sudo killall mysqld
To restart the MySQL service:
sudo systemctl start mysql
or
sudo service mysql start