17 February 2012

Resetting MySQL Root Password: Debian or Ubuntu

If you've forgotten your MySQL root password (or are involved with some nefarious activity), here is how you can reset that password on a Debian GNU/Linux system or an Ubuntu GNU/Linux system:

Find out the account that the system uses to start and stop MySQL (note that I've changed the password for this demo):
root#  cat /etc/mysql/debian.cnf



# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host     = localhost
user     = debian-sys-maint
password = 11223344554433
socket   = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
user     = debian-sys-maint
password = 11223344554433
socket   = /var/run/mysqld/mysqld.sock
basedir  = /usr


Log into MySQL with that account:
root#  mysql -u debian-sys-maint -p
Enter password:
mysql>

Change the password:
mysql>  update mysql.user set password=PASSWORD("YourNewPassW0RD") where User='root';
mysql>  flush privileges; exit;

Log into MySQL as root with your new password:
root#  mysql -u root -p
Enter password:
mysql> 


The above info is for a Debian GNU/Linux or an Ubuntu setup.  If you need to do this on a Red Hat Linux (RHEL), CentOS, or Scientific Linux system, check out this post.

0 comments: