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:

  1. Stop the server:
shell> sudo systemctl stop mysqldshell> pgrep mysqld
  1. Save the SQL code in /var/lib/mysql/mysql-init-password; make it readable to MySQL only:
shell> vi /var/lib/mysql/mysql-init-passwordALTER USER 'root'@'localhost' IDENTIFIED BY 'New$trongPass1';shell> sudo chmod 400 /var/lib/mysql/mysql-init-passwordshell> sudo chown mysql:mysql /var/lib/mysql/mysql-init-password
  1. Start the MySQL server with the --init-file option and other options ...

Get MySQL 8 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.