Solution How To Change MySQL Root Password in Ubuntu 20.04
Solution How To Change MySQL Root Password in Ubuntu 20.04
MySQL MariaDB
Passwords are hard to remember, so if you have forgotten the MySQL root password,
luckily, there is a way to change it. This post has been written for you, and by the end of this
post, you will have successfully changed the password of MySQL.
Before getting straight to the solution, it is assumed that you are using the latest version of
the MySQL database on Ubuntu 20.04 LTS system. This post will provide a step-by-step
guide on how to change the MySQL root password in Ubuntu 20.04. So, without wasting
any time, let’s start.
$ mysql --version
:
[Fl linuxuser@linuxBox:~ Q
linuxuseralinuxBox:$mvsal--version
mysqlVer8.0.28-Oubuntu4forLinuxonX8664((Ubuntu))
linuxuser@linuxBox:$
Check the status of the MySQL server to verify using the command:
Linuxuser@linuxBox:[$sudosystemctistatusmysql.service
omysql.service-MySQLCommunityServer
Loaded:loaded(/lib/systemd/system/mysql.service;enabled;vendorpreset:
Active:inactive(dead)sinceTue2022-04-1915:18:54EDT;50sago
Process:641ExecStartPre=/us/share/mysql/mysql-systemd-startprecode=ex>
Process:781ExecStart=/us/sin/mysqld(code=exited,status=0/SUCCESS)
MainPID:781(codeexited,status=0/SUCCESS)
Status: "Servershutdowncomplete"
CPU:4.228s
Apr1915:10:51linuxuser-VBoxsystemd[1]:StartingMySQLCommunityServer
Apr1915:11:02linuxuser-VBoxsystemd[1]:StartedMySQLCommunityServer.
Apr1915:18:49linuxBoxsystemd[1]:StoppingMySQLCommunityServer.
Apr1915:18:54linuxBoxsystem[1]:mysql.service:Deactivatedsuccessfully.
Apr1915:18:54linuxBoxsystemd[1]:StoppedMySQLCommunityServer.
Apr1915:18:54linuxBoxsystemd[1]:mysql.service:Consumed4.228sCPUtime.
lines1-15/15(END)
FI linuxuser@linuxBox: Q
|linuuser@linuxBox:$sudosystemctlset-environmentMYSQLDOPTS=".-skip-networking
--skip-grant-tables"
Linuxuser@linuxBox:$
Alright, the environment variable is set, and we can log in to the MySQL shell without
providing any password.
linuxuser@linuxBox:~
Linuxuser@linuxBox:$sudosystemctlstartmysql.service
linuxuser@linuxBox:$
linuxuser@linuxBox:~
linuxuser@linuxBox:$sudosystemctlstatusmysql.service
•mysql.service-MySQLCommunityServer
Loaded:loaded(/lib/systemd/system/mysql.service;enabled;vendorpreset:enas
•Active:active(running)sinceTue2022-04-1915:22:53EDT;35sago
Process:3069ExecStartre=/us/share/mysql/mysql-systemd-startpre(code-exite
MainPID:3077(mysqld)
Status:"Serverisoperational"
Tasks:38(limit:1536)
Memory:390.6M
CPU:1.8225
(Group:/system.slice/mysql.service
L3077/us/sbin/mysqld
Apr1915:22:47linuxBoxsystemd[1]:StartingMySQLCommunityServer..
Apr1915:22:53linuxBoxsystemd[1]:StartedMySQLCommunityServer.
lines1-14/14(END)
linuxuseralinuxBox:$sudomysal-uroot
WelcometotheMySQLmonitor. Commandsendwith;or\g.
YourMySOLconnectionidis8
Serverversion:8.0.28-Oubuntu4(Ubuntu)
Copyright(c)2000,2022,Oracleand/oritsaffiliates.
OracleisaregisteredtrademarkofOracleCorporationand/orits
affiliates.Othernamesmaybetrademarksoftheirrespective
owners.
Type'help;'or"h'forhelp.Type'Ic'toclearthecurrentinputstatement.
mysql>/
Without providing any password, you will log in to the MySQL shell.
mysql>USEmysql
Readingtableinformationforcompletionoftableandcolumnnames
Youcanturnoffthisfeaturetogetaquickerstartupwith-A
Databasechanged
mysal>
And set the new password for the root user using the ALTER command by typing the
:
following statement:
mysql>ALTERUSER "root'@'localhost'IDENTIFIEDBY'the-new-password';
QueryOK,0rowsaffected(0.00sec)
mysql>
mysql> quit;
mysql>quit;
Bye
linuxuser@linuxBox:$
To do so, firstly unset the “MYSQLD_OPTS” environment variable which you have set
previously:
After doing so, remove the modified system configuration by reverting “MySQL”:
linuxuser@linuxBox:$sudosvstemctlrevertmvsal
linuxuser@linuxBox:~
Step 9: Kill all MySQL processes & Restart the MySQL service
Now, kill all the processes of MySQL before restarting the MySQL server:
After having the output as shown in the picture, hit “Enter” and restart the MySQL server
using the command given below:
linuxuser@linuxBox:~
linuxuser@linuxBox:$sudosystemctlrestartmysql.service
Linuxuser@linuxBox:$
This is it. MySQL’s root password is successfully changed, and you are logged back into
the MySQL shell.
Wrap Up
In this step-by-step guide, you have learned to change the MySQL root password on the
Ubuntu 20.04 LTS system. This post contains a profound and easy-to-understand solution
on resetting the root password of MySQL on Ubuntu 20.04.
:
ABOUT THE AUTHOR
Shehroz Azam
A Javascript Developer & Linux enthusiast with 4 years of industrial experience
and proven know-how to combine creative and usability viewpoints resulting
in world-class web applications. I have experience working with Vue, React &
Node.js & currently working on article writing and video creation.