Just in case you don’t know the password of your MySQL root user anylonger, this is how you can set a new one.
Windows users, follow this guide.
Linux / Unix users: This is for Debian (wheezy) and MySQL 5.5, but should work more or less the same on any linux/unix:
Either do this as (linux user) root or prefix every command with sudo:
- Stop the MySQL server
service mysql stop
- Create a init file:
touch /etc/mysql/mysql-init
- Write the following into this file (adjust the PW):
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPassword');
- Restart MySQL with this init-file:
mysqld_safe --init-file=/etc/mysql/mysql-init
- The new password should work now. Try it:
mysql -u root -p
- Restart MySQL normally:
service mysql restart
- Remove the init-file
rm /etc/mysql/mysql-init
And you’re done. I hope this helps somebody.
Note: if it does not work, it is most likely because the MySQL process cannot access the init-file (e.g. because of missing access rights). In this case check your (sys)logs for stuff like this:
mysqld: 130901 14:20:30 [ERROR] /usr/sbin/mysqld: File '/root/mysql-init' not found (Errcode: 13)
I had this problem first because I tried creating the init-file in /root where the mysql-process was not able to read it.
Great! Worked, and saved me 🙂
THANK YOU
Comment by Giovanni — 29. January 2015 @ 13:48
Permission denied 🙁
Comment by Simon — 4. February 2015 @ 20:20
@Simon: are you logged in as root? or tried sudo?
Comment by Christopher Kramer — 5. February 2015 @ 10:37
Yo dude! Certainly helped me! Thanks
Comment by JimmehWhy — 10. April 2015 @ 23:13
in windows it is not working
Comment by Bharath k — 28. May 2015 @ 08:15
@Bharath k: The guide is for Linux 😀
But on Windows, it is working more or less the same way. See: http://dev.mysql.com/doc/mysql-windows-excerpt/5.5/en/resetting-permissions-windows.html
Comment by Christopher Kramer — 28. May 2015 @ 10:34
Thank you so much! This worked great on my raspberry!
Comment by Daniel — 11. February 2016 @ 11:15
Thanks, worked for my Raspberry Pi 3, but couldn’t work out how to close it. Ctrl+Z
(Might have been because I didn’t press enter after the SET PASSWORD FOR line?)
Comment by abisdad — 3. July 2017 @ 15:52
touch /etc/mysql/mysql-init only gives can’t change – acces denied….
Comment by yoeri — 18. February 2018 @ 12:48
@yoeri: You should do this as root or use sudo.
Comment by Christopher Kramer — 19. February 2018 @ 14:29
[…] You tried a dozen different howtos to change the root password of MySQL (5.7+) in a recent Ubuntu version (18.04+) and did not succeed? Like the one I posted in 2013? […]
Pingback by MySQL root password in Ubuntu cannot be changed - Christosoft Blog — 2. February 2020 @ 16:09