Skip to content

Instantly share code, notes, and snippets.

@faizalmansor
Last active September 16, 2020 07:36
Show Gist options
  • Save faizalmansor/3afb344b739a63bce84e39ade7cfe76d to your computer and use it in GitHub Desktop.
Save faizalmansor/3afb344b739a63bce84e39ade7cfe76d to your computer and use it in GitHub Desktop.
Change MySQL root password easily

Change MySQL root password easily

  1. Log on to your system as the user that MySQL server runs as (for example, mysql)

  2. Stop the MySQL server if it is running

shell> ps ax | grep mysql
3425 mysqld
shell> kill 3425
  1. Create a text file containing the password-assignment statement on a single line. (Replace the password with the password that you want to use)
ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';

Save the file. This example assumes that you name the file /home/me/mysql-init. The file contains the password, so do not save it where it can be read by other users. If you are not logged in as mysql (the user the server runs as), make sure that the file has permissions that permit mysql to read it.

  1. Start the MySQL server with the init_file system variable set to name the file:
shell> mysqld --init-file=/home/me/mysql-init &
  1. After the server has started successfully, delete /home/me/mysql-init

You should now be able to connect to the MySQL server as root using the new password. Stop the server and restart it normally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment