Resetting MySQL Root Password On Ubuntu 16.04 / 17.10 And 18.04 LTS
We’re humans, and we forget stuff… when we do, we tend to go back in time to remember what was forgotten. The same is true with passwords. We do forgot a lot when it comes to passwords… and that’s why there are countless password managers to help us manage our passwords.
Forgetting your desktop password is one thing… however, fogetting the root password to your MySQL database in production is totally another thing and not fun…. and if you did, then you’re in the right place… the steps below will show you how to quickly reset the root password to your MySQL database server…
This brief tutorial is going to show students and new users how to quickly regain access to MySQL databases by resetting the root password.
When you’re ready to reset the root password for MysQL, continue with the steps below
Setting MySQL Root Password
To reset MySQL root password, logon to the Ubuntu server and run the commands below to stop MySQL database service
sudo /etc/init.d/mysql stop
Then run the commands below to create a new mysqld directory
sudo mkdir /var/run/mysqld/
and give mysql user access to it.
sudo chown mysql /var/run/mysqld/
After that, run the commands below to start MySQL in safe mode by bypassing the standard authentication process..
sudo mysqld_safe --skip-grant-tables &
You should see something like this… you may have to press the Enter key…
b2c@ubuntu1710:~$ 2017-12-25T16:49:30.551520Z mysqld_safe Logging to syslog. 2018-12-25T16:49:30.554646Z mysqld_safe Logging to '/var/log/mysql/error.log'. 2018-12-25T16:49:30.578079Z mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql 2018-12-25T16:49:32.568746Z mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended + Done sudo mysqld_safe --skip-grant-tables
Next, run the commands below to logon to the database server with the root account without typing a password… you’re given root access to the database without entering the password…
sudo mysql -u root
Once logged into the database, run the SQL command statement below to use the default mysql database… this database holds the settings for root user and other server settings…
mysql> use mysql; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed
Finally, run the SQL statement below to change the root password
update user set authentication_string=PASSWORD("New_Passwore_Here") where User='root';
Save the change by running the commands below
flush privileges; exit;
Finally, stop MySQL safe_mode and start MySQL default service
sudo /etc/init.d/mysql stop sudo /etc/init.d/mysql start If you did everything as described above, you should be able to log back onto MySQL database using the root new password.
sudo mysql -u root -p
This is how one resets MySQL root user.