Смена пароля root в MySQL 8

Смена пароля root в MySQL 8 выполняется при добавлении в конфигурационный файл директивы skip-grant-tables.

Директива добавляется в файл /etc/mysql/my.cnf или в другой файл определяющий блок [mysqld]

Далее служба перезапускается

systemctl restart mysql


Появляется возможность попасть в базу без пароля

Далее задается пустой пароль

mysql
mysql> update mysql.user set authentication_string=(») where user='root';
mysql> FLUSH PRIVILEGES;

Убираем skip-grant-tables. Вновь перезапускаем службу

systemctl restart mysql

Теперь можно в нормальном режиме зайти без пароля

mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.27-0ubuntu0.20.04.1 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Задаем пароль

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'VM2fiok8jHlxjfvfjkdQ5qIDRc';
Query OK, 0 rows affected (0.02 sec)

mysql> FLUSH PRIVILEGES;

Готово.