Register now and start sharing your code snippets.
-->
How to reset the MySQL root password
Shell Script (Bash) posted about 1 year ago by christian
I just happened to lock myself out of MySQL, but luckily I have root access to the server so I can reset it easily by first shutting down MySQL:
1 /etc/init.d/mysql stop
And then creating a MySQL init file with the desired password:
1 $ echo "SET PASSWORD FOR 'root'@'localhost' = PASSWORD('1234');" >> /tmp/mysql_init.txt
Starting MySQL with the —init-file parameter like this resets the password:
1 $ mysqld_safe --init-file=/tmp/mysql_init.txt
Remember to delete the file:
1 rm /tmp/mysql_init.txt