DEV Community

NDONGO TONUX SAMB
NDONGO TONUX SAMB

Posted on

Change mysql root password on Centos7

Stop Mysql

  systemctl stop mysqld
Enter fullscreen mode Exit fullscreen mode

Set Mysql environment option

  systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"
Enter fullscreen mode Exit fullscreen mode

Start Mysql

  systemctl start mysqld
Enter fullscreen mode Exit fullscreen mode

Login to Mysql as root

  mysql -u root
Enter fullscreen mode Exit fullscreen mode

Update the root user password

  mysql> UPDATE mysql.user SET authentication_string = PASSWORD('MyPassword')
  -> WHERE User = 'root' AND Host = 'localhost';
  mysql> FLUSH PRIVILEGES;
  mysql> quit
Enter fullscreen mode Exit fullscreen mode

Stop mysql

  systemctl stop mysqld
Enter fullscreen mode Exit fullscreen mode

Unset the mySQL envitroment option so it starts normally next time

  systemctl unset-environment MYSQLD_OPTS
Enter fullscreen mode Exit fullscreen mode

Start mysql

  systemctl start mysqld
Enter fullscreen mode Exit fullscreen mode

Login to mysql again using your new password:

mysql -u root -p

NB : for 5.7.6 and later, you should use
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';

Top comments (0)