Восстановление пароля root в MySQL

password repair mysql

Как сменить root пароль MySQL

  1. Останавливаем службу MySQL:

/etc/init.d/mysql stop

2.  Запускаем службу с опцией --skip-grant-tables

mysqld_safe --skip-grant-tables &

  1. Подключаемся с серверу MySQL при помощи клиента mysql:

mysql -u root

  1. Вводим новый пароль для root:

mysql> use mysql;

mysql> update user set password=PASSWORD"NEW\-ROOT\-PASSWORD" where User='root';

mysql> flush privileges;

В Centos 7 можно посмотреть автоматически сгенерированный пароль так:

sudo grep 'temporary password' /var/log/mysqld.log

2019-05-28T13:36:33.142558Z 1 [Note] A temporary password is generated for root@localhost: z,9zk\<x:A0*!

Сброс пароля Centos 7 отличается от традиционного:

https://stackoverflow.com/questions/33510184/change-mysql-root-password-on-centos7

  1. Stop mysql:

systemctl stop mysqld

  1. Set the mySQL environment option

systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"

  1. Start mysql usig the options you just set

systemctl start mysqld

  1. Login as root

mysql -u root

  1. Update the root user password with these mysql commands

mysql> UPDATE mysql.user SET authentication_string = PASSWORD'MyNewPassword' WHERE User = 'root' AND Host = 'localhost';

mysql> FLUSH PRIVILEGES;

mysql> quit

*** Edit ***As mentioned my shokulei in the comments, for 5.7.6 and later, you should use

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';

  1. Stop mysql

systemctl stop mysqld

  1. Unset the mySQL envitroment option so it starts normally next time

systemctl unset-environment MYSQLD_OPTS

  1. Start mysql normally:

systemctl start mysqld

Try to login using your new password:7. mysql -u root -p