This is a very common problem for people using versions of Ubuntu Linux. Usually you install the MySQL server using Apt or Synaptic Package Manager and they setup packages without asking you for a root password. Then you realize that there a bunch of solutions for this problem, but none of them gives you a full, step by step description how to reset the root password. This is really very easy to do and I’ll tell you how.
I don’t know why I didn’t use the mysqladmin tool before, but it seems that it does everything we want, so let’s try this method before going to the actual resetting process. To do that just run the following command and if you have MYSQL newer than 5.5.3 you can pass the new-password parameter and type it when the tool will ask.
$ sudo mysqladmin password "new-password"
For more information enter the official documentation page – mysqladmin — Client for Administering a MySQL Server.
If everything works fine than you don’t need reed further, but if not, then let’s do it manually. First of all, stop the MySQL daemon:
$ sudo service mysql stop
or
$ sudo /etc/init.d/mysql stop
then start the mysql in the safe mode and skip grant tables for logging in without a password
$ sudo mysqld_safe --skip-grant-tables &
Run the command line mysql client and it will log you without asking for a password. Then use the mysql internal database:
> use mysql
Update the password of required user
> UPDATE users SET password=PASSWORD('mypassword') WHERE username="root" > FLUSH PRIVILEGES
Exit mysql client and restart the daemon.
$ sudo service mysql restart
That’s all, hope that one of the methods helped a bit at least.