Skip to content

Instantly share code, notes, and snippets.

@ThomasG77
Created January 24, 2024 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThomasG77/65664f0eaf9dea93da536914b88b4744 to your computer and use it in GitHub Desktop.
Save ThomasG77/65664f0eaf9dea93da536914b88b4744 to your computer and use it in GitHub Desktop.
Reset your MySQL root password

Reset your MySQL root password

Only used on localhost so did not feel concerned about the password weakness here

sudo service mysql stop
echo "ALTER USER 'root'@'localhost' IDENTIFIED BY 'basicPassword1234';" >| $HOME/mysql-init
sudo mysqld --init-file=$HOME/mysql-init &
sudo service mysql start

Then

sudo mysql -p # and type your password here basicPassword1234

Alternative recipe by generating a password

sudo service mysql stop
mypassword=$(strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 30 | tr -d '\n'; echo
)
echo ${mypassword} # as it's generated, you need to keep trace of the password to put in your password manager
echo "ALTER USER 'root'@'localhost' IDENTIFIED BY '"${mypassword}"';" >| $HOME/mysql-init
sudo mysqld --init-file=$HOME/mysql-init &
sudo service mysql start

Then

sudo mysql -p # and type generated password here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment