Resetting the root password
If you forget the root password, you can reset it by two methods, explained as follows.
How to do it...
Let's get into the details.
Using init-file
On Unix-like systems, you stop the server and start it by specifying init-file. You can save the ALTER USER 'root'@'localhost' IDENTIFIED BY 'New$trongPass1'
SQL code in that file. MySQL executes the contents of the file at startup, changing the password of the root user:
- Stop the server:
shell> sudo systemctl stop mysqld shell> pgrep mysqld
- Save the SQL code in
/var/lib/mysql/mysql-init-password
; make it readable to MySQL only:
shell> vi /var/lib/mysql/mysql-init-password ALTER USER 'root'@'localhost' IDENTIFIED BY 'New$trongPass1'; shell> sudo chmod 400 /var/lib/mysql/mysql-init-password shell> sudo chown mysql:mysql /var/lib/mysql/mysql-init-password
- Start the MySQL server with the
--init-file
option and other options as required:
shell> sudo -u mysql /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld...