First, stop the database server.
If you use MySQL, type the following command and hit ENTER key.
$ sudo systemctl stop mysql
For MariaDB:
$ sudo systemctl stop mariadb
Next, restart the database server without permission-checking using the following command:
$ sudo mysqld_safe --skip-grant-tables &
Next, login to your MySQL/MariaDB server as root user:
$ mysql
At the mysql> or MariaDB [(none)]> prompt, run the following command to reset the root user password:
UPDATE mysql.user SET Password=PASSWORD('password') WHERE User='root';
Replace password in the above command with your own password.
Then, type following commands to exit from the mysql console.
FLUSH PRIVILEGES;
exit
Finally, shutdown the running database server that you started earlier with --skip-grant-tables
option. To do so, run:
$ sudo mysqladmin -u root -p shutdown
You will be asked to enter your mysql/mariadb root user password that you set in the previous step.
Enter password: [2]- Done sudo mysqld_safe --skip-grant-tables
Now, start mysql/mariadb service normally using command:
$ sudo systemctl start mysql
For MariaDB:
$ sudo systemctl start mariadb
Verify if the password has really been changed using the following command:
$ sudo mysql -u root -p
Chúc các bạn thành công!
Cảm ơn bạn đã trả lời.