0% found this document useful (0 votes)
68 views

Solution How To Change MySQL Root Password in Ubuntu 20.04

This document provides a step-by-step guide to changing the MySQL root password on Ubuntu 20.04. It begins by checking the MySQL version, stopping the MySQL service. It then sets an environment variable to start MySQL without granting tables or networking. The guide then signs into the MySQL shell without a password, uses the ALTER command to set a new password, and restarts MySQL in normal mode.

Uploaded by

wahab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

Solution How To Change MySQL Root Password in Ubuntu 20.04

This document provides a step-by-step guide to changing the MySQL root password on Ubuntu 20.04. It begins by checking the MySQL version, stopping the MySQL service. It then sets an environment variable to start MySQL without granting tables or networking. The guide then signs into the MySQL shell without a password, uses the ALTER command to set a new password, and restarts MySQL in normal mode.

Uploaded by

wahab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

HOME YOUTUBE TWITTER SUBSCRIBE 

MySQL MariaDB

How to Change MySQL Root


Password in Ubuntu 20.04
2 years ago • by Shehroz Azam

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.

Step 1: Check the version of MySQL on Ubuntu 20.04


First of all, check the version of your MySQL because this post contains the solution of
changing the root password on version 8 or higher. If the version of your MySQL is lower
than 8, then the solution will be different. The command for checking the version of MySQL
is given below:

$ mysql --version
:
[Fl linuxuser@linuxBox:~ Q

linuxuseralinuxBox:$mvsal--version
mysqlVer8.0.28-Oubuntu4forLinuxonX8664((Ubuntu))
linuxuser@linuxBox:$

Step 2: Stop the MySQL server


To change the MySQL root password, you first need to shut down the MySQL server, and
you can do so using the command:

$ sudo systemctl stop mysql.service

Check the status of the MySQL server to verify using the command:

$ sudo systemctl status mysql.service


:
FI linuxuser@linuxBox:~

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)

Step 3: Skip Grant Tables & Networking


To start the MySQL server without granting the tables and networking check, set the
environment variable “MYSQLD_OPTS” which MySQL uses on startup:

$ sudo systemctl set-environment MYSQLD_OPTS="--skip-networking --skip-grant-tables"

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.

Step 4: Start the MySQL service


After setting the environment variable “MYSQLD_OPTS”, start the MySQL service now
using the command:
:
$ sudo systemctl start mysql.service

linuxuser@linuxBox:~

Linuxuser@linuxBox:$sudosystemctlstartmysql.service
linuxuser@linuxBox:$

Step 5: Confirm the status of the MySQL Server


Confirm the status of the MySQL service, whether it is running or not:

$ sudo systemctl status mysql.service

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)

Step 6: Sign In to the MySQL shell


Now, you need to log in as a root user to the MySQL server and for signing in to the MySQL
shell, type the command:

$ sudo mysql -u root


:
FI linuxuser@linuxBox:~

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.

Step 7: Alter the root password


Now, flush the privileges first:

mysql> flush privileges;

Select the MySQL database:

mysql> USE mysql

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> ALTER USER  'root'@'localhost' IDENTIFIED BY 'the-new-password';

mysql>ALTERUSER "root'@'localhost'IDENTIFIEDBY'the-new-password';
QueryOK,0rowsaffected(0.00sec)
mysql>

At the place of “the-new-password” provide your new password. After successfully


changing the MySQL root password, log out from the MySQL shell:

mysql> quit;

mysql>quit;
Bye
linuxuser@linuxBox:$

Step 8: Revert Database Server to Normal settings


For restarting the database server in “normal” mode, it is required to “revert” the changes
we have made so that the networking is enabled and grant tables are loaded.

To do so, firstly unset the “MYSQLD_OPTS” environment variable which you have set
previously:

$ sudo systemctl unset-environment MYSQLD_OPTS

After doing so, remove the modified system configuration by reverting “MySQL”:

$ sudo systemctl revert mysql


:
linuxuser@linuxBox:~

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:

$ sudo killall -u mysql

After having the output as shown in the picture, hit “Enter” and restart the MySQL server
using the command given below:

$ sudo systemctl restart mysql.service

linuxuser@linuxBox:~

linuxuser@linuxBox:$sudosystemctlrestartmysql.service
Linuxuser@linuxBox:$

Step 10: Log in with the newly set Password


Log in to the MySQL shell with the newly set password:

$ sudo mysql -u root -p


:
Provide the recently set password in the ALTER command:

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.

View all posts

RELATED LINUX HINT POSTS

Import JSON into MySQL

What Does the Colon Equals


Operator “:=” Mean in MySQL?

How to Add Primary Key on


Multiple Columns in MySQL?

How to Use DECIMAL Data Type in


MySQL?

What Does Full Outer Join Do in


MySQL?

How to Identify and Kill Queries


Using Command Line Tool in
MySQL?

How to Get List of Running Queries


in MySQL Processlist?
:
Linux Hint LLC, [email protected]
1309 S Mary Ave Suite 210, Sunnyvale, CA
94087
Privacy Policy and Terms of Use
:

You might also like