Change MySQL root Password when you lost it

Posted under » MySQL on 31 March 2014

Maybe you lost your password. Maybe your password is too difficult even for you to remember. Maybe a hacker stole it. Whateva it is you want it back so bad.

At first I read up the manual from the source itself but I can't seem to get it to work on ubuntu.

The other part of the manual work though. Firstly, shut down mysql service. This is to restart it in an unsecure manner so that you can "hack it".

mysqld_safe --skip-grant-tables & 

However, you may encounter this error in Ubuntu 18.06 Bionic. "mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists." So you need to create the directory and

$ mkdir  /var/run/mysqld
$ chown mysql:mysql /var/run/mysqld
$ mysqld_safe --skip-grant-tables &
$ jobs 
[1]+  Running     sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking  & // r

While this mysql is running, open up another terminal to connect to the mysql server. You don't have to use password because you have skipped tables.

$ mysql -u root
mysql>UPDATE mysql.user SET Password=PASSWORD('LeeKwanYewSux') WHERE User='root';

Now stop the unsafe instance of the MySql service.

$ pkill mysqld
$ jobs
[1]+  Done       sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking //

Restart the service normally.

Try your new root password which will work.

mysql -u root -p

Changing Mysql root password in Ubuntu Server 20.04 after install.

On normal situation, you can change your root password like this.

web security linux ubuntu GIT svn Raspberry apache mysql php drupal cake symfony javascript Ajax css